Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to convert a string to a control name

Status
Not open for further replies.

Bigsin

Programmer
Jan 17, 2009
82
NL
What I want is to convert a string variable to a control name.

I have created a textbox named "textbox1"


Dim a As String = "textbox"
Dim b As Integer = 1
Dim c As String = a & b.ToString 'so c = "textbox1"

c.Text = "succeeded!!!"

This code is not working.

What is the solution for my wishes?


 
Try accessing the textbox like this

CType(Me.Controls(c), TextBox).Text = "Succeeded!!
 
Code:
        Dim c As New TextBox
        Dim a As String = "TextBox"
        Dim b As Integer = 1
        c.Name = a & b.ToString    'so c = "textbox1"
        Me.Controls.Add(c)
        c.Text = "succeeded!!!"

Zameer Abdulla
 
Zameer,

This will add a new control to the form not add the text to the existing control.
 
c is a string variable and doesn't have a property of text.
If you want to change the name of an existing control, maybe you can use the name property of the control. I've never done this so don't know if woll work.

Textbox.Name = c
 
The answer of Kliot solved mij issue.

Thanks to you all!
 
Kilot, Please see the difference of code. You must set the name of control to set a value. In the first part you don't need to change anything because the control name is same what you assign. If you change the integer value to something then you must assign the name to control to access it's properties.
Code:
        Dim a As String = "textbox"
        Dim b As Integer = 1
        Dim c As String = a & b.ToString    'so c = "textbox1"
       
        CType(Me.Controls(c), TextBox).Text = "Succeeded!!"

Code:
        Dim a As String = "textbox"
        Dim b As Integer = 2
        Dim c As String = a & b.ToString    'so c = "textbox1"
        Me.TextBox1.Name = c
        CType(Me.Controls(c), TextBox).Text = "Succeeded!!"

This is why I created control on the fly.

Zameer Abdulla
 
I mean in the second snippet
Code:
'so c = "textbox2"


Code:
        Dim a As String = "textbox"
        Dim b As Integer = 2
        Dim c As String = a & b.ToString    'so c = "textbox2"
        Me.TextBox1.Name = c
        CType(Me.Controls(c), TextBox).Text = "Succeeded!!"

Zameer Abdulla
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top