Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C# Windows Console Application using textboxes with integer input and output. I

ID: 3562857 • Letter: C

Question

C# Windows Console Application using textboxes with integer input and output.

I have 3 things I'm trying to relate to each other. textBox1 is for user input of a number, button1 is to click and output to textbox2. Example: input from user is 100, I click the button, a calculation is done and I get the number 10. I was trying to do something like below and my problem arises from the fact I'm trying to compare integers and strings.

I'm just trying to figure out how to calculate an integer input from one textbox and output it to another text box.

Note: I'm not trying to limit input to integers; I want to assume the user input is correct.

private void button_Click(object sender, EventArgs e)
        {
          ouputBox.Text = Convert.ToInt32(inputBox.Text) / 10;
        }

Explanation / Answer

Take the help of paranthesis to avoid confusion to the compiler while performing such mathematical operations.

Also, the text box accepts only String data as a value. The code what you've written will throw out an Integer which is perfectly incompatible for a text box to hold. Hence use ToString() function for this!!

All together, your program will run successfully when,

private void button_Click(object sender, EventArgs e)
        {
          ouputBox.Text = (Convert.ToInt32(inputBox.Text) / 10).ToString();
        }

In addition, my suggestion is to Convert the text from your inputBox to a Double data type rather than to Int32.

It helps you give accurate results when you happen to divide with any number.

Ex: 1/10 will give you 0 as a result if we did convert the inputText to Int32.

Whereas if we did Convert.ToDouble(inputText) then you can see the exact value i.e., 0.1 in the result text box.

Hope this solved your problem!

Good luck!!