Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. How to access a TextBox control if given its Name as a variable?

How to access a TextBox control if given its Name as a variable?

Scheduled Pinned Locked Moved Visual Basic
tutorialquestion
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Lost User
    wrote on last edited by
    #1

    For example, a textbox 'texbbox1' has been placed in a form. in the following code: Dim str as String str = "textbox1" .... how to access textbox1 through variable "str" ? Thanks a lot!

    M L 2 Replies Last reply
    0
    • L Lost User

      For example, a textbox 'texbbox1' has been placed in a form. in the following code: Dim str as String str = "textbox1" .... how to access textbox1 through variable "str" ? Thanks a lot!

      M Offline
      M Offline
      Mitch F
      wrote on last edited by
      #2

      Hello, To get the text from a textbox and set it to your string variable, you could use: str = TextBox.Text Or Dim str as String = TextBox.Text To set a TextBox's text from a variable, you would use: TextBox.Text = str I hope this helps, Mitch My sig: "And it is a professional faux pas to pay someone else to destroy your computer when you are perfectly capable of destroying it yourself." - Roger Wright Get Perpendicular! (Hitachi Storage) My CodeProject Blog Most recent blog post: April 11

      1 Reply Last reply
      0
      • L Lost User

        For example, a textbox 'texbbox1' has been placed in a form. in the following code: Dim str as String str = "textbox1" .... how to access textbox1 through variable "str" ? Thanks a lot!

        L Offline
        L Offline
        lespaul36
        wrote on last edited by
        #3

        Was the textbox put on the form at runtime or designtime. if it was as designtime then use the control name and not the variable to access it. Otherwise, I would put a form level variable of type textbox to hold it. If there are going to be alot of runtime controls added, make an arraylist and add them to that. However, the only way I know of to get the control in this case is: Dim tb as TextBox For Each tb in me.Controls If tb.Name=str Then exit For End If Next tb should be the TextBox you want.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups