question

Booney-3048 avatar image
0 Votes"
Booney-3048 asked Booney-3048 commented

C# winform minus 2 textboxs show answer in 3rd in reverse order

This should be easy to fix but I cant fig it out.
I have 3 textboxes one below the other as a example. I tried textbox change

It works fine if I enter in order but if the user is out of order the math is wrong.
Is there a work around?



TXT1 - TXT2 = Correct answer
TXT2 - TXT1 = Wrong answer

 private void CalcTotal(object sender, EventArgs e)
         {
    
             if (int.TryParse(txt_BackedOut.Text, out int i) && int.TryParse(txt_TOTAL.Text, out int j) && int.TryParse(txt_Online.Text, out int t))
                 textBox1.Text = (j - i - t).ToString();
                            
         }


dotnet-csharpwindows-forms
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.


Did you handle the TextChanged event for all of input textboxes?


0 Votes 0 ·
karenpayneoregon avatar image
0 Votes"
karenpayneoregon answered Booney-3048 commented

Needs some polishing up, see if this might work for you.

132495-figure1.png

 public partial class Form1 : Form
 {
     private readonly List<TextBox> _textBoxes = new List<TextBox>();
     public Form1()
     {
         InitializeComponent();
     }
    
     private void Form1_Load(object sender, EventArgs e)
     {
         _textBoxes.Add(textBox1);
         _textBoxes.Add(textBox2);
         _textBoxes.Add(textBox3);
    
         _textBoxes.ForEach(textbox => textbox.TextChanged += OnTextChanged);
     }
    
     private void OnTextChanged(object sender, EventArgs e)
     {
         if (_textBoxes.All(texbox => int.TryParse(texbox.Text, out _)))
         {
             int.TryParse(textBox1.Text, out var v1);
             int.TryParse(textBox2.Text, out var v2);
             int.TryParse(textBox3.Text, out var v3);
    
             textBox4.Text = (v1 + v2 - v3).ToString();
    
         }
         else
         {
             textBox4.Text = "";
         }
     }
 }



figure1.png (3.0 KiB)
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

works perfect.

Thank you

0 Votes 0 ·
Paul-5034 avatar image
0 Votes"
Paul-5034 answered Booney-3048 commented

Do you mean if you enter values in your textboxes in different order? (i.e. BackedOut, Total, Online -> Online, Total, BackedOut)

· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

yes change any value and the correct value comes back in any order.

0 Votes 0 ·

What's the value you're trying to calculate?

0 Votes 0 ·

just numbers in any order this example minus

0 Votes 0 ·
Paul-5034 avatar image
0 Votes"
Paul-5034 answered Booney-3048 edited

You mean like?:

 textBox1.Text = (-j - i - t).ToString();


· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

20 10 = 30 - 5 = -25 with that line should be 25

0 Votes 0 ·
 j = 20
 i = 10
 t = 5
    
 j + i - t = 25

?

0 Votes 0 ·

Thank you for the help but
iIf I dont do it in that order the answer is wrong

0 Votes 0 ·