Solved

vb.net control array

Posted on 2006-07-05
1
218 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

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 was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

733 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