VB.NET and Control Arrays

Posted on 2014-04-17
Medium Priority
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
  • 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:


...you 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 2000 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
This article will show, step by step, how to integrate R code into a R Sweave document
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses

571 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