Solved

vb.net control array

Posted on 2006-07-05
1
216 Views
Last Modified: 2010-04-23
I am new to vb.net.

I had a vb6 app where there was a control array of text boxes and one of buttons.
When I typed something in the text box, and hit it's button something would happen.

For this exapmle we can assume that I am just trying to get a message box to show up with the text in the text box when I click on the button.

So clicking on button1 would have a message box with the text of text1.
This was easy because I could just use the index of the button click to figure out what text box to use.

How can I do this with .net without having a function for each button?

Or failing that, can you please explain what is going on?

Thanks
0
Comment
Question by:jackjohnson44
1 Comment
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 500 total points
ID: 17042233
You could use the Tag of the textbox to store either the Name or index of the associated TextBox. You then simply need to assign the same Click even handler to each of your buttons, and use the value from the Tag to find the associated TextBox:

    Private Sub Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click, Button5.Click, Button4.Click, Button3.Click, Button2.Click

        Dim b As Button = CType(sender, Button)
        Dim tag As String = b.Tag

        Dim t As TextBox = Me.Controls.Find("TextBox" & tag.ToString(), True)(0)
        MessageBox.Show(t.Text)

    End Sub

Notice that the above method is the handler for Button1 through Button5. Each button has a Tag (i.e. Button1 has a Tag with a value of "1"), which is then used to find the associated TextBox.

You didn't mention which version of VB.Net you are using, so the above sample is in VB 2005.
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

785 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