How to access a TextBox control if given its Name as a variable?
-
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!
Hello, To get the text from a textbox and set it to your string variable, you could use:
str = TextBox.Text
OrDim 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 -
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!
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.