Solved

VB.NET and Control Arrays

Posted on 2014-04-17
5
484 Views
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"
        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
0
Comment
Question by:charlieb01
  • 2
  • 2
5 Comments
 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 40007731
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).
0
 

Author Comment

by:charlieb01
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.

Thanks,
Charlie
0
 
LVL 74

Accepted Solution

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

e.g.

Public Sub New()
    InitializeComponent()

    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.

e.g.

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

0
 
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

0
 

Author Comment

by:charlieb01
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.
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

759 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now