我正在写一个c#windows表单代码

时间:2018-06-10 21:33:17

标签: c#

我正在编写一个c#windows表单代码 从button1和button2获取数字并将它们一起添加到文本框中但是编译器在convert.toint32(textbox3.text)语句中进行了争论 并且它增加了two variablethree 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();
     } 

`

2 个答案:

答案 0 :(得分:0)

更改

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
}

因为它没有任何意义。

答案 1 :(得分:0)

您的变量属于类,可以在构造函数中初始化它们。这可以通过多种方式完成,但您需要检查文本框是否有值,然后尝试转换并添加它。

drop schema humman;

create schema humman;

CREATE TABLE humman.father (
id int not null auto_increment,
firstname varchar(200) not null,
primary key(id)
);

create table humman.mather(
id int not null auto_increment,
FirstName varchar(200),
primary key(id)
);

CREATE TABLE humman.child (
id int not null auto_increment,
firstname varchar(200) not null,
primary key(id)
);

use `humman`;

alter table humman.child 
ADD `parentId` int ,
ADD `motherId` int,
ADD  foreign key (`parentId`) references father(`id`),
ADD foreign key (`motherId`) references mother(`id`);