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.