-4

I have this code for found Textbox1 greatest Textbox2 The code working but if I make “0” or “0,5” there are an bug (if there are ",").

Anybody to have an solution for accept "0" or "," ?

    public partial class MainWindow : Window
{
    int point1, point2;
    int point3, point4;

    public MainWindow()
    {
        InitializeComponent();
        point1 = point2 = 0;
        point3 = point4 = 0;
    }

    private void textBox1_TextChanged(object sender, TextChangedEventArgs e)
    {
        if (textBox1.Text != "") { 
        point1 = int.Parse(textBox1.Text);
        int tt;
        int uu;
        tt = point1 - point2;
        uu = point3 - point4;
        if (tt >=uu)
        texboxxol1.Background = Brushes.Yellow;
        texboxxol2.Background = Brushes.White;
        }
    }

    private void textBox2_TextChanged(object sender, TextChangedEventArgs e)
    {
        if (textBox2.Text != "")
        {
            point2 = int.Parse(textBox2.Text);

            int tt;
            int uu;
            tt = point1 - point2;
            uu = point2 - point1;
            if (uu >= tt)
            texboxxol2.Background = Brushes.White;
            texboxxol1.Background = Brushes.Yellow;
        }
    }
realiste
  • 25
  • 6
  • 3
    well 0,5 isn't an integer. What would you expect/like to happen?... – Sayse Oct 10 '16 at 21:24
  • when i write "0,5" or just "0," i have the message :The input string format is incorrect. – realiste Oct 10 '16 at 21:33
  • 1
    Thats correct. A decimal isn't an integer. That isn't a programming problem, thats just math – Sayse Oct 10 '16 at 21:35
  • 1
    When you don't even know what data types you are using, you probably shouldn't just be using code you "found"... I'd recommend following some basic tutorials first. – Joe Oct 10 '16 at 21:59

1 Answers1

1

first of all if you want to be able to compare number like 0,5 you need to use a different data type than integer. float would be a good start. Here is a table with all built in data types of C#.

Second: using float alone will not solve your problem, because you use the TextChanged event of the TextBox which is fired every time the user enters a single digit into the TextBox. Even if the user enters only an int like 1234567890 the event will be fired 10 times.

May be you should use a button or the so that the code inside your events can be executed once when the entire number is typed into the TextBox. You could also use the KeyDown event so that the user can confirm the input by pressing Enter. Here is a post that shows how to do that.

Community
  • 1
  • 1
Mong Zhu
  • 23,309
  • 10
  • 44
  • 76
  • Thx for your information, But I can’t use the key down because when I push a button There are a calculated that going from 0.5 + 0.5, the results go up, all time. – realiste Oct 10 '16 at 22:41
  • @realiste I don't see in your posted code any addition or something counting up. When you use the `KeyDown` event properly you should solve your problem. Your calculation will be executed if and only if the user presses the ENTER key. Do you intend to compare the points each time when the user enters each number in succession? like 123 ? at 1 at 2 at 3 ? – Mong Zhu Oct 11 '16 at 20:34
  • i understand and i change my code with only 1,2,3,4... thx for your information – realiste Oct 13 '16 at 19:34