1

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?

8
  • 2
    I have to tell you that using 100 checkboxes on a single form is going to be extremely busy and confusing to your user. Is there a different way you could break up the choices for them? Commented Mar 5, 2010 at 5:47
  • then what about the textBox array? Commented Mar 5, 2010 at 5:55
  • You can set up arrays of controls in VB.Net but they do not act like control arrays did in VB6. You would be responsible for setting them up in code including their text, location, etc. It may be easier for you to re-think your user interface than to try to get VB.Net to work like VB6. Commented Mar 5, 2010 at 5:59
  • how can I think to set text, location of near about 250 controls spanned across 4 tab-pages BY CODING!! Commented Mar 5, 2010 at 6:10
  • 1
    Well, the other way to do it is not to treat them as an array, but to, instead, point a common control event to a single method, then use their Name as a referent to do whatever it is you need to do. Commented Mar 5, 2010 at 6:13

3 Answers 3

4

Perhaps you should use a CheckedListBox instead

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

0

Mr. Tejas...

This was essentially answered in your question here:

About using arguments passed to the CheckedChanged event of Checkbox

4 Comments

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.
Mr. tobrien, pls recheck both these questions have no similarities other than they both relate to checkbox. In that I asked about arguments of checkedChanged event & even the word 'array' is not there!!
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
yes I would definitely like to have that mr. tobrien. pls provide it.... Thanks :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.