Visual Basic Development Bookmark and Share   
 Home > Visual Basic IDE > How to reference textboxes with a string?
 

How to reference textboxes with a string?

To simplify my problem, lets say I have 200 textboxes on a form. I want the first 100 to become disabled in a certain subroutine. I don't want to write 100 lines to disable each textbox; I'd rather write a loop to do it but can't figure out how. For instance:

dim i as integer

dim s as string

for i = 1 to 100

s = "Textbox"&(i)

<What could I put here to make s.enabled=false?>

next

I tried "dim c as new control" then "c.name=s" but I don't think this makes "c" as a member of my form and it didn't do anything as a result. I think I need to figure out how to declare c as a control that exists within my form but can't figure out the syntax for that. Any help is appreciated.

Pratt  Wednesday, April 11, 2007 2:02 PM

Ignoring the fact that having 200 textboxes on the same form may have some performance and usability issues (I don't see myself typing anything into 200 textboxes - I usually get bored after having had to fill out 50 or so )

This line:

Code Snippet

For each tb as TextBox in Me.Controls

assumes that all the controls in the Controls collection are text boxes...

Try something like this:

Code Snippet

For each ctrl as Control in Me.Controls ' No assumption as to what type of control this is...

Dim tb As TextBox = TryCast(ctrl, TextBox) ' Try cast will either give you a text box, or Nothing if the control is not a TextBox

If tb IsNot Nothing Then

tb.enabled = False

End If

Next

Best regards,

Johan Stenberg

MSFT Johan Stenberg  Wednesday, April 11, 2007 4:22 PM

try this

Public Function gen_Enable_AllTextBox(ByValargEnable AsBoolean,ByVal TheForm As Form)

Dim txt As Control

For Each txt In TheForm.Controls

If TypeOf txt Is TextBox Then

txt.Enabled = argEnable

End If

Next

End Function

if u have all text box in user control the this code is ok.

suppose u a trying some other control like tab control then u have to change that control type there.

saAction  Thursday, April 12, 2007 5:03 AM

Hi Pratt

I suppose by now you already have reached a solution. Wish I got into this thread earlier.

Constructing your own custom control, would make this problem easy to solve.

How about this concept.

1) Use graphics drawing(GDI) to draw what looks like a whole lot of textboxes on the form

2)Use just one textbox (which you can make appear and then disappear anywhere on the form -as your input tool)

3)Use a string array, to hold all your inputs, and use textual graphics to diplay all your inputs on the form in their respective positions

4) Use mousemove to log your mouse position, so when you have a click event you can write code to detirmine which "textbox" to activate

Someone said somewhere in these forums (I can't remember who) and I paraphrase: -

"constructing your own custom controls - is not as daunting as you might first think - - -"

Ifyou have never used GDI before, then you will have a little learning curve to achieve first - before you can easily draw and put text on a form. (or into an array of bitmats on the form - probably a better idea)

Edit: GDI =Graphics Device Interface

LeonCS  Wednesday, May 02, 2007 10:06 AM

If you have a standard naming convention (i.e. TextBox1, TextBox2, ..., TextBox120, ....) Then do

Dim tbname as String = ""

For I as Integer = 1 To 100

tbname = "TextBox" & I.ToString

For each tb as TextBox in Me.Controls

If tb.name = tbname Then

tb.enabled = False

End If

Next

Next

Swade  Wednesday, April 11, 2007 2:47 PM

I tried that and I got the following error:

"Unable to cast object of type 'System.Windows.Forms.TabControl' to type 'System.Windows.Forms.TextBox'."

I do have a tab control but I don't know why it's creating that error with that code.

Pratt  Wednesday, April 11, 2007 4:03 PM

Ignoring the fact that having 200 textboxes on the same form may have some performance and usability issues (I don't see myself typing anything into 200 textboxes - I usually get bored after having had to fill out 50 or so )

This line:

Code Snippet

For each tb as TextBox in Me.Controls

assumes that all the controls in the Controls collection are text boxes...

Try something like this:

Code Snippet

For each ctrl as Control in Me.Controls ' No assumption as to what type of control this is...

Dim tb As TextBox = TryCast(ctrl, TextBox) ' Try cast will either give you a text box, or Nothing if the control is not a TextBox

If tb IsNot Nothing Then

tb.enabled = False

End If

Next

Best regards,

Johan Stenberg

MSFT Johan Stenberg  Wednesday, April 11, 2007 4:22 PM

Excellent, many thanks.

There are lots of textboxes and numeric boxes as it's a rather large engineering program with many inputs on many tabs and I'm not a programmer (obviously) so I don't know the best way to handle it. If I could use a premade excel-style spreadsheet instead of a bunch of textboxeswith a way to reference each "cell" I'd probably use that instead for many of the thermodynamic inputs that now reside in text boxes. I'm still having way more fun with this than fortran.

Pratt  Wednesday, April 11, 2007 4:52 PM

fyi I had to use this line belowinstead to get it to work. I guess form1 only includes the tab controls, which only includes the tabpages.

For Each ctrl As Control In Me.TabControl1.TabPages(0).Controls

Pratt  Wednesday, April 11, 2007 6:33 PM

Check out the DataGridViewcontrol.. It may be a good match for what you are looking for...

Best regards,

Johan Stenberg

MSFT Johan Stenberg  Thursday, April 12, 2007 12:32 AM

try this

Public Function gen_Enable_AllTextBox(ByValargEnable AsBoolean,ByVal TheForm As Form)

Dim txt As Control

For Each txt In TheForm.Controls

If TypeOf txt Is TextBox Then

txt.Enabled = argEnable

End If

Next

End Function

if u have all text box in user control the this code is ok.

suppose u a trying some other control like tab control then u have to change that control type there.

saAction  Thursday, April 12, 2007 5:03 AM
All container controls have their own controls collection. And so controls placed on a tab or a frame wont be avauilable in Forms.Controls
Merin Gazell  Wednesday, May 02, 2007 4:19 AM

Hi Pratt

I suppose by now you already have reached a solution. Wish I got into this thread earlier.

Constructing your own custom control, would make this problem easy to solve.

How about this concept.

1) Use graphics drawing(GDI) to draw what looks like a whole lot of textboxes on the form

2)Use just one textbox (which you can make appear and then disappear anywhere on the form -as your input tool)

3)Use a string array, to hold all your inputs, and use textual graphics to diplay all your inputs on the form in their respective positions

4) Use mousemove to log your mouse position, so when you have a click event you can write code to detirmine which "textbox" to activate

Someone said somewhere in these forums (I can't remember who) and I paraphrase: -

"constructing your own custom controls - is not as daunting as you might first think - - -"

Ifyou have never used GDI before, then you will have a little learning curve to achieve first - before you can easily draw and put text on a form. (or into an array of bitmats on the form - probably a better idea)

Edit: GDI =Graphics Device Interface

LeonCS  Wednesday, May 02, 2007 10:06 AM
I like that idea.I will try it and see if it reduces the small amount of lag that users get when the tab index is changed, while the textboxes redraw.
Pratt  Wednesday, May 02, 2007 8:54 PM
I once had a similar problem, with 1600 picture boxes!
For i As Integer = 1 To 100
    Me.Controls("TextBox" & i).Enabled = False
Next
jammyatjammy  Thursday, November 12, 2009 10:59 PM

You can use google to search for other answers

Custom Search

More Threads

• error on build MSB3299
• How to prevent the IDE from executing some code in New() in inherited form?
• Test Framework can't find new class
• Lost the Graphics.
• properties for references not displaying
• How to Add ListView to a user form in VBA
• VB6 and VS 2008 installation
• VS05 Coversion did not split forms into 2 files
• VB Bug?
• IDE Slow, movement of solution explorer window and others