Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. how to create control array ? in vb .net

how to create control array ? in vb .net

Scheduled Pinned Locked Moved Visual Basic
csharpdesigndata-structureshelp
11 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B babusat

    i want to create control array in vb.net in runtime or design time help me sathish

    T Offline
    T Offline
    Tom Deketelaere
    wrote on last edited by
    #2

    what do you mean exactly. from what I understand you could do it with the list variable. example: dim l as list (of button) dim l as list (of textbox) dim l as lizt (of control) (don't know how/if this will work though)

    B 1 Reply Last reply
    0
    • T Tom Deketelaere

      what do you mean exactly. from what I understand you could do it with the list variable. example: dim l as list (of button) dim l as list (of textbox) dim l as lizt (of control) (don't know how/if this will work though)

      B Offline
      B Offline
      babusat
      wrote on last edited by
      #3

      exactly i want create control array like vb like example: textbox(0) in runtime or at design time sathish

      T 1 Reply Last reply
      0
      • B babusat

        exactly i want create control array like vb like example: textbox(0) in runtime or at design time sathish

        T Offline
        T Offline
        Tom Deketelaere
        wrote on last edited by
        #4

        Then I would use the list example code that creates a list and fills it with 20 textbox Private l As New List(Of TextBox) Private Sub filltextbox() Dim i As Integer = 0 Do While i < 20 Dim txt As New TextBox txt.Text = "test" + i l.Add(txt) i += 1 Loop End Sub

        B 1 Reply Last reply
        0
        • T Tom Deketelaere

          Then I would use the list example code that creates a list and fills it with 20 textbox Private l As New List(Of TextBox) Private Sub filltextbox() Dim i As Integer = 0 Do While i < 20 Dim txt As New TextBox txt.Text = "test" + i l.Add(txt) i += 1 Loop End Sub

          B Offline
          B Offline
          babusat
          wrote on last edited by
          #5

          it is very useful but i want to access textbox values using there index value and in this the textbox names are different why ? sathish

          T 1 Reply Last reply
          0
          • B babusat

            it is very useful but i want to access textbox values using there index value and in this the textbox names are different why ? sathish

            T Offline
            T Offline
            Tom Deketelaere
            wrote on last edited by
            #6

            don't really know what you mean the textbox names aren't different I just added text to it as a way to show how to access te property's If you want to access the textbox with index value you could always give that index value along with the textbox.tag property and then loop true the list untill you find the right textbox

            1 Reply Last reply
            0
            • B babusat

              i want to create control array in vb.net in runtime or design time help me sathish

              T Offline
              T Offline
              TwoFaced
              wrote on last edited by
              #7

              There is no design time support for creating control arrays. Here is some code that demonstrates a few different approaches.

              Public Class Form1
              ' An array of textbox's
              Private ControlArray As New List(Of TextBox)

              Private Sub Form1\_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
                  ' Here I am adding TextBox's that already exist on my form to the array
                  With ControlArray
                      .Add(TextBox1)
                      .Add(TextBox2)
                      .Add(TextBox3)
                  End With
              
                  ' Loop through each textbox in the array
                  For Each tb As TextBox In ControlArray
                      MsgBox(tb.Name)
                  Next
              
                  ' Access a textbox by index
                  MsgBox(ControlArray(1).Name)
              
                  ' Here I am creating textboxes dynamically and adding them
                  ' to the array
                  For i As Integer = 0 To 2
                      ' Create a textbox
                      Dim tb As New TextBox
                      ' Set it's position
                      tb.Location = New Point(20, 10 + i \* 30)
                      ' Set it's width
                      tb.Width = 100
              
                      ' Add the textbox to the array
                      ControlArray.Add(tb)
              
                      ' Add the textbox to the form
                      Me.Controls.Add(tb)
              
                      ' Add handlers for the GotFocus and LostFocus events
                      AddHandler tb.GotFocus, AddressOf TextBox\_GotFocus
                      AddHandler tb.LostFocus, AddressOf TextBox\_LostFocus
                  Next
              
                  ' Here I've looped through the forms controls and found
                  ' all the textboxes and added them to the array
                  ' This is a good approach if you have many controls you added at
                  ' design time but don't want to manually add each one to the array.
                  ' The desired event handlers can also be dynamically added this way
                  For Each ctrl As Control In Me.Controls
                      Dim tb As TextBox = TryCast(ctrl, TextBox)
                      If tb IsNot Nothing Then
                          ControlArray.Add(tb)
                      End If
                  Next
              End Sub
              
              ' Handles the textboxes GotFocus event
              ' I've handled the controls I created at design time by including them after the handles keyword
              Private Sub TextBox\_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.GotFocus, TextBox2.GotFocus, TextBox3.GotFocus
                  ' tb is a reference to the textbox that raised this event
                  Dim tb As TextBox = DirectCast(sender, TextBox)
                  tb.BackColor = Color.Wheat
              End Sub
              
              ' Handles the textboxe
              
              B 1 Reply Last reply
              0
              • T TwoFaced

                There is no design time support for creating control arrays. Here is some code that demonstrates a few different approaches.

                Public Class Form1
                ' An array of textbox's
                Private ControlArray As New List(Of TextBox)

                Private Sub Form1\_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
                    ' Here I am adding TextBox's that already exist on my form to the array
                    With ControlArray
                        .Add(TextBox1)
                        .Add(TextBox2)
                        .Add(TextBox3)
                    End With
                
                    ' Loop through each textbox in the array
                    For Each tb As TextBox In ControlArray
                        MsgBox(tb.Name)
                    Next
                
                    ' Access a textbox by index
                    MsgBox(ControlArray(1).Name)
                
                    ' Here I am creating textboxes dynamically and adding them
                    ' to the array
                    For i As Integer = 0 To 2
                        ' Create a textbox
                        Dim tb As New TextBox
                        ' Set it's position
                        tb.Location = New Point(20, 10 + i \* 30)
                        ' Set it's width
                        tb.Width = 100
                
                        ' Add the textbox to the array
                        ControlArray.Add(tb)
                
                        ' Add the textbox to the form
                        Me.Controls.Add(tb)
                
                        ' Add handlers for the GotFocus and LostFocus events
                        AddHandler tb.GotFocus, AddressOf TextBox\_GotFocus
                        AddHandler tb.LostFocus, AddressOf TextBox\_LostFocus
                    Next
                
                    ' Here I've looped through the forms controls and found
                    ' all the textboxes and added them to the array
                    ' This is a good approach if you have many controls you added at
                    ' design time but don't want to manually add each one to the array.
                    ' The desired event handlers can also be dynamically added this way
                    For Each ctrl As Control In Me.Controls
                        Dim tb As TextBox = TryCast(ctrl, TextBox)
                        If tb IsNot Nothing Then
                            ControlArray.Add(tb)
                        End If
                    Next
                End Sub
                
                ' Handles the textboxes GotFocus event
                ' I've handled the controls I created at design time by including them after the handles keyword
                Private Sub TextBox\_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.GotFocus, TextBox2.GotFocus, TextBox3.GotFocus
                    ' tb is a reference to the textbox that raised this event
                    Dim tb As TextBox = DirectCast(sender, TextBox)
                    tb.BackColor = Color.Wheat
                End Sub
                
                ' Handles the textboxe
                
                B Offline
                B Offline
                babusat
                wrote on last edited by
                #8

                thanks for your help and another thing that i want to known textbox name which currently active. i could not get it name from MsgBox(Me.ActiveControl.Name) this command why ? thanks sathish

                T 1 Reply Last reply
                0
                • B babusat

                  thanks for your help and another thing that i want to known textbox name which currently active. i could not get it name from MsgBox(Me.ActiveControl.Name) this command why ? thanks sathish

                  T Offline
                  T Offline
                  TwoFaced
                  wrote on last edited by
                  #9

                  Me.ActiveControl will give you a reference to the control that has focus. The problem I'm guessing is your code resides in a button click event. If you click the button it gets focus so Me.ActiveControl.Name will return the buttons name. What exactly are you trying to do? Can you describe the program a little?

                  B 1 Reply Last reply
                  0
                  • T TwoFaced

                    Me.ActiveControl will give you a reference to the control that has focus. The problem I'm guessing is your code resides in a button click event. If you click the button it gets focus so Me.ActiveControl.Name will return the buttons name. What exactly are you trying to do? Can you describe the program a little?

                    B Offline
                    B Offline
                    babusat
                    wrote on last edited by
                    #10

                    the problem is Me.ActiveControl.Name does not display any name that's only i asked why ? and i want to known which textbox is currently active thank you sathish

                    T 1 Reply Last reply
                    0
                    • B babusat

                      the problem is Me.ActiveControl.Name does not display any name that's only i asked why ? and i want to known which textbox is currently active thank you sathish

                      T Offline
                      T Offline
                      TwoFaced
                      wrote on last edited by
                      #11

                      I showed you code to dymanically create textboxs, is that how these textbox's were created or were they created at design time? Controls created at design time are given a default name, however, controls created at runtime are not. When you create the textbox you need to set it's name property. This is one reason why 'Name' may not be returning anything.

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups