Results 1 to 6 of 6

Thread: [02/03] Accessing controls dynamically

  1. #1

    Thread Starter
    Fanatic Member pvbangera's Avatar
    Join Date
    Sep 2001
    Location
    Mumbai, India
    Posts
    961

    Arrow [02/03] Accessing controls dynamically

    Hi,

    Say I have 5 textboxes on my form (TextBox1, TextBox2, ...TextBox5).

    Now I want to access them using their names:

    Something like me.Controls("TextBox" & n.ToString).Text.

    Is there any way out?
    Microsoft Techie

  2. #2
    Hyperactive Member josep's Avatar
    Join Date
    Sep 2006
    Location
    Barcelona
    Posts
    409

    Re: [02/03] Accessing controls dynamically

    Check this code

    VB Code:
    1. Dim c As Control
    2.  
    3.         For Each c In Me.Controls
    4.             If TypeOf c Is TextBox AndAlso CType(c, TextBox).Name = "textbox1" Then
    5.                 'you find it just do whatever you want
    6.                 CType(c, TextBox).Text = "anything"
    7.             End If
    8.         Next

    Hope this helps
    Useful links:DB connection strings ADO.NET VB.NET Tutorials

    • Don't forget to close the thread if solved
    • If this post helps you rate it

  3. #3

    Thread Starter
    Fanatic Member pvbangera's Avatar
    Join Date
    Sep 2001
    Location
    Mumbai, India
    Posts
    961

    Re: [02/03] Accessing controls dynamically

    I need something like this
    VB Code:
    1. i = 0
    2. Do While rdr.Read
    3.   me.controls("txtMyTextBox" & i.ToString).Text = "Some text"
    4.   i += 1
    5. Loop

    If TypeOf c Is TextBox AndAlso CType(c, TextBox).Name = "textbox1" Then
    If I know the name of the textbox then I could have easily written it as me.TextBox1.
    Microsoft Techie

  4. #4
    Hyperactive Member josep's Avatar
    Join Date
    Sep 2006
    Location
    Barcelona
    Posts
    409

    Re: [02/03] Accessing controls dynamically

    You have the name property on the controls collection, but there is no way to call an object like you are want, just a loop on all controls checking the name of the desired control once you get, do the action.
    Useful links:DB connection strings ADO.NET VB.NET Tutorials

    • Don't forget to close the thread if solved
    • If this post helps you rate it

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [02/03] Accessing controls dynamically

    Argh! It hurts me physically every time I see this question. Put you controls in an array and then access them by index:
    VB Code:
    1. Private textBoxes As TextBox()
    2.  
    3. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    4.     Me.textBoxes = New TextBox() {Me.TextBox1, Me.TextBox2, Me.TextBox3, Me.TextBox4, Me.TextBox5}
    5. End Sub
    6.  
    7. Private Sub PopulateTextBoxes()
    8.     For i As Integer = 0 To Me.textBoxes.GetUpperBound(0)
    9.         Me.textBoxes(i).Text = "TextBox" & (i + 1)
    10.     Next i
    11. End Sub
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6

    Thread Starter
    Fanatic Member pvbangera's Avatar
    Join Date
    Sep 2001
    Location
    Mumbai, India
    Posts
    961

    Re: [02/03] Accessing controls dynamically

    This is really cool buddy. Sorry for hurting you. But I still miss VB 6 control arrays, wherein we easily assign index to them and use it accordingly.

    Anyways....thx a tonne.
    Microsoft Techie

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width