Results 1 to 6 of 6

Thread: [RESOLVED] Updating Labels text within a "for loop" sequentially?

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2009
    Location
    Charlotte, NC - USA
    Posts
    3

    Resolved [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
    Last edited by Pansophist; Apr 20th, 2009 at 03:31 AM.

  2. #2
    PowerPoster keystone_paul's Avatar
    Join Date
    Nov 2008
    Location
    UK
    Posts
    3,327

    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 (=)

  3. #3

    Thread Starter
    New Member
    Join Date
    Apr 2009
    Location
    Charlotte, NC - USA
    Posts
    3

    Re: Updating Labels text within for loop sequentially?

    Quote Originally Posted by keystone_paul View Post
    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.

  4. #4
    Hyperactive Member su ki's Avatar
    Join Date
    Oct 2007
    Posts
    354

    Re: Updating Labels text within a "for loop" sequentially?

    hey Pansophist

    you may get reference of label dynamically like this

    vb Code:
    1. Dim lblRollno As Label = DirectCast(Me.Controls("roll" +rollNum ), Label )
    2. ' then set the text
    3. lblRollno.Text = randomNum.ToString()
    * If my post helped you, please Rate it
    * If your problem is solved please also mark the thread resolved it is there in right top of page under thread tools
    * Why Rating is useful

  5. #5
    PowerPoster keystone_paul's Avatar
    Join Date
    Nov 2008
    Location
    UK
    Posts
    3,327

    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!]

  6. #6

    Thread Starter
    New Member
    Join Date
    Apr 2009
    Location
    Charlotte, NC - USA
    Posts
    3

    Re: Updating Labels text within a "for loop" sequentially?

    Quote Originally Posted by su ki View Post
    hey Pansophist

    you may get reference of label dynamically like this

    vb Code:
    1. Dim lblRollno As Label = DirectCast(Me.Controls("roll" +rollNum ), Label )
    2. ' then set the text
    3. lblRollno.Text = randomNum.ToString()
    Thank you very much. Now I can start debugging other problems! Woohoo.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width