Solved

vb.net control array

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

695 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