Adding Controls to Windows Forms using DTE

Hi All,

I managed to create new Windows Appliation Projects in the runtime using the Application Object Model and to add forms to it. But I couldn't figure out how add controls (textboxes, labels,...) to it in the runtime. Can anybody give me a clue

Regards,

Imesh



Answer this question

Adding Controls to Windows Forms using DTE

  • Vista lover

    Hi Imesh,

    I would suggest you check out the "Windows Forms Automation" sample from the VS2005 Automation Samples.

    In short, you need to get an instance of the designer's IDesignerHost interface and use that. For example, you can add a button to the active form with the following code:

    If TypeOf DTE.ActiveDocument.ActiveWindow.Object Is IDesignerHost Then

    Dim host As IDesignerHost = CType(DTE.ActiveDocument.ActiveWindow.Object, IDesignerHost)

    Dim button As IComponent = host.CreateComponent(host.GetType("System.Windows.Forms.Button,System.Windows.Forms"))

    TypeDescriptor.GetProperties(button)("Parent").SetValue(button, host.RootComponent)

    End If

    I encourage you to check out the full sample though since it provides more detail. Hope that helps!

    Aaron Marten



  • James Owens

    See also:

    HOWTO: Add a control to a Windows form from a Visual Studio add-in

    http://www.mztools.com/Articles/2006/MZ016.htm

    You have other articles at my web site.

    --
    Best regards,
    Carlos J. Quintero
    MZ-Tools: Productivity add-ins for Visual Studio
    You can code, design and document much faster:
    http://www.mztools.com



  • gcollins

    Hi Aaron,

    Thanks for your reply, I will look into it.

    Regards,

    Imesh


  • Henry Chen

    Thanks Aaron, I found it in Windows Forms Automation example but the problem is its in VB. I will try my best to convert it in to C#.

    Regards,
    Imesh


  • Adding Controls to Windows Forms using DTE