Home > Mobile >  getting value of a button to calculate with it in windows forms (c#)
getting value of a button to calculate with it in windows forms (c#)

Time:08-17

I'm actually new in windows forms, got a tiny bit of c# experience and wanted to program a calculator. I didn't want to watch a whole tutorial to see how it works (don't want to be a script kid). I'm not a native English speaker so sorry for that mistakes in the text.

So first of all I started with the customization of my Windows Form Calculator, so far so good. It includes 9 normal Buttons and with the numbers from 1 to 9 and a button which says "calculate". I also got a textbox where the result should be shown.

I thought that it might be good to set every button with an integer from 1 to 9 (like button 1 gets the value 1, button 2 gets 2....)

But then I was stuck. I tried to create a method where the whole progress should be in but I cant even call the button values...

I'm not sure if I'm just dumb or can't see the problem right now. I created a calculator in the normal command prompt with vs and it worked well. It definitely wasn't that much of a problem so I thought it might be cool to try a little harder (looks like its to hard for me).

Every help is useful but please don't be rude or something because that's "to high for me" or something like that. If you have other suggestions for some easier projects let me know.

CodePudding user response:

I'm new in windows forms too, but I think I can help you Did you try using for buttons .Text?

for example:

button_zero.Text //it will work if in your button number "0"

and it must returns text in your button

Sorry if that's not what you're going to disappear and for my english :)))

CodePudding user response:

Reproduce the problem: Make a simple calculator (2,3,4. Calculate the addition, subtraction and multiplication of these 3 numbers).

UI page:

enter image description here

Calculate the effect:

enter image description here

implementation code:

   public Form1()
    {
        InitializeComponent();
    }

    double a = 0;
    double b = 0;
    bool c = false;
    string d;

    private void button1_Click(object sender, EventArgs e)
    {
        if (c == true)
        {
            textBox1.Text = "";
            c = false;
        }

        textBox1.Text  = "2";

    }

    private void button2_Click(object sender, EventArgs e)
    {
        if (c == true)
        {
            textBox1.Text = "";
            c = false;
        }

        textBox1.Text  = "3";

    }

    private void button3_Click(object sender, EventArgs e)
    {
        if (c == true)
        {
            textBox1.Text = "";
            c = false;
        }

        textBox1.Text  = "4";
    }

    private void button4_Click(object sender, EventArgs e)
    {
        c = true;
        b = double.Parse(textBox1.Text);
        d = " ";

    }

    private void button5_Click(object sender, EventArgs e)
    {
        c = true;
        b = double.Parse(textBox1.Text);
        d = "-";
    }

    private void button6_Click(object sender, EventArgs e)
    {
        c = true;
        b = double.Parse(textBox1.Text);
        d = "*";
    }

    private void button7_Click(object sender, EventArgs e)
    {
        switch (d)
        {
            case " ":
                a = b   double.Parse(textBox1.Text);
                break;
            case "-":
                a = b - double.Parse(textBox1.Text);
                break;
            case "*":
                a = b * double.Parse(textBox1.Text);
                break;

        }

        textBox1.Text = a   "";

        c = true;
    }

    private void button8_Click(object sender, EventArgs e)
    {
        textBox1.Text = "";
    }
}

Clicking the number button makes the text box take on the value. Click the operator button to operate. Click the "=" button to get the operation result. Click the "empty" button to empty the value of the text box. Hope it helps you.

  • Related