VB.NET and Control Arrays

Posted on 2014-04-17
Last Modified: 2014-05-05
I realize that with VB.NET that control arrays are no longer used. However, I have an application written in VB version 6 that has 17 instances (0 to 16) that do use control arrays. One of the buttons in the control array has an effect on two text boxes and since I can use control arrays, the code for this is as follows:

Private Sub btnSetOut1_Click(Index As Integer)
    Dim i As Integer
    i = Index
    'validate manual setting
    If Not IsNumeric(txtOut1(i).Text) Then
        MsgBox "Invalid Entry: Only numeric values are allowed", vbOKOnly + vbExclamation, "Invalid Data Entry"
    ElseIf Val(txtOut1(i).Text) < 0 Or Val(txtOut1(i).Text) > 100 Then
        MsgBox "Invalid Entry: The output must be in the range of 0 and 100", vbOKOnly + vbExclamation, "Invalid Data Entry"
        If UCase(CtrlOutputType$(i%)) = "SINGLE" Then
            UT550OutputValWrite% CtrlChan(i), Val(txtOut1(i).Text)
        ElseIf UCase(CtrlOutputType$(i%)) = "DUAL" Then
            'if DUAL set txtOut2 to 0.0
            If Val(txtOut1(i).Text) > 0 Then
                txtOut2(i).Text = "0.0"
            End If
            UT550WriteHOutputValue% CtrlChan(i), Val(txtOut1(i).Text)
            UT550WriteCOutputValue% CtrlChan(i), Val(txtOut2(i).Text)
        End If
    End If
End Sub

Now, this is ALL the code I need to handle all 17 instances the 'btnSetOut1'. I believe that if I were to attempt to write this program in VB.NET, I would need to replicate the above code 16 more times!
That seems extremely tedious so I am wondering if someone can explain to me an easy way to do the same thing in VB.NET that I did in the VB6 code above.
I would think that the programming would be easier, not more difficult.

Thanks in advance,
Question by:charlieb01
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 40007731
I never worked with control arrays in VB6, but from what I read here: should simply need to use the standard VB.NET functionality of associating multiple controls to the same handler.


Private Sub btnSetOut1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btn1.Click, btn2.Click, btn3.Click
    Dim btn As Button = DirectCast(sender, Button)

    btn.Text = "Hello World!"

Open in new window

As you can see above, you simply add the list of applicable controls to the end of the method signature (including the event being associated). Inside your method, the sender parameter will always be the control (not necessarily a button) that raised the event. You can cast the sender to a Button type and then have access to all of the properties of the button that triggered the event.

The "e" parameter holds extra information pertaining to the event. There isn't really anything extra for button clicks (as I recall).

Author Comment

ID: 40007844
I believe I see your point and the point in the link you sent as well. However, this simply handles the button, my VB6 code uses the index to not only handle the button click based on the index but it also handles the values in two text boxes that have the same index.
So what I am looking for is how to do the same functionality in the Sub, including handling the textboxes.

LVL 75

Accepted Solution

käµfm³d   👽 earned 500 total points
ID: 40007868
Well the Tag property of the Control class is of type Object, so in reality you can stick anything you want in there. What if you created the button handler as I described above, and in your constructor (or Form_Load) you assign each associated TextBox to the relevant Button's Tag property:


Public Sub New()

    btn1.Tag = txtBox1
    btn2.Tag = txtBox2
    btn3.Tag = txtBox3
End Sub

Open in new window

Then in your handler it's simply a matter of:

Dim btn As Button = DirectCast(sender, Button)
Dim txt As TextBox = DirectCast(btn.Tag, TextBox)

Open in new window


You can still work with indexes if you really want. Controls can be placed in an array just like anything else. So you could maintain two arrays of controls--one for buttons and one for text boxes.


Dim btns(2) As Button
Dim txts(2) As TextBox

btns(0) = btn1
btns(1) = btn2
btns(2) = btn3

txts(0) = txt1
txts(1) = txt2
txts(2) = txt3

Open in new window

Then your index code would simply be:

Dim btn As Button = DirectCast(sender, Button)
Dim i As Integer = Array.IndexOf(btns, btn)
Dim txt As TextBox = txts(i)

Open in new window

LVL 40
ID: 40008704
You can mix different controls in the Handles. You can also mix different events.

This usually requires that the standard definition of the event has the same signature, that is that their second parameter is of the same type. By default, you cannot mix together a Button.MouseDown with a TextBox.Validating, because the mouse down receives a MouseEventArgs arguments, while the Validating receives CancelEventArgs.

However, since all events inherits from EventArgs, you can use polymorphism to mix different events that usually don't. If you define the event procedure as receiving an EventArgs as the second argument, you can set anything in the Handles clause and thus trap anything you want from the same procedure. The only problem for that is that since EventArgs does not provide any useful property, you lose the information that is often transmitted through more specific event argsuments, such as determining the button that was pressed on a MouseEventArgs. This can be solved through casting however.

Private Sub MixedEvent(sender As Object, e As EventArgs) Handles TextBox1.Validating, Button2.MouseDown

		If TypeOf e Is MouseEventArgs Then
			If DirectCast(e, MouseEventArgs).Button = Windows.Forms.MouseButtons.Middle Then
				MessageBox.Show("Middle button was pressed")
			End If
		End If

End Sub

Open in new window


Author Comment

ID: 40042254
Sorry for the delay responding. I was pulled away from this onto another urgent matter.

The TAG property appears to be the best solution to this problem.

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
There is an easy way, in .NET, to centralize the treatment of all unexpected errors. First of all, instead of launching the application directly in a Form, you need first to write a Sub called Main, in a module. Then, set the Startup Object to th…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

729 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question