Improve company productivity with a Business Account.Sign Up


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: 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

What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Simulator games are perfect for generating sample realistic data streams, especially for learning data analysis. It is even useful for demoing offerings such as Azure stream analytics, PowerBI etc.
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

602 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