我几乎不学习如何做GUI。所以,我是这个新手。我在visual stuidos中使用windows form应用程序。
我需要帮助将我输入的所有三个输入添加到文本框中。
旁注 - MessageBox.Show(sum)
下方有一条红线
这是我的代码:
public partial class Form1 : Form
{
double principal;
double rate;
double terms;
double sum;
public Form1()
{
InitializeComponent();
}
private void inputTextBoxPrincipal_TextChanged(object sender, EventArgs e)
{
principal = Convert.ToDouble(inputTextBoxPrincipal.Text);
}
private void inputTextBoxRate_TextChanged(object sender, EventArgs e)
{
rate = Convert.ToDouble(inputTextBoxRate.Text);
}
private void inputTextBoxTerms_TextChanged(object sender, EventArgs e)
{
terms = Convert.ToDouble(inputTextBoxTerms);
}
private double add()
{
sum = principal + rate + terms;
return sum;
}
private void clickButtonEnter_Click(object sender, EventArgs e)
{
MessageBox.Show(sum);
}
}
答案 0 :(得分:1)
更改clickButtonEnter_Click
事件,如下所示。
private void clickButtonEnter_Click(object sender, EventArgs e)
{
double sum = add();
MessageBox.Show(sum.ToString());
}
编辑:您也不需要这些TextChanged
个事件。以下就好了。
public partial class Form1 : Form
{
double principal;
double rate;
double terms;
double sum;
public Form1()
{
InitializeComponent();
}
private double add()
{
principal = Convert.ToDouble(inputTextBoxPrincipal.Text);
rate = Convert.ToDouble(inputTextBoxRate.Text);
terms = Convert.ToDouble(inputTextBoxTerms);
sum = principal + rate + terms;
return sum;
}
private void clickButtonEnter_Click(object sender, EventArgs e)
{
double sum = add();
MessageBox.Show(sum.ToString());
}
}