0

I'm new to learning winforms and i'm stuck on the following problem and I do not think what I have done is the correct way, so any help would be appreciated.

I have 4 textboxes such as the following

private void txtBxPlayer1Bid_TextChanged(object sender, EventArgs e)
        {

            txtBxFundsAvialable.Text = (Convert.ToInt32(txtBxFundsAvialable.Text) - Convert.ToInt32(txtBxPlayer1Bid.Text)).ToString();
        } 

The 5th textbox txtBxFundsAvialable simple subtract the value of txtBxPlayer1Bid from txtBxFundsAvialable.

In designer.cs I have

this.txtBxPlayer1Bid.Leave += new System.EventHandler(this.txtBxPlayer1Bid_TextChanged);

The problem I have is, if I have 100 in txtBxFundsAvialable and enter 10 in txtBxPlayer1Bid the value in txtBxFundsAvialable should be 90, but txtBxPlayer1Bid etc seem to go into a loop and the value in txtBxFundsAvialable becomes 60. 4 textboxes X 10.

This happens for any of the 4 textboxes

The only way I can solve the problem is to set the values of the 4 textboxes to 0 in the txtBxFundsAvialable_TextChanged as shown below.

 private void txtBxFundsAvialable_TextChanged(object sender, EventArgs e)
        {
            if (Convert.ToInt32(txtBxPlayer1Bid.Text) > 4 || (Convert.ToInt32(txtBxPlayer2Bid.Text)> 4 || (Convert.ToInt32(txtBxPlayer3Bid.Text)> 4) || (Convert.ToInt32(txtBxPlayer2Bid.Text)> 4)))
            {
                txtBxPlayer1Bid.Text    = "0";
                txtBxPlayer2Bid.Text    = "0";
                txtBxPlayer3Bid.Text    = "0";
                txtBxPlayer4Bid.Text    = "0";
             }
        }

Is what I'm doing the correct way, as stated at the beginning, I'm new to winforms and it a canny leanning curve

George Phillipson
  • 830
  • 11
  • 39
  • Why do you listen to the `Leave` event? – SimpleVar Feb 06 '15 at 22:41
  • Hi @YoryeNathan looking for help on forums and here and it seems this was the correct way i.e http://stackoverflow.com/questions/16619225/why-my-textbox-textchanged-event-gets-fired-after-i-enter-only-one-character-i – George Phillipson Feb 06 '15 at 22:44
  • Listen to the `TextChanged` event. It's just so logically correct. – SimpleVar Feb 06 '15 at 22:46
  • Either do what @YoryeNathan said or make a button. I didn't quite understand what you were trying to do in this script. Also, why do you have 4 different textboxes for a simple subtract? – HelloWorld Feb 06 '15 at 22:47
  • Hi @Aradmey the 4 textboxes can have different values, the user does not have to enter a value in three of them, but at least 1 must have a value. I was thinking about a button, but was trying to do it without a button. Don't learn unless you try :) – George Phillipson Feb 06 '15 at 22:54
  • 1
    There is an obvious logical error in your code, changing the textbox text cannot subtract an amount from a total. The user types `1` and you subtract 1, then `0` and you subtract 10. So you've subtracted 11, that's not correct of course. Thinking about how to fix that is a mental exercise that's important to do by yourself if you want to become a programmer. – Hans Passant Feb 06 '15 at 22:56
  • Hi @HansPassant not sure what you mean, if I have txtBxFundsAvialable.Text = (Convert.ToInt32(txtBxFundsAvialable.Text) - Convert.ToInt32(txtBxPlayer1Bid.Text)).ToString(); and the value in txtBxFundsAvialable is 100, if I enter 1 in txtBxPlayer1Bid then the value in txtBxFundsAvialable becomes 99. – George Phillipson Feb 06 '15 at 23:01

2 Answers2

3

I wrote a simple code with 2 textboxes that get values and a textbox with the result. Updates with TextChangedevent. Try to use it to fix your code..

    private void textBox1_TextChanged(object sender, EventArgs e)
    {
        try
        {
            int num1 = Int32.Parse(textBox1.Text), num2 = Int32.Parse(textBox2.Text);
            textBox3.Text = (num1 - num2).ToString();
        }
        catch { }
    }

    private void textBox2_TextChanged(object sender, EventArgs e)
    {
        textBox1_TextChanged(sender, e);
    }

EDIT

Try this code and link any of your "bid" textboxes to this function. textbox1 in this code is equivalent to your "available" textbox.

    private void textBox_Leave(object sender, EventArgs e)
    {
        try
        {
            int num = Int32.Parse(((TextBox)sender).Text), available = Int32.Parse(textBox1.Text);
            textBox1.Text = (available - num).ToString();
        }
        catch { }
    }
HelloWorld
  • 1,128
  • 1
  • 7
  • 14
  • Hi @Aradmey quick question, what version of VS are you using, I have just tried your code example and get the following error when entering 3 Input string was not in a correct format. Also as stated earlier, when using TextChanged it only allows entering 1 character before fireing. – George Phillipson Feb 06 '15 at 23:14
  • I am using VS 2010. I just understood what you're trying to script and have made something work with `Leave` event. Will edit my answer in a moment – HelloWorld Feb 06 '15 at 23:22
  • Check the new code I posted above. And if you want to clear the textbox from text after it leaves too, use this line: `((TextBox)sender).Text = "";` – HelloWorld Feb 06 '15 at 23:28
0

Not sure how .Leave operates. Try to use .TextChanged or whatever the equivalent in WinForms.

All four (or even five) text boxes should use same event callback method.

Here is what you can do in that method:

private void txtBx_TextChanged(object sender, EventArgs e)
{
    double player1 = 0, player2 = 0, player3 = 0, player4 = 0, total = 0;
    if (int.TryParse(txtBxPlayer1Bid.Text, out player1)
         && int.TryParse(txtBxPlayer2Bid.Text, out player2)
         && int.TryParse(txtBxPlayer3Bid.Text, out player3)
         && int.TryParse(txtBxPlayer4Bid.Text, out player4)
         && int.TryParse(txtBxFundsAvialable.Text, out total)

    {
        total = player1 + player2 + player3 + player4;
    }
}
Alkasai
  • 3,757
  • 1
  • 19
  • 25
  • Hi @Alexey .TextChanged only allows 1 char before firing, that was why I was using leave. – George Phillipson Feb 06 '15 at 22:53
  • that is right, that's will constantly update the total as you type. otherwise you can add a button to trigger calculation as @Aradmey suggested. – Alkasai Feb 06 '15 at 22:57