首页 > 解决方案 > 我正在编写 ac# windows 窗体代码

问题描述

我正在编写 ac# windows 窗体代码以从 button1 和 button2 获取数字并将它们一起添加到文本框中,但编译器在 convert.toint32(textbox3.text) 语句上争论,它还two variable增加three variable了我保持不变但增加了文本框的价值,我需要一个解决方案吗?

  int Three = 0;
    int Two   = 0;
    //int one   = 0;
    int sum   = 0;
   // int sum   = 0;
    //int dec   = 0;

    public Form1()
    {

        InitializeComponent();

    }

    private void Form1_Load(object sender, EventArgs e)
    {
       // MessageBox.Show("Enter the teams` name");

    }
    private void button1_Click(object sender, EventArgs e)
    {

        //Three += 3;
        //textBox3.Text = sum.ToString();
        Three += 3;
        sum = Convert.ToInt32(textBox3.Text) + Three;
        textBox3.Text = sum.ToString();


    }

    private void button2_Click(object sender, EventArgs e)
    {
        Two += 2;
        sum = Two + Convert.ToInt32(textBox3.Text) + Three;
        textBox3.Text =Convert.ToInt32(textBox3.Text) + Two.ToString();




    }

    private void textBox3_TextChanged(object sender, EventArgs e)
    {

        textBox3.Text = 0.ToString();
     } 

`

标签: c#

解决方案


改变

sum = Convert.ToInt32(textBox3.Text) + Three;

sum = Convert.ToInt32(textBox3.Text == "" ? "0" : textBox3.Text) + 3;

另外,删除

private void textBox3_TextChanged(object sender, EventArgs e)
{
    textBox3.Text = 0.ToString(); // this
}

因为这没有任何意义。


推荐阅读