CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Aug 2001
    Location
    Minneapolis, MN, USA
    Posts
    150

    Adding Controls at Runtime

    Does anyone know how to add controls at runtime?

    dim btn as new button '?

    I want to add an array of user controls which I have created and I'm stumped!

    Thanks....

  2. #2
    Join Date
    Mar 2002
    Location
    NY
    Posts
    236

    Re: Adding Controls at Runtime

    I haven't tried that, but... here is the link:
    http://support.microsoft.com/directo...308433&SD=MSDN

  3. #3
    Join Date
    Aug 2001
    Location
    Minneapolis, MN, USA
    Posts
    150

    Adding Controls at Runtime

    Looks good Akim! Thanks!

  4. #4
    Join Date
    May 2000
    Location
    New York, NY, USA
    Posts
    2,878
    Dim Form2 as New Form()
    Dim btnCancel as New Button()

    'set button properties
    btnCancel.text = "Cancel"
    btnCancel.Location = New Point(110,100)

    'add new object to control collection
    Form2.Controls.Add(btnCancel)

    'show form as DialogBox
    Form2.ShowDialog()

    another example------------------------------------------

    VB6 (This code adds a TextBox to the form and makes it visible.)
    Dim myControl As Control
    Set myControl = Me.Controls.Add("VB.TextBox", "myControl")
    myControl.Visible = True
    'remove control
    Me.Controls.Remove "myControl"

    VB.NET
    Dim c As Control
    c = New TextBox()
    c.Name = "myControl"
    Me.Controls.Add(c)

    In Windows Forms, controls are indexed by number, not by name. To remove a control by name, you have
    to iterate through the controls collection, find the control, and remove it. The following code shows how
    to remove the newly added control by name:

    Dim c As Control
    For Each c In Me.Controls
    If c.Name = "myControl" Then
    Me.Controls.Remove(c)
    Exit For
    End If
    Next
    Iouri Boutchkine
    [email protected]

  5. #5
    Join Date
    Mar 2002
    Location
    NY
    Posts
    236
    Here is THE MAN
    Thanks Iouri.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured