@NikitaSova

Как сложить числа из TextBox?

Добрый день всем.
Я начинающий программист и захотел сделать калькулятор в WinForms. Сегодня решил сделать сложение но когда писал код столкнулся с проблемой: вместо результата сложения в тексбоксе пишет ноль. И что бы я ни делал всё равно пишет ноль. Пожалуйста помогите найти проблему.
Вот код (простите за непричёсанный вид):
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);
    }
}
  • Вопрос задан
  • 1314 просмотров
Решения вопроса 1
mindtester
@mindtester Куратор тега C#
http://iczin.su/hexagram_48
для примера, что у вас делает кнопка + по клику ?
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);

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

присваивает символ + в некий TextBox SecondText;
.. а где собственно сложение?
.. и как вам подсказывать при таких раскладах?
Ответ написан
Комментировать
Пригласить эксперта
Ответы на вопрос 1
Пример для int:
int n,k,res;
n=int.Parse(textbox1.Text);
k=int.Parse(textbox2.Text);
res=n+k;
Ответ написан
Комментировать
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы