Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

loop through certain label boxes

Status
Not open for further replies.

flybravo

Programmer
Jun 30, 2003
35
US
I have baout 25 label boxes on this form. I need to be able to only loop through 10 label boxes and edit the text value. Each of the 10 boxes have the first part of the name same.

This is what i have tried

for i = 1 to 10
me.controls("lblPropertValue" & i).text = 5
Next
 
The way I would do it is to make a control array ..
Dim myLabels(9) As Label ... you can make this global by putting in a mod file or make it for the form, by putting it at the top of the form (You may know this already, but I am playing it safe)

In the form_load event

myLabels(0)=lblPropertValue1
myLabels(1)=lblPropertValue2
.
.
myLabels(9)=lblPropertValue10

now you can do this ...

For i = 0 to 9
myLables(i).text = 5
Next

This is the quick and dirty way, there are other ways, but this works best for me in most cases.

Becca






Somtimes, the easy answer is the hardest to find. :)
 
I typed in the code and when I open the form i recieve this message

An unhandled exception of type 'System.NullReferenceException' occurred in Grades.exe

Additional information: Object reference not set to an instance of an object.
 
did you remember the dim statement? place it just under the "windows generated code" section, and before any other code.

Becca

Somtimes, the easy answer is the hardest to find. :)
 
I changed the location and still get the same message. Here is my Code

<CODE>
Dim myLabels() As Label ' below the windows region box

Private Sub frmOne_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

myLabels(0) = lblValueProperty1
myLabels(1) = lblValueProperty2
myLabels(2) = lblValueProperty3
myLabels(3) = lblValueProperty4
myLabels(4) = lblValueProperty5
myLabels(5) = lblValueProperty6
End Sub
 
That should work, let me double check something.

becca

Somtimes, the easy answer is the hardest to find. :)
 
next question, are the labels, on the same form as the code for them?

Somtimes, the easy answer is the hardest to find. :)
 
ok, here's the answer to the problem, as I see it now ...

The Dim statement for the myLabels() array must have a number in it 'Dim myLabels(9) as Label' before you load the array.

Becca

Somtimes, the easy answer is the hardest to find. :)
 
It seems to me that you want to set 10 certain labels to a value, which will have a 1 through 10 at the end of the name. This should work.

Code:
Dim c As Control
For Each c In Me.Controls
            If TypeName(c) = &quot;Label&quot; Then
                If c.Name.Length = 8 Then
                    If Mid(c.Name, 1, 7) = &quot;MyLabel&quot; AndAlso (Mid(c.Name, 8, 1) >= &quot;1&quot; AndAlso Mid(c.Name, 8, 1) <= &quot;9&quot;) Then
                        c.Text = &quot;5&quot;
                    End If
                ElseIf c.Name.Length = 9 Then
                    If Mid(c.Name, 1, 7) = &quot;MyLabel&quot; AndAlso (Mid(c.Name, 8, 2) = &quot;10&quot;) Then
                        c.Text = &quot;5&quot;
                    End If
                End If
            End If
Next
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top