N
N
NikitaSova2020-07-29 12:29:17
C++ / C#
NikitaSova, 2020-07-29 12:29:17

How to add numbers from TextBox?

Good day to all.
I am a beginner programmer and wanted to make a calculator in WinForms. Today I decided to do the addition, but when I wrote the code, I ran into a problem: instead of the result of the addition, it writes zero in the texbox. And no matter what I do, it still writes zero. Please help find the problem.
Here is the code (sorry for the unkempt look):

using System;
using System.Windows.Forms;
class MyButtonClass : Form
{

    private Button One, Plus, Equally;
    private TextBox FirstText, SecondText, ThirdText, FourthText;
    string s;
    double a;
    double b;
    double c;

    public MyButtonClass()
    {
        One = new Button();
        One.Text = "1";
        One.Top = 200;
        One.Left = 0;
        One.Height = 50;
        One.Width = 70;
        One.Click += new System.EventHandler(OneButton);
        this.Controls.Add(One);
        //Declare, properties and call a button

        Plus = new Button();
        Plus.Text = "+";
        Plus.Top = 200;
        Plus.Left = 70;
        Plus.Height = 50;
        Plus.Width = 70;
        Plus.Click += new System.EventHandler(PlusButton);
        this.Controls.Add(Plus);
        //Declare, properties and call a button

        Equally = new Button();
        Equally.Text = "=";
        Equally.Top = 200;
        Equally.Left = 140;
        Equally.Height = 50;
        Equally.Width = 70;
        Equally.Click += new System.EventHandler(EquallyButton);
        this.Controls.Add(Equally);
        //Declare, properties and call a button

        FirstText = new TextBox();
        FirstText.Top = 10;
        FirstText.Left = 0;
        FirstText.Height = 50;
        FirstText.Width = 210;
        this.Controls.Add(FirstText);
        //Declare, properties and call a TextBox

        SecondText = new TextBox();
        SecondText.Top = 60;
        SecondText.Left = 0;
        SecondText.Height = 50;
        SecondText.Width = 210;
        this.Controls.Add(SecondText);
        //Declare, properties and call a TextBox

        ThirdText = new TextBox();
        ThirdText.Top = 110;
        ThirdText.Left = 0;
        ThirdText.Height = 50;
        ThirdText.Width = 210;
        this.Controls.Add(ThirdText);
        //Declare, properties and call a TextBox

        FourthText = new TextBox();
        FourthText.Top = 160;
        FourthText.Left = 0;
        FourthText.Height = 50;
        FourthText.Width = 210;
        this.Controls.Add(FourthText);
        //Declare, properties and call a TextBox
    }
    static void Main()
    {
        Application.Run(new MyButtonClass());
        //starting objects of class MyButtonClass
    }
    private void FirstText_TextChanged(object sender, EventArgs e)
    {
        s = FirstText.Text;
        a = Convert.ToDouble(s);
    }
    private void ThirdText_TextChanged(object sender, EventArgs e)
    {
        s = ThirdText.Text;
        b = Convert.ToDouble(s);
    }
    void OneButton(object sender, EventArgs e)
    {
        if (SecondText.Text == "")
        {
            FirstText.Text += "1";
        }
        else if (SecondText.Text == "+")
        {
            ThirdText.Text += "1";
        }
    }
    void PlusButton(object sender, EventArgs e)
    {
        SecondText.Text = "+";
    }
    void EquallyButton(object sender, EventArgs e)
    {
        c = a + b;
        FourthText.Text = Convert.ToString(c);
    }
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
#
#, 2020-07-29
@NikitaSova

For example, what does a button +on click do for you?

Plus = new Button();
        Plus.Text = "+";
        Plus.Top = 200;
        Plus.Left = 70;
        Plus.Height = 50;
        Plus.Width = 70;
        Plus.Click += new System.EventHandler(PlusButton);
        this.Controls.Add(Plus);

and
void PlusButton(object sender, EventArgs e)
    {
        SecondText.Text = "+";
    }

assigns a character +to some TextBox SecondText;
.. where is the actual addition?
.. and how to tell you in such situations?

D
Dmitry Pavlov, 2020-07-29
@Stalker31

Example for int:

int n,k,res;
n=int.Parse(textbox1.Text);
k=int.Parse(textbox2.Text);
res=n+k;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question