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?
Modifying a label
Started by Alex_j, May 08 2010 10:09 AM
5 replies to this topic
#1
Posted 08 May 2010 - 10:09 AM
|
|
|
#2
Posted 08 May 2010 - 03:51 PM
You could just use a if loop, like:
EDIT* you could also use case's:
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.
Science is only an educated theory, which we cannot disprove.
#3
Posted 09 May 2010 - 12:36 AM
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
Posted 09 May 2010 - 12:48 AM
label1.Text = "You rolled a " + diceNum;
#5
Posted 09 May 2010 - 06:24 AM
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.
Science is only an educated theory, which we cannot disprove.
#6
Posted 09 May 2010 - 06:34 AM
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.
Science is only an educated theory, which we cannot disprove.


Sign In
Create Account


Back to top









