views:

34

answers:

3

I'm working on a program that asks for input, then calculates and posts the output.

I managed to do it on a console application, but I don't know how to store input from a textbox.

The input has to be positive integers, with an error message in place if anything invalid is input (to prevent crashing).

I plan to have use a button to trigger the calculation and output, but I need to know how to store the input first.

+2  A: 

add a click handler to your button with double-clicking on it. afterwards, insert the following code:

private void button1_Click(object sender, EventArgs e)
{
    string text = textBox1.Text;
}

the text from the textbox is now within variable text.

henchman
A: 

Store the input in instance variables. I believe that the information is stored in a property of the textfields. Something along the lines of

 theNameOfYourTextField.text
Kyle
+1  A: 

Double click your button to add a handler method, then grab the value from the field as an integer:

private void button1_Click(object sender, EventArgs e)
{
    int foo;
    int bar;
    if ( ! Int32.TryParse(textBox1.Text, out foo))
    {
        MessageBox.Show("Foo must be a number!");
        return;
    }
    if ( ! Int32.TryParse(textBox2.Text, out bar))
    {
        MessageBox.Show("Bar must be a number!");
        return;
    }
    // do something with foo and bar
}
Don Kirkby
I have multiple text boxes. How can I use this for all of them?
Slateboard
@Slateboard, I updated my answer to show two fields: textBox1 and textBox2.
Don Kirkby
Thanks. I'll try this now.
Slateboard
It worked. It doesn't crash when an invalid value is input. Thanks.
Slateboard