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 / IDE / January 2006

Tip: Looking for answers? Try searching our database.

Add cotrold to a form by using a macro

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Matthew Heinrich - 17 Jan 2006 17:01 GMT
Is there a way to add a control with a macro to the current form I am
working on? What I am looking to do is that every time I add a textbox I
would like to add a label.

Thanks Matt

PS. I am using VS 2005
"Gary Chang[MSFT]" - 18 Jan 2006 08:43 GMT
Hi Matthew,

>Is there a way to add a control with a macro to the current
>form I am working on?

Since the VS Macro cannot access some necessary Visual Studio service, I am
afraid you could not use the VS Macroto add a control in the current form,
but it would be easy to do such task via a Visual Studio Add-in, and
currently we have already provided a VS2005 automation sample to do
this--Windows Forms Automation, that add-in demonstrates working with the
object model provided by the
Windows Forms designer. It will create a new Windows Application project,
and use the object model to build up a form.

Please download it for reference:

http://www.microsoft.com/downloads/details.aspx?FamilyID=79c7e038-8768-4e1e-
87ae-5bbbe3886de8&displaylang=en

To add a Label control to the currently activated Winform in the designer,
you can simplify the Exec method of the Windows Forms
Automation(connect.vb) sample as the following:

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 =
"WinFormsAutomation.Connect.WinFormsAutomation" Then
               '' Get IDesignerHost, the root of the forms designer object
model
               Dim host As IDesignerHost
               host = CType(_applicationObject.ActiveWindow.Object,
IDesignerHost)

               Dim label As IComponent
               label =
host.CreateComponent(host.GetType("System.Windows.Forms.Label,System.Windows
Forms"))
     
               Dim parent As PropertyDescriptor =
TypeDescriptor.GetProperties(Label)("Parent")
               parent.SetValue(label, host.RootComponent)
     
               Dim pdc As PropertyDescriptorCollection
               pdc = TypeDescriptor.GetProperties(label)
   

               '' Get and set the value of the size proeprty, text values
               '' before and after.
               Dim pd As PropertyDescriptor
               pd = pdc("Size")
               Dim sz As System.Drawing.Size
               sz = New Size(150, 20)
               pd.SetValue(label, sz)

               pd = pdc("Text")
               Dim tz As String
               tz = "Sample Label"
               pd.SetValue(label, tz)
               
           End If
       End If
End Sub

Wish it helps!

Best regards,

Gary Chang
Microsoft Community Support
--------------------
Get Secure! ¡§C www.microsoft.com/security
Register to Access MSDN Managed Newsgroups!
http://support.microsoft.com/default.aspx?scid=/servicedesks/msdn/nospam.asp
&SD=msdn

This posting is provided "AS IS" with no warranties, and confers no rights.
Matthew Heinrich - 18 Jan 2006 17:04 GMT
Gary,
Thank you very much I will give it a try.

Matt
> Hi Matthew,
>
[quoted text clipped - 77 lines]
> This posting is provided "AS IS" with no warranties, and confers no
> rights.
"Gary Chang[MSFT]" - 19 Jan 2006 02:55 GMT
You are welcome, Matt.

If you have any other problems about it, please let me know. It's my
pleasure to be of assistance.

Good Luck!

Best regards,

Gary Chang
Microsoft Community Support
--------------------
Get Secure! ¡§C www.microsoft.com/security
Register to Access MSDN Managed Newsgroups!
http://support.microsoft.com/default.aspx?scid=/servicedesks/msdn/nospam.asp
&SD=msdn

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.