Create control collection dynamically in Vb2005

Hi, does anyone know if you can create a vartiable number of controls(labels) dynamically in vb2005. Add design time the number of controls and names are unknown.


Answer this question

Create control collection dynamically in Vb2005

  • SC_Rambo

    If you need to convert to a Label then:




    Private Sub Label_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

       Dim lbl As Label = DirectCast(sender, Label)

       Messagebox.Show(Me.Controls.IndexOf(lbl).ToString)

    End Sub


     



    Else, just use the sender:




    Private Sub Label_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

      Messagebox.Show(Me.Controls.IndexOf(sender).ToString)

    End Sub


     



    -Joe



  • Dusan Kocurek

    Here's how to create the event handler:

        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

            Dim lblNew As Label
            Dim intCount As Integer

            For intCount = 1 To 10
                lblNew = New Label()
                lblNew.Text = "Label #" & intCount
                lblNew.Top = (intCount * lblNew.Height) + 10
                AddHandler lblNew.Click, New EventHandler(AddressOf Label_Click)
                Me.Controls.Add(lblNew)
            Next

        End Sub

        Private Sub Label_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

            MsgBox("Hello World")

        End Sub


  • Kim Meyer

    Works great thanks, however I am having trouble trying to identify the index value of the selected label. It can be found but is not unique to the labels collection but apppears as an index of the forms controls collection. The following example shows how I tried to identify it.    Dim objType As Type
        Dim newtype As Type

        Private Sub Label_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

            'MsgBox(sender.ToString)
            objType = sender.type
            newtype = Label
            If objType IsNot newtype Then
                Exit Sub
            Else
                MsgBox(Me.Controls.IndexOf(sender))
            End If

        End Sub

    The code does not work as a label is a type and cannot be used in an expression. Any ideas

    Regards
              Kev


  • -lance

    Thanks fopr the quick response. If you could provide a way of including event handlers it would be greatly appreciated. I am trying to load a range of labels into a container so the user can click and drag them to any position within the container. A Printable label template of sorts. Tried in VB6 but only partially successful. Only started on .Net yesterday so stumbling everywhere.

    Regards   
             Kev


  • cagla

            Dim lblNew As Label
            Dim intCount As Integer

            For intCount = 1 To 10
                lblNew = New Label()
                lblNew.Text = "Label #" & intCount
                lblNew.Top = (intCount * lblNew.Height) + 10

                Me.Controls.Add(lblNew)
            Next

    If you need to add event handlers please reply!

    Thanks,
    Tyler

  • Create control collection dynamically in Vb2005