Disabling a range of textboxes

Blitzkrieg99
03-25-2004, 01:59 AM
Hello everyone, I use the following code to disable all textboxes:

For i=0 To (Me.Controls.Count) - 1
If TypeOf Me.Controls(i) Is TextBox Then
Me.Controls(i).Enabled = False
End If
Next

Suppose I have 40 textboxes. And I only want to disable, say, textboxes 15 to 32. How would I go about doing this? Thanks.

SK2
03-25-2004, 02:44 AM
How abt putting

For i = 14 to 31
if typeof.....
.....
.....
next

AtmaWeapon
03-25-2004, 06:48 AM
That only works if the textboxes are either the only controls on the form or the first 40. What I suggest is setting each TextBox's Tag member to a number that represents the TextBox, then in your code you would use the tag value to determine whether or not to disable it.

AFterlife
03-25-2004, 10:34 AM
That only works if the textboxes are either the only controls on the form or the first 40. What I suggest is setting each TextBox's Tag member to a number that represents the TextBox, then in your code you would use the tag value to determine whether or not to disable it.
Why not just use a for each.

dim ctl as control
for each ctl in me.controls
if typeof ctl is textbox then
textbox.enabled=false
end if
next

AtmaWeapon
03-25-2004, 11:27 AM
Why not just use a for each.

And I only want to disable, say, textboxes 15 to 32.

Logic error. Suggested solution does not match question.

Blitzkrieg99
03-25-2004, 05:12 PM
Thanks to all who replied. AtmaWeapon, please provide further details on how to go about the solution you suggested. Thanks.

Originally Posted by AtmaWeapon
What I suggest is setting each TextBox's Tag member to a number that represents the TextBox, then in your code you would use the tag value to determine whether or not to disable it.

Bucky
03-25-2004, 06:54 PM
Each TextBox (and indeed every control on your form) has a Tag property that can be
used for any general purpose (it doesn't affect the control's behavior).

At design-time, set the Tag properties of your textboxes to numbers, say, 0 to 39.


Then, at run-time...



Dim ctl As Control

For Each ctl In Me.Controls
Dim number As Integer = Convert.ToInt32(ctl.Tag) ' Convert the Object to an integer
If number >= 15 And number <= 32 Then
ctl.Enabled = False
End If
Next


And there you have it.

As an alternative solution, you might want to group your textboxes into
GroupBox or Panel control to make your form more organized aesthetically
(spelling?) and programmatically. If you put those 17 TextBoxes in their own
GroupBox or Panel, you could just loop through the parent control's
Controls collection, and forget about all the numbering. It looks nicer on the
form, too.

AFterlife
03-25-2004, 09:19 PM
Logic error. Suggested solution does not match question.
Oops, my bad didnt see the writing under his example.

Blitzkrieg99
03-28-2004, 05:12 PM
Thanks for that Bucky. :)

Mikecrosoft
03-29-2004, 09:38 AM
But If you can't group the Textboxes for unknown reasons, just use the AtmaWeapon solution:


Dim Ctrl As Control
For Each Ctrl In Me.Controls
If TypeOf Ctrl Is TextBox Then
Select Case Ctrl.Tag
Case "Group1"
'Do somthing here
Case "Group2"
'Do something different here too

End Select
End If
Next Ctrl

AtmaWeapon
03-29-2004, 10:02 AM
Not quite, I proposed a solution with no code, since it isn't Recycle Bin. Bucky posted the code that shows my solution.

Mikecrosoft
03-29-2004, 11:16 AM
Yeah, I know, I said the use the AtmaWeapon solution, POST #3, where you said Use the Textbox's Tag to determine which textbox disable or not. ;)

Blitzkrieg99
03-29-2004, 05:27 PM
so does that mean that multiple labels/controls can have the same tag?

Mikecrosoft
03-30-2004, 07:24 AM
Yep :)

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum