• We just launched and are currently in beta. Join us as we build and grow the community.

How to get the Name Of Multiple TextBoxes Using For Each Loop

EnigmaticallySpeaking

System Reliability Specialist
E Rep
0
0
0
Rep
0
E Vouches
0
0
0
Vouches
0
Posts
138
Likes
200
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 200 XP
In this tutorial I will teach you how to get the name of a specific TextBoxes by using For Each Loop in VB.Net. This method will determine what are the names of your TextBoxes that you drag it on the Form. You will be able to find out the specific name of the TextBox because it will be seen in each TextBoxes.

So let’s begin:

Open Visual Studio and create a new Project. Drag all the TextBoxes and a Button. Then, do the Form just like this.

firstformnames.png


Double click the Button to fire the click

event handler in the Method.

Now, create a variable that represents the control.

  1. Dim

    txt As

    Control

After that, Loop the control that you have created.

  1. For

    Each

    txt In

    Me

    .

    Controls

  2. Next

Then, check if the type of the control is a Textbox and if it’s true, then perform the process that puts the name of the TextBox to itself. Do this code inside the Loop.


  1. If

    txt.

    GetType

    Is

    GetType

    (

    TextBox)

    Then
  2. txt.

    Text

    =

    txt.

    Name
  3. End

    If

And here are the codes that we made.

  1. Private

    Sub

    Button1_Click(

    ByVal

    sender As

    System.

    Object

    , ByVal

    e As

    System.

    EventArgs

    )

    Handles

    Button1.

    Click
  2. Dim

    txt As

    Control
  3. For

    Each

    txt In

    Me

    .

    Controls
  4. If

    txt.

    GetType

    Is

    GetType

    (

    TextBox)

    Then
  5. txt.

    Text

    =

    txt.

    Name
  6. End

    If
  7. Next
  8. End

    Sub

Press F5 to run your project and click the Button to fire the code in the method.

 

452,292

323,526

323,535

Top