Click here to Skip to main content
14,932,691 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
On Button click, divide the value of textbox1 with the sum of the values of TesxtBox2, TextBox3, and TextBox4, then multiply the result with the value of TextBox5. Show the final result in textBox6. I have tried manipulating the below code, but doesn't seem to get. Would appreciate any valuable assistance


What I have tried:

private void textBox1_TextChanged(object sender, EventArgs e)
    {
        if(!string.IsNullOrEmpty(textBox1.Text) || string.IsNullOrEmpty(textBox2.Text))

         { 
             textBox3.Text = (Convert.ToInt32(textBox1.Text) + Convert.ToInt32(textBox2.Text).ToString());
         }
    }

    private void textBox2_TextChanged(object sender, EventArgs e)
    {
        if(!string.IsNullOrEmpty(textBox1.Text) || string.IsNullOrEmpty(textBox2.Text))

        {
        textBox3.Text = (Convert.ToInt32(textBox1.Text) + Convert.ToInt32(textBox2.Text).ToString());
        }
    }
Posted
Updated 9-May-21 9:54am

Few observations
- Since you calculate sum for the values I take it both text boxes 1 and 2 need to be filled in. So instead of using || (or) try using && (and). See Boolean logical operators - C# reference | Microsoft Docs[^]
- Based on the previous, you seem to be missing ! (not) from the second condition (string.IsNullOrEmpty(textBox2...)
- Converting to int fails if the format isn't correct so you either would need error checking (see try-catch - C# Reference | Microsoft Docs[^]) or you can use Int32.TryParse Method (System) | Microsoft Docs[^]
- Also the assignment seems to define that the calculation is done hen a button is clicked :)
   
First step:
Try to replace
C#
textBox3.Text = (Convert.ToInt32(textBox1.Text) + Convert.ToInt32(textBox2.Text).ToString());

with
C#
textBox3.Text = (Convert.ToInt32(textBox1.Text) + Convert.ToInt32(textBox2.Text)).ToString();
   
Comments
Richard MacCutchan 26-Apr-21 4:00am
   
They should always be using TryParse.
Patrice T 26-Apr-21 4:07am
   
Sure, but the misplaced parenthesis is even worse.
BillWoodruff 26-Apr-21 6:05am
   
+2 not a solution
Patrice T 26-Apr-21 7:42am
   
The misplaced parenthesis qualify as error !
Just giving correction to this error.
BillWoodruff 26-Apr-21 12:16pm
   
my vote acknowledges you have posted 20% of a solution; that's really charity on my part :)
Define a Dictionary to map TextBoxes to an integer value: private Dictionary<TextBox, int> TbxToInt;

Put all five TextBoxes in a Panel named 'panel1; in the Form Load event:
private void Form1_Load(object sender, EventArgs e)
{
    TbxToInt = new Dictionary<TextBox, int>();

    foreach(TextBox tbx in panel1.Controls.OfType<TextBox>())
    {
        TbxToInt.Add(tbx, 0);
        tbx.Text = "0";
    }
}
Note all the five TextBoxes all have their initial Text value set to "0:" select all the four TextBoxes for user input in design view, and assign the same 'Leave EventHandler to them:
private void textBox_Leave(object sender, EventArgs e)
{
    TextBox tbx = sender as TextBox;
    
    int value = 0;

    if (! int.TryParse(tbx.Text, out value))
    {
        tbx.Text = "0";
    }
}
This ensures that only an integer value is displayed.

Left for you to write: the code to scan the Dictionary key-value pairs, and use the integer values in calculation.

Note: I'd handle this by creating a custom component inheriting from TextBox that only let the user type in integer key characters; a more advanced solution: if I showed that, and you used it for a homework assignment, your instructor might be unhappy.
   
v2
Just a side note... Your directions start with "On Button click,". So shouldn't you be using a "Calculate" button instead of "textBox1_TextChanged"?
   
I would suggest you use a Int32.TryParse method to validate the string before blindly assuming the string is a integer and perform an operation.

Int32.TryParse Method (System) | Microsoft Docs[^]

int result1;
int result2;
if(Int32.TryParse("My String", out result) && Int32.TryParse("Another String", out result2))
{
// Do something with the integer results.
}

Otherwise you may get a runtime exception.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900