Click here to Skip to main content
15,867,568 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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

What I have tried:

C#
<pre>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();
                        
        }
Posted
Updated 16-Sep-21 2:36am
Comments
Member 15329613 15-Sep-21 15:55pm    
Order is important when subtracting so how is it wrong?

But change your code to be clear what you want.

Do you want (j-i) - t or j-(i-t)? You need to add parens to be clear on what you are doing.
Member 12349103 15-Sep-21 18:20pm    
I want it to work in any order the values are entered
Richard Deeming 16-Sep-21 4:28am    
Presumably the calculation doesn't change depending on the order in which the user enters the values?

"TextBox1" should always be "total" - "backed out" - "online", right?
Member 15329613 16-Sep-21 7:42am    
Then check for blanks and substitute with 0 until they have typed something in.
Member 12349103 15-Sep-21 18:35pm    
some users enter the data in the wrong order so it needs to work on the fly

"Validation" is about not giving a user the opportunity to make an error, as well as ensuring that user-entered (or run-time accessed, or pasted-in) data is in the correct format.

If you don't want the user to type in what is now your third TextBox, use a Label to show the answer.

One strategy:

1) initially enable only the first TextBox: when the user has entered a valid number, enable the second TextBox; when both have valid numbers, show the answer in the Label.

1a) you can use the 'Leave event of a TextBox to trigger validation; or you can sub-class TextBox to make a TextBox will accept only numbers, and/or decimal-point, backspace, key entries.

There is a heavier-duty facility for validation for Controls in WinForms: study that, and learn how to use it: [^]. Example of validation with TextBox: [^]
 
Share this answer
 
v2
<pre> 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 = "";
         }
     }
 }
 
Share this answer
 

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