Click here to Skip to main content
14,972,741 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My Dice Rolling program is not displaying results in the listbox, any suggestions to remedy this issue much appreciated, here is the code:

C#
{
    public partial class FormDice : Form
    {
        int sides;
        int answer;

        public FormDice()
        {
            InitializeComponent();
        }

        private void exitButton_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void clearButton_Click(object sender, EventArgs e)
        {
            twoRadioButton.Checked = false;
            tenRadioButton.Checked = false;
            fiftyRadioButton.Checked = false;
            resultsListBox.Items.Clear();
        }

        private void rollButton_Click(object sender, EventArgs e)
        {
           

            if(twoRadioButton.Checked == true)
            {
                sides = 2;
                RollDice roll = new RollDice(sides);
                answer = roll.Roll();
            }

            else if(tenRadioButton.Checked == true)
            {
                sides = 10;
                RollDice roll = new RollDice(sides);
                answer = roll.Roll();
            }

            else if(fiftyRadioButton.Checked == true)
            {
                sides = 50;
                RollDice roll = new RollDice(sides);
                answer = roll.Roll();
            }

            else
            {
                RollDice roll = new RollDice(sides);
                answer = roll.Roll();
            } 
        }

        private void resultsListBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            resultsListBox.Items.Add(answer.ToString());
        }

       
    }
}




C#
{
    class RollDice
    {
        //variables
        private int numberOfSides;
        private Random randomNumberGenerator = new Random();

        //constructor with no parameters
        public RollDice()
        {
            numberOfSides = 6;
        }

        //constructor with parameter
        public RollDice(int sides)
        {
            numberOfSides = sides;
        }


        //method to roll the dice by returning an integer value
        public int Roll()
        {
            int randomNumber = randomNumberGenerator.Next(1, numberOfSides + 1);
            return randomNumber;
        }


    }


}
Posted

1 solution

The reason is that the item is being added in SelectedIndexChanged event of resultsListBox as given in the question
C#
private void resultsListBox_SelectedIndexChanged(object sender, EventArgs e)
{
   resultsListBox.Items.Add(answer.ToString());
}

If the result is required to be added when the rollButton is clicked, then add the item
C#
resultsListBox.Items.Add(answer.ToString());

in the Click event of rollButton, rollButton_Click event.
   
v2
Comments
Nancylh23 18-Apr-12 0:29am
   
TY very much that was indeed the problem!
VJ Reddy 18-Apr-12 0:30am
   
You are welcome. Thank you for response.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900