Auto calculate textbox has a value of zero when emptying textbox for listbox

user9771053

I am using this calculate function

  private void Calculate()

    {

        double.TryParse(textBox1.Text, out num);

        for (int a = 1; a <= 10; a++)
        {
            listBox1.Items.Add(a + " * " + num + "\n = " + a * num);
        }
    }

Putting Calculate() inside private void textBox1_TextChanged so it will autocalculate

I am also using :

 private void textBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Back)
            {
                listBox1.Items.Clear();


        }
    }

This works but if i backspace the value inside textBox1 then when that box is empty it still shows the calculate function using a zero in the calculation displayed inside the listbox as if the empty textbox is a zero if it's empty except when i backspace again it will go away. I was wondering why that is and what the logic is behind that. So to be clear

When i enter 12 the calculate functions show 12, and if i backspace it will be 1 and it will show the calculate function as 1 but if i backspace again to make it empty it will show the calculate function as 0 inside the listbox.Here is the screenshot

Thanks for your time and help.

Steve

You don't take the result of TryParse and whatever you have (or not have) in the textbox you run the calculate code. Check if the return of TryParse is false and exit the code

If TryParse is unable to parse the input string (like in case of an empty string) it sets the out variable to its default value (zero for double) and thus you have your code running against the zero multiplier.
This happens also if the user types a not numeric value like a letter.

private void Calculate()
{

    if(!double.TryParse(textBox1.Text, out num))
       return;
    for (int a = 1; a <= 10; a++)
    {
        listBox1.Items.Add(a + " * " + num + "\n = " + a * num);
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Listbox Sorting based on TextBox value

From Dev

Textbox in listbox not updating when lostfocus

From Dev

Check if textbox has a value

From Dev

how to use the listbox value to update textbox

From Dev

Listbox DataTemplate clear textbox value on selection

From Dev

check if a textbox value already exists in a listbox

From Dev

how to disable a textbox when the select before it has a value of 0

From Dev

Use Up/Down keys to scroll a ListBox when a TextBox has focus without moving cursor

From Dev

How to set the value of a textbox to 0 “Zero” in a continues form footer while the detail of the form has no data MS Access

From Dev

Zero Pad an integer array when printed to textbox

From Dev

Auto set minimum and maximum value on textbox

From Dev

Get value from calculate it by using textbox? Javascript

From Dev

Check if the textbox has value, make the other textbox required

From Dev

Check if the textbox has value, make the other textbox required

From Dev

Auto check a checkbox when textbox gets text in it

From Dev

Check if listbox contains textbox

From Dev

Check if listbox contains textbox

From Dev

How to set a TextBox value when another TextBox value is changed

From Dev

Excel VBA: Searching for value in listbox based on value set in textbox

From Dev

Excel VBA: Searching for value in listbox based on value set in textbox

From Dev

Textbox.Text has the wrong value

From Dev

CKEditor has Empty Value on asp:textbox

From Dev

Disable a button when textbox has data in it

From Dev

How do I add the tag value and the text value of multiple TextBox, only when the text has changed in WPF?

From Dev

Data Binding so that changing a TextBox value updates the LIstBox

From Dev

Data Binding so that changing a TextBox value updates the LIstBox

From Dev

Child textbox will not stretch when grid in ListBox ItemTemplate is split into two columns

From Dev

When using a button to choose a listbox item to show a string of text in a textbox

From Dev

Adding zero to TextBox with jQuery

Related Related

  1. 1

    Listbox Sorting based on TextBox value

  2. 2

    Textbox in listbox not updating when lostfocus

  3. 3

    Check if textbox has a value

  4. 4

    how to use the listbox value to update textbox

  5. 5

    Listbox DataTemplate clear textbox value on selection

  6. 6

    check if a textbox value already exists in a listbox

  7. 7

    how to disable a textbox when the select before it has a value of 0

  8. 8

    Use Up/Down keys to scroll a ListBox when a TextBox has focus without moving cursor

  9. 9

    How to set the value of a textbox to 0 “Zero” in a continues form footer while the detail of the form has no data MS Access

  10. 10

    Zero Pad an integer array when printed to textbox

  11. 11

    Auto set minimum and maximum value on textbox

  12. 12

    Get value from calculate it by using textbox? Javascript

  13. 13

    Check if the textbox has value, make the other textbox required

  14. 14

    Check if the textbox has value, make the other textbox required

  15. 15

    Auto check a checkbox when textbox gets text in it

  16. 16

    Check if listbox contains textbox

  17. 17

    Check if listbox contains textbox

  18. 18

    How to set a TextBox value when another TextBox value is changed

  19. 19

    Excel VBA: Searching for value in listbox based on value set in textbox

  20. 20

    Excel VBA: Searching for value in listbox based on value set in textbox

  21. 21

    Textbox.Text has the wrong value

  22. 22

    CKEditor has Empty Value on asp:textbox

  23. 23

    Disable a button when textbox has data in it

  24. 24

    How do I add the tag value and the text value of multiple TextBox, only when the text has changed in WPF?

  25. 25

    Data Binding so that changing a TextBox value updates the LIstBox

  26. 26

    Data Binding so that changing a TextBox value updates the LIstBox

  27. 27

    Child textbox will not stretch when grid in ListBox ItemTemplate is split into two columns

  28. 28

    When using a button to choose a listbox item to show a string of text in a textbox

  29. 29

    Adding zero to TextBox with jQuery

HotTag

Archive