TechGeek
TechGeek

Reputation: 2212

CheckBox Array in VB.net

I need to have plenty of checkBoxes & textBoxes in my vb.net form.

So I really need CheckBox array to reduce coding of their events.

But I am not able to create array of them like in VB-6

Also I need to make them at design time only. So in coding writing like

dim chkBox as new CheckBox()

And then setting location & text of each of them is not at all feasible for me bcoz I need to have near about 100 of them in my form.

So pls help me in CREATING AN ARRAY OF THEM?

Upvotes: 1

Views: 3567

Answers (3)

tobrien
tobrien

Reputation: 618

Mr. Tejas...

Hopefully this code can be useful to you...

Assume that "OneTextObject" (a textbox) has been created in the Designer

Dim aTextObject() As TextBox
Dim theObjectCount% = 0

in the Form Load:

ReDim aTextObject(0)
aTextObject(0) = Me.OneTextObject

Then when you want to add to this "array" ....

theObjectCount += 1
    ReDim Preserve aTextObject(theObjectCount)
    aTextObject(theObjectCount) = New TextBox
    Me.Controls.Add(aTextObject(theObjectCount))
    AddHandler aTextObject(theObjectCount).DoubleClick, AddressOf aTextObject_Click
    AddHandler aTextObject(theObjectCount).MouseMove, AddressOf aTextObject_MouseMove
    AddHandler aTextObject(theObjectCount).MouseDown, AddressOf aTextObject_MouseDown
    aTextObject(theObjectCount).ContextMenu = New ContextMenu
aTextObject(theObjectCount).Location = New System.Drawing.Point(some_x, some_y)
    aTextObject(theObjectCount).Tag = "You can use this TAG to identify this TextBox vs all the others...  |  Item#1"  ' note the PIPE "|" symbol ... it can be utilized later.

    aTextObject(theObjectCount).Text = "Whatever"
    aTextObject(theObjectCount).Visible = True
    aTextObject(theObjectCount).BringToFront()
    aTextObject(theObjectCount).TextAlign =  HorizontalAlignment.Left

aTextObject(theObjectCount).Width = some_width
    aTextObject(theObjectCount).Height = some_height
    aTextObject(theObjectCount).Refresh()

Then here are some examples of callbacks for the textBoxes created.... note that there is no HANDLES phrase(s) !!!!!

 Public Sub aTextObject_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
    Dim theParse() As String

    theParse = sender.tag.Split("|") 

Select Case theParse(1) 'which ITEM#

    Case "Item#1"

    Case Else

End Select

End Sub

'This example uses the CType(Sender) mechanism...

Public Sub aTextObject_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs)

    Dim ltextbox As TextBox

    ltextbox = CType(sender, TextBox)

'do something with ltextbox...

Dim theParse() As String

    theParse = ltextbox.tag.Split("|") 

Select Case theParse(1) 'which ITEM#

    Case "Item#1"

    Case Else

End Select

End Sub



Public Sub aTextObject_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs)

    Dim ltextbox As TextBox

    ltextbox = CType(sender, TextBox)

'do something with ltextbox...

Dim theParse() As String

    theParse = ltextbox.tag.Split("|") 

Select Case theParse(1) 'which ITEM#

    Case "Item#1"

    Case Else

End Select



End Sub

Upvotes: 1

tobrien
tobrien

Reputation: 618

Mr. Tejas...

This was essentially answered in your question here:

About using arguments passed to the CheckedChanged event of Checkbox

Upvotes: 0

John Knoeller
John Knoeller

Reputation: 34128

Perhaps you should use a CheckedListBox instead

Upvotes: 4

Related Questions