Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Visual Studio.NET / Extensibility / June 2007

Tip: Looking for answers? Try searching our database.

Rename Form Controls

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
John - 21 Jun 2007 19:35 GMT
Hi there,

I'm trying to write a macro that will run through an active project in
VS2005 and get a reference to all controls in each form, so that I can later
rename them.  My code so far is below, but my questions are:

a) How can I find out if a project item is a form (as opposed to some other
file type)?

b) Once I can identify a form, is it just a case of iterating through:
something like For Each ctrlObj in frmObj.Controls?

c) Are there any issues I need to be aware of in changing the controls names
directly?  (ie will the form designer file get upset?)

Thanks in advance.

Best regards

John

Sub GetFormControls()

Dim selectedProjs As Object

Dim proj As EnvDTE.Project

Dim projItem As EnvDTE.ProjectItem

selectedProjs = DTE.ActiveSolutionProjects

For Each proj In selectedProjs

For Each projItem In proj.ProjectItems

If Right(projItem.Name, 3) = ".vb" Then

'NEXT CODE HERE

End If

Next

Next

End Sub
Walter Wang [MSFT] - 22 Jun 2007 06:30 GMT
Hi John,

This is a quick note to let you know that I am performing research on this
issue and will get back to you as soon as possible. I appreciate your
patience.

Regards,
Walter Wang (wawang@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
Walter Wang [MSFT] - 25 Jun 2007 08:21 GMT
Hi John,

So far I've only found some example on how to do this in an Add-in instead
of a macro:

#Download details: Visual Studio 2005 Automation Samples
http://www.microsoft.com/downloads/details.aspx?FamilyID=79c7e038-8768-4e1e-
87ae-5bbbe3886de8&displaylang=en

Check out the "WinFormsAutomation" sample add-in.

Please note you will have to get to the designer service by its window, I
don't think it's possible to get it without opening it, which means it's
not possible to simply iterate over the ProjectItems collection and see if
an item is a form: without opening it, it's merely a text file.

When a ProjectItem is opened, you can check if currently opened in a
designer by checking if ActiveWindow.Object is a IDesignerHost object:

If TypeOf _applicationObject.ActiveWindow.Object is IDesignerHost Then
..

After you get the IDesignerHost reference, you can use its
.Container.Components to iterate through the child controls. Here's some
code to print out each control's Name:

   Public Sub Exec(ByVal commandName As String, ByVal executeOption As
vsCommandExecOption, ByRef varIn As Object, ByRef varOut As Object, ByRef
handled As Boolean) Implements IDTCommandTarget.Exec
       handled = False
       If executeOption = vsCommandExecOption.vsCommandExecOptionDoDefault
Then
           If commandName =
"RenameControlDemo2.Connect.RenameControlDemo2" Then
               If TypeOf _applicationObject.ActiveWindow.Object Is
IDesignerHost Then
                   Dim designer As IDesignerHost =
CType(_applicationObject.ActiveWindow.Object, IDesignerHost)
                   Dim i As Integer
                   For i = 0 To designer.Container.Components.Count - 1
                       Dim comp As IComponent =
designer.Container.Components.Item(i)
                       Dim pd As PropertyDescriptor =
TypeDescriptor.GetProperties(comp)("Name")
                       Debug.Print(CType(pd.GetValue(comp), String))
                       If CType(pd.GetValue(comp), String) = "Button1" Then
                           pd.SetValue(comp, "myButton1")
                       End If
                   Next
               End If

               handled = True
               Exit Sub
           End If
       End If
    End Sub

You should be able to use pd.SetValue() to change the control's Name.
Changing name this way is supported, the designer will invoke code
refactoring for you when you change the Name property.

Hope this helps.

Regards,
Walter Wang (wawang@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
John - 25 Jun 2007 08:46 GMT
Morning Walter,

Thanks very much for your comprehensive reply.  I'll get started on this,
this morning.

Best regards

John

> Hi John,
>
[quoted text clipped - 71 lines]
> This posting is provided "AS IS" with no warranties, and confers no
> rights.
John - 25 Jun 2007 18:55 GMT
Hi Walter,

Thanks again for your response to this issue.  Your code sample has helped
me make a good deal of headway with it.

A note FYI on the Automation samples is that I got a corrupted cab file
error when trying to install the msi.

The code sample was useful too and it's given me some good insights into the
DTE object model.  After all this however I've discovered the Document
Outline tool which allows you to rename controls.  I notice it doesn't
refactor the code behind but that's acceptable at the moment as my main task
at the moment is to rename controls prior to the coding taking place.

Anyway, thanks again for a really useful response.

Best regards

John

> Morning Walter,
>
[quoted text clipped - 83 lines]
>> This posting is provided "AS IS" with no warranties, and confers no
>> rights.
Walter Wang [MSFT] - 26 Jun 2007 02:45 GMT
Hi John,

Thanks for the update.

I just re-downloaded the file and it seems working fine on my side. FYI:
the file VS2005AutomationSamples.msi should have size 1,445,376 bytes, and
md5 checksum is BAF611ED6A969854214657BC322C3455.

Didn't noticed that you only need to find a easier way to batch rename the
controls; anyway, I'm glad that you've found the approach of using the
Document Outline window.

Have a nice day!

Regards,
Walter Wang (wawang@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.