Jump to content

Modifying a label

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
5 replies to this topic

#1
Alex_j

Alex_j

    Newbie

  • Members
  • PipPip
  • 29 posts
Hi, I need to change the value of a label, based upon a value rolled by a dice, for example if the dice rolls a 6 then the label named 'square6' has to be changed or if it rolled a 10 square 10 should be changed etc.

How could I do this?

#2
CommittedC0der

CommittedC0der

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,565 posts
You could just use a if loop, like:
if(diceNum == 1)
{
label1.Text = "You rolled a one";
}
else if (diceNum == 2)
{
label2.Text = "You rolled a two";
}
ect...
There may be a easyer way to do this, but this will work.

EDIT* you could also use case's:
switch(diceNum)
{
case 1: 
label1.Text = "You rolled a one";
break;

case 2:
label2.Text = "You rolled a two";
break;

case 3: ect.....
}

A man can be defined by what he does when no one is looking.
Science is only an educated theory, which we cannot disprove.

#3
Alex_j

Alex_j

    Newbie

  • Members
  • PipPip
  • 29 posts
I've done a switch statement, but there are 36 cases and its quite long, so a way to shorten it would be great. I've been told to look at controls.find? Or use a label array?

#4
Vswe

Vswe

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 9,552 posts
label1.Text = "You rolled a " + diceNum;


#5
CommittedC0der

CommittedC0der

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,565 posts
Here, you can use an array to get the values and print them into the labels, there's not really a way your going to avoid changing the labels value without writing them all out(that I know of, I look at it more). If you don't know how to use arrays, see this tutorial: http://forum.codecal...ial-arrays.html :)
 int[] DiceNum = new int[5] { dice1Roll, dice2Roll,dice3Roll dice4Roll, dice5Roll};
            label1.Text = Convert.ToString(DiceNum[0]);
            label2.Text = Convert.ToString(DiceNum[1]);
            label3.Text = Convert.ToString(DiceNum[2]);
            label4.Text = Convert.ToString(DiceNum[3]);
            label5.Text = Convert.ToString(DiceNum[4]);

A man can be defined by what he does when no one is looking.
Science is only an educated theory, which we cannot disprove.

#6
CommittedC0der

CommittedC0der

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,565 posts
Ahhh, here we go, I just messed around and came up with this:
 Label[] label = new Label[5] { label1, label2, label3,label4, label5};
            int[] diceVal = new int[5] { dice1Roll, dice2Roll, dice3Roll, dice4Roll, dice5Roll };
            for (int i = 0; i < diceVal.Length; i++)
            {
                label[i].Text = Convert.ToString(diceVal[i]);
            }
I hope that clear enough, if you don't understand read the tutorial or ask me, and ill try to help. :)
A man can be defined by what he does when no one is looking.
Science is only an educated theory, which we cannot disprove.