Solved

vb.net control array

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
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 …
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

708 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

14 Experts available now in Live!

Get 1:1 Help Now