-1

I am making a random number generator in a form application and the user will type a number in a text box. When user clicks on the OK button. the text in the textbox will be stored in a string value. for example;

if (ButtonOK is clicked)
{
    String a = textbox1;
    int b = int.Parse(a);
}

Then the value of the textbox will become a labels value. for example:

b = label1.Text;

how do i do that? I would be really happy if anyone could help me solve this problem.

SOLVED thanks to Soner Gönül

Muhammad Umar
  • 3,761
  • 1
  • 24
  • 36
user3024531
  • 9
  • 2
  • 6

2 Answers2

1

I feel like you need something like;

private void ButtonOK_Click(object sender, EventArgs e)
{
    string a = textbox1.Text;
    int b;
    if (Int32.TryParse(a, out b))
    {
        label1.Text = b.ToString();       
    }
}
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
1

Assuming is a WinForm application just drag a button and a textbox on the form, double click on the button and write this code:

 private void button1_Click(object sender, EventArgs e)
 {
  int max;
  if (!int.TryParse(textBox1.Text, out max))
  {
        label1.Text = "Not a number";
  }
  else
  {
      Random r = new Random();
      int random = r.Next(max);
      label1.Text = string.Format("Random number: {0}", random);
  }
}
  • nice to see your exlanation from the beginning(dragging button on to the form), writing actual part inside if block is much better and i don't suggest to create Random() object inside the if/else block as it can generate duplicate random numbers instead of that create a static object and call Next method every time – Sudhakar Tillapudi Nov 24 '13 at 11:08
  • see this common pit fall => http://stackoverflow.com/questions/767999/random-number-generator-only-generating-one-random-number – Sudhakar Tillapudi Nov 24 '13 at 11:12
  • Yes but for a simple example like that this should be enough. –  Nov 24 '13 at 11:16
  • Thank you! this one was really good, but i did not want to create a new messagebox. i wanted to change a value on a label. – user3024531 Nov 24 '13 at 11:28