Creating arrays of controls in VB 2005. Help!

It seems that the easy and useful capability iof creating arrays of controls which can be indexed is not easy to do in the .NET environment.

I ported a small application that had several arrays of textboxes and comboboxes, and checkboxes. The port worked well and did create arrays of  all except the checkboxes.

Now I am creating a new application and I want a number of columns of checkboxes. I'd like them to be arrayed so I can use the same index for the control as the data array in the program. Easy right. NO.

How do I create arrays of indexed textboxes


Answer this question

Creating arrays of controls in VB 2005. Help!

  • Sudheer Palyam

  • ClaudiaM

    <quote>Sheesh. You'd think I would be able to insert a panel or a groupbox and then insert a textbox and copy/paste a bunch of them in to create an aray of them. But noooooooo, it can't be that easy. </quote>

    Actually you can do exactly that...

    Place a groupbox/panel/container control on your form drop your textboxes on the container and then you can iterate through the controls collection of the container!  You may not have that index so sweetly defined though!



  • Marian Negru

    Well, it may not be elegant, but let me try.

  • Aaron Tan

    Have you tried using the tag property in lieu of the index property from VB6

    Then you could modify the event handlers to handle all of your particular controls.

    Here is something I've done before with text boxes

    Assign numbers to thier tag properties, perhaps to coincide with the array positions if known, but since you want to set other properties at design time perhaps you know.

    Create an enum to give them friendly names (but not necessary)

    private enum myfield
        FirstName = 5
        LastName = 10
        MiddleInit = 15
    end enum

    Set one of the events to handle all the events for the controls you want

    Private Sub textbox_validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles _
        textbox1.Validating,  textbox2.Validating, textbox3.Validating

            Dim txtField As TextBox

            If TypeOf sender Is TextBox Then
                txtField = CType(sender, TextBox)
           
                Select Case txtField.Tag

                    Case myField.FirstName
                         'Do whatever with the textbox having a tag property of 5

                End select
            End If

    End sub


  • Harish Krishnan

    Sometimes those VB6 features are truely missed (control arrays ahhhh)..

    you do have several other options though...

    MyTextboxArray() as System.Windows.Forms.Textbox

    or

    Public Class TextBoxCollection

    Inherits System.Collections.CollectionBase

    Public Sub Add(ByVal TXB As TextBox)

    ' Invokes Add method of the List object to add a TextBox
    List.Add(TXB)

    End Sub

    'youll have to override a couple of other methods...
    End
    Class

    Just as an example of a couple of your options...



  • Mike.PD

    Is there a graphical way to implement this in the design page

    It looks like you present a method of creating the textboxes via code. Then I have to place them and size them. I'd like to design the form.

    I noticed on the ported code that my textboxes were in the form _txtModulePin_0, _txtModulePin_1, etc where they have a property "Index on txtModulePin". This is the sweet solution  I'd like.

    Sheesh. You'd think I would be able to insert a panel or a groupbox and then insert a textbox and copy/paste a bunch of them in to create an aray of them. But noooooooo, it can't be that easy.

  • Creating arrays of controls in VB 2005. Help!