Solved

vb.net control array

Posted on 2006-07-05
1
217 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VB: Convert 2 dates to specific format 24 52
VB.NET 2008 Publish Error 2 33
VB.NET String Settings and Temp Folder Question 3 53
Convert Ctime to date time in textfile? 7 30
I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
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…

840 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