Results 1 to 6 of 6

Thread: Controls at Runtime

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2004
    Location
    Manchester
    Posts
    266

    Controls at Runtime

    Ladies, gents and everything in between. I've been searching far and wide across the lands to find a way to create an array of controls at runtime. I basically have a user enter text in a text box, click on the form and the text then appears in the form of a label at the cursor location.

    However,

    I can't work out how to create an array of controls so that I can manage them, iterate through them etc when required. Is there a simple way to create a control array without using complicated API's or workarounds?

    (I also require this so that I can have click events for each individual label)

    Cheers,
    Jord

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,415

    Re: Controls at Runtime

    here's how. this creates an array of labels + an array of textboxes from existing controls:

    vb Code:
    1. Public Class Form1
    2.  
    3.     Dim labels() As Label
    4.     Dim textboxes() As TextBox
    5.  
    6.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    7.         labels = New Label() {Label1, Label2, Label3}
    8.         textboxes = New TextBox() {TextBox1, TextBox2, TextBox3}
    9.     End Sub
    10.  
    11. End Class

  3. #3
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Controls at Runtime

    This code will get all of the controls. Based on your requirements I don't see how this helps.

    Code:
        Dim allControls As New Dictionary(Of String, Control)
        Private Sub GetAllControls()
            Dim ctrl As Control = Me.GetNextControl(Me, True)
            Do Until ctrl Is Nothing
                allControls.Add(ctrl.Name, ctrl)
                ctrl = Me.GetNextControl(ctrl, True)
            Loop
        End Sub
    You can have click events for labels without it.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2004
    Location
    Manchester
    Posts
    266

    Re: Controls at Runtime

    Quote Originally Posted by .paul. View Post
    here's how. this creates an array of labels + an array of textboxes from existing controls:

    vb Code:
    1. Public Class Form1
    2.  
    3.     Dim labels() As Label
    4.     Dim textboxes() As TextBox
    5.  
    6.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    7.         labels = New Label() {Label1, Label2, Label3}
    8.         textboxes = New TextBox() {TextBox1, TextBox2, TextBox3}
    9.     End Sub
    10.  
    11. End Class
    Thanks for this pointer. The difficulty is that the controls are created at runtime, they don't already exist.

    Code:
    Dim newlab As New Label
                newlab.Text = textBxad.Text
                newlab.Left = Cursor.Position.X - 15
                newlab.Top = Cursor.Position.Y - 118
                newlab.AutoSize = True
                textBxad.Text = ""
                groupbox.Controls.Add(newlab)
    I'd like to have an index through which I can iterate. i.e newlab(1), newlabl(2) etc. The code I have quoted works fine but I assume it just creates a new "newlab" label and forgets about the old one each time it runs resulting in the old label being inaccessible.

  5. #5
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Controls at Runtime

    So create them in a loop

    Code:
            'assumes GroupBox is created
            For x As Integer = 1 To 10
                Dim newlab As New Label
                newlab.Name = "myLbl" & x.ToString
                newlab.Text = textBxad.Text
                newlab.Left = Cursor.Position.X - 15
                newlab.Top = Cursor.Position.Y - 118
                newlab.AutoSize = True
                textBxad.Text = ""
                AddHandler newlab.Click, AddressOf fooLabel_Click 'some handler if needed
                GroupBox.Controls.Add(newlab)
            Next x
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  6. #6
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    Re: Controls at Runtime

    I agree. If you are creating the controls, then you get to say where they are located. While you have to put them into the proper controls collection, you can also put them anywhere else that you want. In your case, you also want them in some kind of a collection, perhaps a list, or possibly a Dictionary such that you can access them by name.
    My usual boring signature: Nothing

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