Click here to Skip to main content
15,884,042 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Doing a bit of math if a user enters a number in a textbox and press delete or backspace textbox will show (0) zero instead of blank.
Any help appreciated.

What I have tried:

C#
<pre>private void OnTextChanged(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))
                 txt_Different.Text = (j - i - t).ToString();   
    
             if (txt_Online.Text == "")
                 MessageBox.Show("Please enter Zero or another number in the textbox");    
              
         }
Posted
Updated 3-Oct-21 23:38pm

This is really easy if you use the debugger to follow what the code is doing.

This:
C#
txt_Different.Text = (j - i - t).ToString();
is where the 0 is coming from. If none of the text in the textboxes can be parsed, the values of those variables is going to be 0. The you're essentially calling 0.ToString(), which is going to return the string "0".
 
Share this answer
 
1) modifying the standard behavior of key entry in a Control violates user interface guidelines: it will probably confuse users. 'delete clears the current selection; why deprive the user of that functunality ?

2) You have 4 TextBoxes, assuming 'OnTextChanged is the TextChanged event handler for all 4:

a) if 'txt_Different is used only to display the result of calculations, why allow the user to type in it. consider using a Label.

b) if any of the attempts to parse 'txt_BackedOut, 'txt_TOTAL, 'txt_Online fail, why test 'txt_Different, and possibly change it?

Here's something to help you get started:
private TextBox currentTextBox;

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
    currentTextBox = sender as TextBox;

    char key = e.KeyChar;

    bool allowkey =
        (Char.IsNumber(key) || key == '\b')
        &&
        (currentTextBox.Text.Length >  0 && key != '0');

    e.Handled = ! allowkey;
}
This event handler checks each character entered: if the character is a number, or a backspace; then the entry of '0' is prevented if it's the first character. Note that delete is not disabled, and that cut, and paste, are not disabled.

i have strong opinions about how data-entry should be handled/validated in cases like this; i'll be happy to share them, but, first, i'd like to see you consider what i've said here and revise your code.
 
Share this answer
 
v3
You can use the KeyDown event, for example:
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.Delete || e.KeyCode == Keys.Back)
    {
        e.Handled = true;
        textBox1.Text = "0";
    }
}

You are strongly recommended to use NumericUpDown in stead of TextBox.
 
Share this answer
 
v3

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