I have written a very simple program inside of a standard .NET 4.0 windows form (well a form opened by another form to be more exact) in C# using Visual Studio 2010. The basic idea is I have a series of text boxes with numbers. I want to be able to increase them by specific preset amounts and stop at a given limit. The limit being 20 increments (remaining) between all attributes (Hp,Mp, Str, Agil, Int, Will, Luck) (50/50/10/10/10/10/10), however decreasing an increment (by the same amount) should return the increment counter to its former state.

Ex: Number is 10. Its incremented 20 times so the number is 30.. it will not go under 9 or be incremmented by more than 1 under any circumstance. The code below allows HP to work perfectly. That is until I try to modify other numbers, then that goes out the window. (Along with this PC soon as this rate). The code for MP is litterally a copy/paste of HP with the textBox names changed.

However, MP and everything below act 'funny' after the first increment. The increment counter should never change by more than 1, however is often jumping by 50. The + and - buttons will stop responding and act as if there is no code linked to the buttons.

I have tried saving, rebuilding, cleaning, restarting. Gone back and verified the code multiple times. Even rewrote certain statements to various forms like number = number - 1; number -= 1;, number--; but to no avail. I have confirmed that number is written to at the start of each button click method to ensure that no other method/button click stored eronous data inside. I have confirmed that number is only declared one time at the start of the class.

There are no compile / build errors. Aside from automatically generated code to create the form and the objects on it, all code is in the file listed below. Close and Reset buttons appear to be doing their jobs correctly. The numbers the amounts are changing by are only numbers that I have hard coded, but not in the proper places. (Ex: Mp+ should be -1 Remaining, +50 Mp.. it does +50 to both). Not sure what else of relavence I can mention.

If its really that important I can try going through various increments / decrements to find exactly when 1 or 50 is added or subtracted from each text box, but I am trying to avoid such tedious and pointless meanderings.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class CharCreate : Form
    {
        int number; //temp for parsing

        public CharCreate()
        {InitializeComponent();}

        private void buttonClose_Click(object sender, EventArgs e)
        {this.Close();}

        #region radioButtons

        private void radioButtonWarrior_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButtonWarrior.Checked == true)
            {
                number = Convert.ToInt32(textBoxStr.Text);
                number += 10;
                textBoxStr.Text = number.ToString();
            }

            if (radioButtonWarrior.Checked == false)
            {
                number = Convert.ToInt32(textBoxStr.Text);
                number -= 10;
                textBoxStr.Text = number.ToString();
            }
        }

        private void radioButtonMage_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButtonMage.Checked == true)
            {
                number = Convert.ToInt32(textBoxInt.Text);
                number += 10;
                textBoxInt.Text = number.ToString();
            }

            if (radioButtonMage.Checked == false)
            {
                number = Convert.ToInt32(textBoxInt.Text);
                number -= 10;
                textBoxInt.Text = number.ToString();
            }
        }

        private void radioButtonThief_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButtonThief.Checked == true)
            {
                number = Convert.ToInt32(textBoxAgil.Text);
                number += 10;
                textBoxAgil.Text = number.ToString();
            }

            if (radioButtonThief.Checked == false)
            {
                number = Convert.ToInt32(textBoxAgil.Text);
                number -= 10;
                textBoxAgil.Text = number.ToString();
            }
        }

        #endregion

        #region buttonD

        private void buttonHpD_Click(object sender, EventArgs e)
        {
            number = Convert.ToInt32(textBoxRemain.Text);

            if (number < 20 && Convert.ToInt32(textBoxHp.Text) > 50)
            {
            number++;
            textBoxRemain.Text = number.ToString();

            number = Convert.ToInt32(textBoxHp.Text);
            number = number - 50;
            textBoxHp.Text = number.ToString();
            }

        }

        private void buttonMpD_Click(object sender, EventArgs e)
        {
            number = Convert.ToInt32(textBoxRemain.Text);

            if (number < 20 && Convert.ToInt32(textBoxMp.Text) > 50)
            {
                number++;
                textBoxRemain.Text = number.ToString();

                number = Convert.ToInt32(textBoxMp.Text);
                number = number - 50;
                textBoxMp.Text = number.ToString();
            }
        }

        private void buttonStrD_Click(object sender, EventArgs e)
        {
            number = Convert.ToInt32(textBoxRemain.Text);

            if (number < 20 && Convert.ToInt32(textBoxStr.Text) > 10)
            {
                number++;
                textBoxRemain.Text = number.ToString();

                number = Convert.ToInt32(textBoxStr.Text);
                number--;
                textBoxStr.Text = number.ToString();
            }
        }

        private void buttonAgilD_Click(object sender, EventArgs e)
        {
            number = Convert.ToInt32(textBoxRemain.Text);

            if (number < 20 && Convert.ToInt32(textBoxAgil.Text) > 10)
            {
                number++;
                textBoxRemain.Text = number.ToString();

                number = Convert.ToInt32(textBoxAgil.Text);
                number--;
                textBoxAgil.Text = number.ToString();
            }
        }

        private void buttonIntD_Click(object sender, EventArgs e)
        {
            number = Convert.ToInt32(textBoxRemain.Text);

            if (number < 20 && Convert.ToInt32(textBoxInt.Text) > 10)
            {
                number++;
                textBoxRemain.Text = number.ToString();

                number = Convert.ToInt32(textBoxInt.Text);
                number--;
                textBoxInt.Text = number.ToString();
            }
        }

        private void buttonWillD_Click(object sender, EventArgs e)
        {
            number = Convert.ToInt32(textBoxRemain.Text);

            if (number < 20 && Convert.ToInt32(textBoxWill.Text) > 10)
            {
                number++;
                textBoxRemain.Text = number.ToString();

                number = Convert.ToInt32(textBoxWill.Text);
                number--;
                textBoxWill.Text = number.ToString();
            }
        }

        private void buttonLuckD_Click(object sender, EventArgs e)
        {
            number = Convert.ToInt32(textBoxRemain.Text);

            if (number < 20 && Convert.ToInt32(textBoxLuck.Text) > 10)
            {
                number++;
                textBoxRemain.Text = number.ToString();

                number = Convert.ToInt32(textBoxLuck.Text);
                number--;
                textBoxLuck.Text = number.ToString();
            }
        }

        #endregion

        #region buttonU

        private void buttonHpU_Click(object sender, EventArgs e)
        {
            number = Convert.ToInt32(textBoxRemain.Text);

            if (number > 0)
            {
                number--;
                textBoxRemain.Text = number.ToString();

                number = Convert.ToInt32(textBoxHp.Text);
                number = number + 50;
                textBoxHp.Text = number.ToString();
            }
        }

        private void buttonMpU_Click(object sender, EventArgs e)
        {
            if (number > 0)
            {
                number--;
                textBoxRemain.Text = number.ToString();

                number = Convert.ToInt32(textBoxMp.Text);
                number = number + 50;
                textBoxMp.Text = number.ToString();
            }
        }

        private void buttonStrU_Click(object sender, EventArgs e)
        {
            if (number > 0)
            {
                number--;
                textBoxRemain.Text = number.ToString();

                number = Convert.ToInt32(textBoxStr.Text);
                number++;
                textBoxStr.Text = number.ToString();
            }
        }

        private void buttonAgilU_Click(object sender, EventArgs e)
        {
            if (number > 0)
            {
                number--;
                textBoxRemain.Text = number.ToString();

                number = Convert.ToInt32(textBoxAgil.Text);
                number++;
                textBoxAgil.Text = number.ToString();
            }
        }

        private void buttonIntU_Click(object sender, EventArgs e)
        {
            if (number > 0)
            {
                number--;
                textBoxRemain.Text = number.ToString();

                number = Convert.ToInt32(textBoxInt.Text);
                number++;
                textBoxInt.Text = number.ToString();
            }
        }

        private void buttonWillU_Click(object sender, EventArgs e)
        {
            if (number > 0)
            {
                number--;
                textBoxRemain.Text = number.ToString();

                number = Convert.ToInt32(textBoxWill.Text);
                number++;
                textBoxWill.Text = number.ToString();
            }
        }

        private void buttonLuckU_Click(object sender, EventArgs e)
        {
            if (number > 0)
            {
                number--;
                textBoxRemain.Text = number.ToString();

                number = Convert.ToInt32(textBoxLuck.Text);
                number++;
                textBoxLuck.Text = number.ToString();
            }
        }

        #endregion

        private void buttonReset_Click(object sender, EventArgs e)
        {
            radioButtonWarrior.Checked = true;
            textBoxName.Text = "";
            textBoxRemain.Text = "20";
            textBoxHp.Text = "100";
            textBoxMp.Text = "100";
            textBoxStr.Text = "20";
            textBoxAgil.Text = "10";
            textBoxInt.Text = "10";
            textBoxWill.Text = "10";
            textBoxLuck.Text = "10";

        }
    }
}

Instead of all this complicated code, why don't you use NumericUpDown boxes?

That was my first idea, but from what it would have required just as much to track which button was pushed as opposed to an event change which doesnt specify weather it was pushed up or down? So you're thinking having two ints stored for each NUD to check the before/after state on an event change would be easier?

The main problem being is I not only want the number to have limits, but I have multiple numbers. So if I add +5 to number 1, I want to be only able to add +15 to number 2. Thus I would have to check 7 numbers everytime one has an event change and count them. Then figure out how many was attempted to be added to the current number event change.

Ex: Someone tries to change number 3 from 10 to 20 via typing (as oppossed to the NUD click), I dont want this to be possible if there are only 5 increments remaining.

Granted this is a little on the bulky side, but I didn't think that this would be overcomplex until 1+1 started equaling 50 :(. I'll try the alternative.. though I am still extremely curious how this error occured.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.