Join 118,589 VB Programmers for FREE! Ask your question and get quick answers from experts. There are 825 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!
In Visual Studio, I have a form with 50 labels and 50 text boxes with names lab1, lab2 .... and txt1, txt2 ..... I have to disable a range of them and the range can vary.
How can I disable, say lab41 - lab50 and txt41 - txt50 in a For loop?
In Visual Studio, I have a form with 50 labels and 50 text boxes with names lab1, lab2 .... and txt1, txt2 ..... I have to disable a range of them and the range can vary.
How can I disable, say lab41 - lab50 and txt41 - txt50 in a For loop?
Thanks
instead of naming them with numbers, name them the same and set their index property. then try a loop like this(note:I use lbl instead of lab).
CODE
Dim intCounter As Integer
For intCounter = 40 To 49 lbl(intCounter).Enabled = False txt(intCounter).Enabled = False Next
there I have 40 to 49 because the index starts at 0 instead of 1. let us know if that doesn't work.
This post has been edited by jakerman999: 8 Aug, 2008 - 08:55 PM
In Visual Studio, I have a form with 50 labels and 50 text boxes with names lab1, lab2 .... and txt1, txt2 ..... I have to disable a range of them and the range can vary.
How can I disable, say lab41 - lab50 and txt41 - txt50 in a For loop?
Thanks
instead of naming them with numbers, name them the same and set their index property. then try a loop like this(note:I use lbl instead of lab).
CODE
Dim intCounter As Integer
For intCounter = 40 To 49 lbl(intCounter).Enabled = False txt(intCounter).Enabled = False Next
there I have 40 to 49 because the index starts at 0 instead of 1. let us know if that doesn't work.
[/quote] This seems like a good plan but my labels do not have an index property! [/quote]
This seems like a good plan but my labels do not have an index property!
Sounds like you're not using vb6, but a vb.net version.
If this is so then use the following subroutine.
vb
Private Sub SetEnableState_Range(ByRef onForm As Form, ByVal baseName As String, ByVal from_v As Integer, ByVal to_v As Integer, ByRef state As Boolean) While from_v <= to_v Try onForm.Controls(baseName & from_v.ToString).Enabled = state Catch Exit Try End Try from_v += 1 End While End Sub
Examples of Useage
vb
SetEnableState_Range(Form1,"lab",41,50,False)' Disable lab40 to lab50 on Form1 SetEnableState_Range(Form1,"txt",41,50,False)' Disable txt40 to lab50 on Form1
SetEnableState_Range(Form1,"lab",41,50,True)' Enables lab40 to lab50 on Form1 SetEnableState_Range(Form1,"txt",41,50,Ture)' Enables txt40 to lab50 on Form1
Private Sub Private Sub SetEnableState_Range(ByRef tf As Form, ByVal base As String, ByVal fv As Integer, ByVal tv As Integer, ByVal ts As Boolean) Dim i As Integer
For i = 0 To tf.Controls.Count - 1 If Mid$(tf.Controls(i).Name, 1, Len(base)) = base Then If Int(Mid$(tf.Controls(i).Name, 4)) >= fv And Int(Mid$(tf.Controls(i).Name, Len(base) + 1)) <= tv Then Form1.Controls(i).Enabled = ts End If End If Next i End Sub
System.NullReferenceException was caught Message="Object reference not set to an instance of an object." Source="WindowsApplication1" StackTrace: at WindowsApplication1.Scorecard.SetEnableState_Range(Form& onForm, String baseName, Int32 from_v, Int32 to_v, Boolean& state)