tags:

views:

220

answers:

3

Hello to all,

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?

+4  A: 

Perhaps you should use a CheckedListBox instead

John Knoeller
A: 

Mr. Tejas...

This was essentially answered in your question here:

http://stackoverflow.com/questions/2370923/about-using-arguments-passed-to-checkedchanged-event-of-checkbox/2371467#2371467

tobrien
And, BTW, it would be possible to create JUST ONE Checkbox in the designer, then create the remainder in CODE via a class wrapper which CAN BE treated using array tactics.
tobrien
Tejas
Yes, but the idea is the same ... in the other thread you wanted to trap the changed event for multiple Checkboxes and here you want to arrange your code to act on them... Since you already knew (it seems) that you CAN NOT have an actual ARRAY of CONTROLS, the idea of having ONE HANDLER (offered above) via the HANDLES phrase offers the same answer to new question which, in my opinion, has the SAME subtext.Just because you did not use the word ARRAY in the other question does not mean it isn't the same problem.I can offer an example of creating/managing CONTROLs in code if you wish
tobrien
yes I would definitely like to have that mr. tobrien.pls provide it....Thanks :-)
Tejas
+1  A: 

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
tobrien