Retrieve Textbox by its name ?

  • Thread starter Thread starter Te-Deum
  • Start date Start date
T

Te-Deum

Hi,

I have an application with a lot of Textbox.
Do you know if I can set any Textbox Text only with the string name of the
Textbox.

Example : For the Textbox REXENP_CATEGW could I set the Text with its string
name "REXENP_CATEGW"
So I don't use 'REXENP_CATEGW.Text' but GetItem("REXENP_CATEGW").Text

Thank's.
 
* "Te-Deum said:
I have an application with a lot of Textbox.
Do you know if I can set any Textbox Text only with the string name of the
Textbox.

Example : For the Textbox REXENP_CATEGW could I set the Text with its string
name "REXENP_CATEGW"
So I don't use 'REXENP_CATEGW.Text' but GetItem("REXENP_CATEGW").Text

Add all of the controls you want to access by name to a 'Hashtable', use
their names as keys and a reference to the control as value.

--
Herfried K. Wagner
MVP · VB Classic, VB.NET
<http://www.mvps.org/dotnet>

Dilbert's words of wisdom #18: Never argue with an idiot. They drag you down
to their level then beat you with experience.
 
Here is an example :
I want to replace this code
Textbox1.Text = "....."
Textbox2.Text = "....."
Textbox3.Text = "....."
Textbox4.Text = "....."

By a code like this one :
Dim i as integer
For i = 1 to 4
GetItem("Textbox" & i.ToString()) = "...."
Next
Does the function "GetItem" exist ???

Thank's.
 
Te-Deum said:
Here is an example :
I want to replace this code
Textbox1.Text = "....."
Textbox2.Text = "....."
Textbox3.Text = "....."
Textbox4.Text = "....."

By a code like this one :
Dim i as integer
For i = 1 to 4
GetItem("Textbox" & i.ToString()) = "...."
Next
Does the function "GetItem" exist ???

Thank's.

Why not put the textboxes in an array? Variable names are out of interest at
runtime (IMHO).

private m_Textboxes as textbox()
'...
In OnLoad/Form_Load:
m_textboxes = new textbox(){textbox1, textbox2, textbox3, textbox4}

You can also use expressive names for the textboxes.

Loop:
for i=0 to 3
m_textboxes(i) = "...."
next i
 
* "Te-Deum said:
Here is an example :
I want to replace this code
Textbox1.Text = "....."
Textbox2.Text = "....."
Textbox3.Text = "....."
Textbox4.Text = "....."

By a code like this one :
Dim i as integer
For i = 1 to 4
GetItem("Textbox" & i.ToString()) = "...."
Next
Does the function "GetItem" exist ???

Have a look at Armin's suggestion.

'GetItem' doesn't exist, but:

<http://groups.google.de/groups?selm=#[email protected]>

--
Herfried K. Wagner
MVP · VB Classic, VB.NET
<http://www.mvps.org/dotnet>

Dilbert's words of wisdom #18: Never argue with an idiot. They drag you down
to their level then beat you with experience.
 
Back
Top