Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a test calculator - It's setup with 2 textboxes (the main one and a side one)
the main one is where it shows the users input for the first number (string number1 - yes sring ill convert it later) and then when the users selects a tool like Add, Subtract, Divide or Multiply, number1 travels to the smaller textbox and textBox1 clears so the user enters the main box while their first input stays in the small textBox2. (ask questions if im unclear)

basically when they select Add (or another tool) the number1 travels to textbox2 but textbox1 does not write number2 which the user enters

ill include some sample code:

C#
static public string number1 = String.Empty;
        static public string number2 = String.Empty; //declare empty strings
        static public string spare = String.Empty;

             //this adds 6 to the string number1 or number2 
        private void button6_Click(object sender, EventArgs e) //add 6 to string
        {
            if (num1)
            {
                number1 = number1 + "6";
                textBox1.Text = number1; 
            }
            if (!num1)
            {
                number2 = number2 + "6";
                textBox1.Text = number2;
            }
        }


//this is the Add button which sends number1 to spare & to textbox2
//then writes number2 into textBox1 for the user to intput numbers in
C#
private void button12_Click(object sender, EventArgs e) //add
        {
            add = true;
            spare = number1;
            number1 = String.Empty;
            textBox2.Text = spare;
            textBox1.Text = number2;
            num1 = false;
            num2 = true;
        }


but textBox1 doesnt write number2 when they enter it, it just stays blank
why?
Posted
Updated 27-Dec-11 1:52am
v2
Comments
uspatel 27-Dec-11 7:53am    
EDIT: pre tag added.

This is going to sound a little strange, but try dropping the concept of "number1" and "number2".

Instead, use Textbox1.Text as your "current input string area" and add to it in your button click event:
C#
private void button6_Click(object sender, EventArgs e) //add 6 to string
    {
    textBox1.Text += "6";
    }
Copy from TextBox1.Text to TextBox2.Text directly and ignore num1, num2 number1 and number2 completely.

In addition, I would make a small change:
Every control has a Tag property. For each of your number buttons, make the Tag that character.
1 button -> Tag == "1"
2 button -> Tag == "2"
...
Then have a single handler routine for all number buttons:
C#
private void NumberButton_Click(object sender, EventArgs e) //add 6 to string
    {
    Button b = sender as Button;
    if( b != null)
        {
        textBox1.Text += (string) b.Tag;
        }
    }

This way, you don't have to change the code in ten different places to alter your button handling.

And pretty please, don't use the Visual studio default names for things! button12 is not as obvious as butAdd when you come back to the code next week, and it can save a lot of time trying to work out which button does what.

[edit]Typos - OriginalGriff[/edit]
 
Share this answer
 
v2
Comments
Addy Tas 27-Dec-11 8:12am    
My 5.
Joel Whatley- 29-Dec-11 23:33pm    
Oke i'm a little confused but im trying it, im just not sure what the "b" is in your example code, is it a certain button? or do i write it for every button with a unique name?

im trying to teach myself windows form programming because at college we're doing console stuff and i'm far infront of anyone else, and i get bored at home

also any help to simplify explanations would be greatly helpful
Joel Whatley- 29-Dec-11 23:35pm    
I mean it works if I use that code for a single button but is there a way to make it work for all of them with that single method? instead of copying it for each button =)
thanks in advance
OriginalGriff 30-Dec-11 3:31am    
Yes - that is the whole idea!
When an event occurs, the source of the event is passed through as the sender parameter: hence the
Button b = sender as Button;
which converts the sender into the Button control which was clicked to raise the event. (Which answers your earlier question!)
All you have to do is in the designer change all the number buttons Click event to use the event handler - use the drop down in the events list.
Joel Whatley- 30-Dec-11 10:31am    
Ohhh :D Thank you so much =)
You are awesome!
C#
//add multiply or division etc click
private void button6_Click(object sender, EventArgs e) //add 6 to string
{
    if(textBox1.Text != string.Empty && textBox2.Text!=string.Empty)
    {
       switch(((Button)sender).Text)
       {
       case "+":
            textBox1.Text=""+(Covert.toDouble(textBox1.Text) +  Covert.toDouble(textBox2.Text));
            break;
        case "-":
            textBox1.Text=""+(Covert.toDouble(textBox1.Text) -  Covert.toDouble(textBox2.Text));
            break;
        case "/":
            textBox1.Text=""+(Covert.toDouble(textBox1.Text) /  Covert.toDouble(textBox2.Text));
            break;
        case "X":
            textBox1.Text=""+(Covert.toDouble(textBox1.Text) *  Covert.toDouble(textBox2.Text));
            break;
       }

    }
    else if(textBox2.Text==string.Empty && textBox1.Text != string.Empty)
    {
       textBox2.Text = textBox1.Text;
    }
 }



you can add case for add multiply or division or ..

regards,
 
Share this answer
 
v2

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