Welcome to Dream.In.Code
Getting VB Help is Easy!

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!



accessing multiple labels and text boxes

3 Pages V  1 2 3 >  
Reply to this topicStart new topic

accessing multiple labels and text boxes

shavian
post 8 Aug, 2008 - 12:32 PM
Post #1


New D.I.C Head

*
Joined: 8 Aug, 2008
Posts: 15

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
User is offlineProfile CardPM

Go to the top of the page


jakerman999
post 8 Aug, 2008 - 08:54 PM
Post #2


New D.I.C Head

*
Joined: 7 Aug, 2008
Posts: 11



Thanked 1 times
My Contributions


QUOTE(shavian @ 8 Aug, 2008 - 12:32 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.

This post has been edited by jakerman999: 8 Aug, 2008 - 08:55 PM
User is offlineProfile CardPM

Go to the top of the page

shavian
post 9 Aug, 2008 - 04:24 AM
Post #3


New D.I.C Head

*
Joined: 8 Aug, 2008
Posts: 15

QUOTE(jakerman999 @ 8 Aug, 2008 - 08:54 PM) *

QUOTE(shavian @ 8 Aug, 2008 - 12:32 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]
User is offlineProfile CardPM

Go to the top of the page

AdamSpeight2008
post 9 Aug, 2008 - 06:45 AM
Post #4


DICula

Group Icon
Joined: 29 May, 2008
Posts: 614



Thanked 38 times

Dream Kudos: 2000
My Contributions


QUOTE(shavian @ 9 Aug, 2008 - 12:24 PM) *

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
User is offlineProfile CardPM

Go to the top of the page

AdamSpeight2008
post 9 Aug, 2008 - 09:27 AM
Post #5


DICula

Group Icon
Joined: 29 May, 2008
Posts: 614



Thanked 38 times

Dream Kudos: 2000
My Contributions


And if it VB6
vb

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

Examples of usage
CODE

Call SetEnableState_Range(Form1, "Lbl", 40, 50, False)
Call SetEnableState_Range(Form1, "Text", 40, 50, False)
Call SetEnableState_Range(Form1, "Lbl", 40, 50, True)
Call SetEnableState_Range(Form1, "Text", 40, 50, True)


This post has been edited by AdamSpeight2008: 9 Aug, 2008 - 09:29 AM
User is offlineProfile CardPM

Go to the top of the page

shavian
post 9 Aug, 2008 - 09:58 AM
Post #6


New D.I.C Head

*
Joined: 8 Aug, 2008
Posts: 15

QUOTE

Almost there but not quite!

I am getting this runtime error

Object reference not set to an instance of an object.

my code is as follows where I want to disable the range 46 - 55

CODE

FirstForm.intMaxOvers = 45
            SetEnableState_Range(Me, "labOver", FirstForm.intMaxOvers + 1, 55, False)  
            SetEnableState_Range(Me, "txtBowler", FirstForm.intMaxOvers + 1, 55, False)  

End If

User is offlineProfile CardPM

Go to the top of the page

AdamSpeight2008
post 9 Aug, 2008 - 10:00 AM
Post #7


DICula

Group Icon
Joined: 29 May, 2008
Posts: 614



Thanked 38 times

Dream Kudos: 2000
My Contributions


Try
CODE

FirstForm.intMaxOvers = 45
            SetEnableState_Range(FirstForm, "labOver", FirstForm.intMaxOvers + 1, 55, False)  
            SetEnableState_Range(FirstForm, "txtBowler", FirstForm.intMaxOvers + 1, 55, False)  

End If
User is offlineProfile CardPM

Go to the top of the page

AdamSpeight2008
post 9 Aug, 2008 - 10:06 AM
Post #8


DICula

Group Icon
Joined: 29 May, 2008
Posts: 614



Thanked 38 times

Dream Kudos: 2000
My Contributions


Post the error message.
I can't pm you.
User is offlineProfile CardPM

Go to the top of the page

shavian
post 9 Aug, 2008 - 10:11 AM
Post #9


New D.I.C Head

*
Joined: 8 Aug, 2008
Posts: 15

Still no good!

the labels and textboxes to be disabled are on the Form called Scorecard.

I have put the subroutine in the Class and the code in Scorecard_load.

Thanks for the swift response
User is offlineProfile CardPM

Go to the top of the page

AdamSpeight2008
post 9 Aug, 2008 - 10:13 AM
Post #10


DICula

Group Icon
Joined: 29 May, 2008
Posts: 614



Thanked 38 times

Dream Kudos: 2000
My Contributions


simply
vb

FirstForm.intMaxOvers = 45
SetEnableState_Range(ScoreCard, "labOver", FirstForm.intMaxOvers + 1, 55, False)
SetEnableState_Range(ScoreCard, "txtBowler", FirstForm.intMaxOvers + 1, 55, False)

End If

Else change it from Private to Public

This post has been edited by born2c0de: 13 Aug, 2008 - 05:05 AM
User is offlineProfile CardPM

Go to the top of the page

shavian
post 9 Aug, 2008 - 10:15 AM
Post #11


New D.I.C Head

*
Joined: 8 Aug, 2008
Posts: 15

QUOTE

the error message is as follows


vb

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)
User is offlineProfile CardPM

Go to the top of the page

AdamSpeight2008
post 9 Aug, 2008 - 10:22 AM
Post #12


DICula

Group Icon
Joined: 29 May, 2008
Posts: 614



Thanked 38 times

Dream Kudos: 2000
My Contributions


Are you sure this vb6?
User is offlineProfile CardPM

Go to the top of the page

3 Pages V  1 2 3 >
Fast ReplyReply to this topicStart new topic
Time is now: 10/11/08 07:44PM

Live VB Help!

VB Tutorials

Reference Sheets

VB Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month