[RESOLVED] Updating Labels text within a "for loop" sequentially?
Embarassingly enough, I posted in the wrong forum the first time around, and I'm pretty sure that's why it never even appeared. Oh well. THIS is the right forum now, I hope, so here's my problem:
I'm basically simulating bowling. I've created a form that has a "scoreboard" on it, and I'm updating the cells of the scoreboard to display the bowl result. Without doing 21 if statements, I figured I could call a method, send it which roll number it is, and then use a for loop to update each label's text values.
So, here's what I have:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Code:
Dim counterRolls, tempRoll, counterTotal As Integer
counterTotal = 1
For counterRolls = 1 To 20
If counterRolls Mod 2 = 1 Then
tempRoll = rollGame(counterRolls)
If tempRoll = 10 Then
counterRolls += 1
End If
Else
tempRoll = rollGame(counterRolls, tempRoll)
End If
If roll19.Text = "10" And roll20.Text = "10" Then
tempRoll = rollGame(21)
Else
roll21.Text = "Null"
End If
Next
End Sub
That is the button click function that calls one of two methods depending on the variables. For example, one method is here:
Code:
Private Overloads Function rollGame(ByVal rollNum As Integer) As Integer
randomNum = randomGenerator.Next(0, 11)
Select Case rollNum
Case Is = 11
MessageBox.Show("Congratulations! That's a STRIKE!")
Case Is < 11
MessageBox.Show("You knocked down " + randomNum + "pins!")
End Select
roll(rollNum).Text(randomNum.ToString)
End Function
Clearly, this part "roll(rollNum).Text(randomNum.ToString)" is the part that I tried and failed to update the label. The labels are labelled, in this case, roll1 through roll21.
Is there anything I can do to resolve this problem?
Thanks so much!
-William
Re: Updating Labels text within for loop sequentially?
That line should be
Code:
roll(rollNum).Text = randomNum.ToString
as you want to assign the rolled number to the label's text property, so you need to use the assignment operator (=)
Re: Updating Labels text within for loop sequentially?
Quote:
Originally Posted by
keystone_paul
That line should be
Code:
roll(rollNum).Text = randomNum.ToString
as you want to assign the rolled number to the label's text property, so you need to use the assignment operator (=)
You are correct in the part of assigning it. However, the roll(rollNum) is representing a label. I have 20 labels, counted from roll1 to roll21. I am unaware as to how to call a label using a variable acting as the integer. That is the problem I am having.
I believe that VB takes the roll(rollNum) as meaning calling a function roll with the parameter rollNum sent to it. I'm just trying to call the labels using a variable.
Thanks.
Re: Updating Labels text within a "for loop" sequentially?
hey Pansophist
you may get reference of label dynamically like this
vb Code:
Dim lblRollno As Label = DirectCast(Me.Controls("roll" +rollNum ), Label )
' then set the text
lblRollno.Text = randomNum.ToString()
Re: Updating Labels text within a "for loop" sequentially?
Ah OK I understand what you are getting at - your terminology is a little strange!
I assumed that roll() was an array of labels.
You if the labels are named roll1, roll2 etc, you could use :
Code:
CType(me.controls("roll" & rollnum.tostring), label).Text = randomNum.ToString
[EDIT - Su Ki beat me to it!]
Re: Updating Labels text within a "for loop" sequentially?
Quote:
Originally Posted by
su ki
hey Pansophist
you may get reference of label dynamically like this
vb Code:
Dim lblRollno As Label = DirectCast(Me.Controls("roll" +rollNum ), Label )
' then set the text
lblRollno.Text = randomNum.ToString()
Thank you very much. Now I can start debugging other problems! Woohoo.