-3

Im trying to write a program in C# that allows the user to input the number of seats sold, then the program multiples each number by the price to get how many were bough in each section. Ive gotten to the end but he program is not working because of the red line under the texbox.toString part. Can someone help me know what my errors are?

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void label7_Click(object sender, EventArgs e)
    {

    }

    private void button3_Click(object sender, EventArgs e)
    {
        this.Close();
    }

    private void button2_Click(object sender, EventArgs e)
    {
        textBox4.Text = "";
        textBox3.Text = "";
        textBox2.Text = "";
        total.Text = "";
        input1.Text = "";
        input2.Text = "";
        input3.Text = "";
    }

    private void button1_Click(object sender, EventArgs e)
    {
        int input1;
        int input2;
        int input3;

        input1 = int.Parse(input1.ToString());
        input2 = int.Parse(input2.ToString());
        input3 = int.Parse(input3.ToString());

        int sum1 = input1 * 15;
        int sum2 = input2 * 12;
        int sum3 = input3 * 9;

        sum1 = textBox3.ToString();
        sum2 = textBox2.ToString();
        sum3 = textBox4.ToString();
    }
}
Ben
  • 2,433
  • 5
  • 39
  • 69
Niomi
  • 9
  • 1
  • 6
  • Did you mean to use `textbox.Text` instead? – shree.pat18 Feb 11 '15 at 04:17
  • I would recommend to use **NumericUpDown** control to input numeric data. That will prevent you to typecasting every-time you are doing here and there. – Rohit Prakash Feb 11 '15 at 04:34
  • 1
    you are getting `"use of unassigned local variable'input1...` because you define `int input1` the same as your textbox name `input1`, try the solution proposed in my answer – chouaib Feb 11 '15 at 04:42

5 Answers5

1

to obtain the string written in your textBox:

textBox.Text;

to display a value sum in textBox:

textBox.Text = sum.ToString();

This is how button1_Click would look like at least:

private void button1_Click(object sender, EventArgs e)
{
    int in1; // change int names to not confuse woth textboxes
    int in2;
    int in3;

    in1 = int.Parse(input1.Text); // input1 is textBox
    in2 = int.Parse(input2.Text);
    in3 = int.Parse(input3.Text);

    int sum1 = in1 * 15;
    int sum2 = in2 * 12;
    int sum3 = in3 * 9;

    textBox3.Text = sum1.ToString();
    textBox2.Text = sum2.ToString();
    textBox4.Text = sum3.ToString();

}
chouaib
  • 2,763
  • 5
  • 20
  • 35
0

You can use just

int.Parse(textbox.Text);
Animal Style
  • 689
  • 2
  • 10
  • 31
  • I used that line and now in my input lines "input1 = int.Parse(input1.ToString());" the input... in the parentheses is saying "use of unassigned local variable'input ...' – Niomi Feb 11 '15 at 04:36
  • @chouaib I think has a full solution now. – Animal Style Feb 11 '15 at 04:44
0

sum is an integer but textBox.ToString() is a string. You can't assign a string into an integer

it should be

textBox3.Text = sum1.ToString();

Ben
  • 2,433
  • 5
  • 39
  • 69
Chidchai
  • 91
  • 6
0

The problem is occurring because you are attempting to store the value of a string into an int.

The line:

sum = textBox.ToString();

is just converting the value of textBox to a string, not the value of the text in textBox, and passing it into the sum variable. That won't work.

Try this instead:

sum = Convert.ToInt32(textBox.Text);

However, if you ever want to show the value of sum in textBox, you can implicitly convert from an int to a string:

textBox.Text = sum;
Ben
  • 2,433
  • 5
  • 39
  • 69
0

Use the "Text" property of the text box control instead. It would be better to have it as something like "input1.Text.Trim()", as this would remove any leading/trailing spaces that accidentally got into the input.

As a best practice, particularly while getting values from text controls in UI, it is good to use Int32.TryParse. (Please see: Parse v. TryParse)

As an additional point, your code does not follow basic naming conventions.

Community
  • 1
  • 1