0

I am pretty new to c#, I am taking a class and we didn't cover numericUpDown at all. I am creating a basic dice roller for a project and have the basics laid out, however I can't seem to figure out how to add the numericUpDown how I would like.

private void btn4_Click(object sender, EventArgs e)
        {
            Random rnd = new Random();
            int randomnumber = rnd.Next(1, 5); //+1 higher than the dice selected
            txt4.Text = randomnumber.ToString();
        }

Image of a piece of the GUI

I want to be able to use the numericUpDown to select the amount of dice rolled and then when you click roll it will return however many values the numericUpDown has indicated. For example: If you have 4 selected on the numericUpDown it will return 4 random numbers when you click roll.

1
  • It's not clear to me where you're stuck. If you double-click on the numericUpDown control on the design surface, Visual Studio creates a handler function for when that control's value changes, does it not? Just like creating the click handler for the button that you already have? Then in that handler method you'd put your logic for what to do when the user changes that numeric value. Commented May 12, 2023 at 18:20

2 Answers 2

1

You'll need to use the value of your NumericUpDown in a for loop to tell it how many times to roll

Example:

private void button1_Click(object sender, EventArgs e)
{
    textBox1.Clear() // Empty the text box before rolling
    Random rnd = new Random();

    for (int i = 0; i < numericUpDown1.Value; i++)
    {
        int randomNumber = rnd.Next(1, 5);
        textBox1.AppendText($"Roll {i + 1}: {randomNumber}\r\n");
    }

}
Sign up to request clarification or add additional context in comments.

Comments

0

Maybe you need loop in there so until your numericUpDown value goes 0 you have to take random number and add that values in your string and then you can print it.

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.