Link to home
Start Free TrialLog in
Avatar of charlieb01
charlieb01

asked on

VB.NET and Control Arrays

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"
        txtOut1(i).SetFocus
    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"
        txtOut1(i).SetFocus
    Else
        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,
Charlie
Avatar of kaufmed
kaufmed
Flag of United States of America image

I never worked with control arrays in VB6, but from what I read here:

http://msdn.microsoft.com/en-us/library/kxt4418a(v=vs.90).aspx

...you should simply need to use the standard VB.NET functionality of associating multiple controls to the same handler.

e.g.

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).
Avatar of charlieb01
charlieb01

ASKER

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.

Thanks,
Charlie
ASKER CERTIFIED SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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

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.