我试图让这段代码工作,所以当数字放在长度和宽度文本框中时,它们会计算得出区域和周长。这是一个课堂作业,我的老师告诉我,我没有在输出文本框中正确显示计算结果。如果有人告诉我如何编辑我的代码以便它能正确运行,我会很高兴。谢谢。
private void btnCalculatE_Click(object sender, EventArgs e)
{
double Length;
double Width;
double txtArea;
double txtPerimeter;
Length = Convert.ToDouble(txtLength.Text);
Width = Convert.ToDouble(txtWidth.Text);
txtArea = Length * Width;
txtPerimeter = 2 * (Length + Width);
txtArea = Convert.ToDouble(txtArea);
txtPerimeter = Convert.ToDouble(txtPerimeter);
txtLength.Focus();
}
private void btnExiT_Click(object sender, EventArgs e)
{
this.Close();
}
答案 0 :(得分:0)
我假设您使用TextBox.Text
,并且您有2个名为textArea
和textPerimeter
的输出文本框。
private void btnCalculatE_Click(object sender, EventArgs e)
{
double Length;
double Width;
double textAreaDouble;
double textPerimeterDouble;
//Length & Width now are double
Length = Convert.ToDouble(txtLength.Text);
Width = Convert.ToDouble(txtWidth.Text);
txtAreaDouble = Convert.Length * Width;
txtPerimeterDouble = 2 * (Length + Width);
//Set new values in your textboxes
textArea.Text = textAreaDouble.ToString();
textPerimeter.Text = textPerimeterDouble.ToString();
txtLength.Focus();
}
答案 1 :(得分:0)
如果您将显示 Area 和 Perimeter 的文本框命名为txtArea
和txtPerimeter
,那么这就是您需要的代码:
private void btnCalculatE_Click(object sender, EventArgs e)
{
double Length;
double Width;
Length = Convert.ToDouble(txtLength.Text);
Width = Convert.ToDouble(txtWidth.Text);
txtArea.Text = Convert.ToString(Length * Width);
txtPerimeter.Text = Convert.ToString(2 * (Length + Width));
//The following has nothing to do with calculating the result.
//You may leave it if you intend to set focus to the *Length* textbox after calculating.
txtLength.Focus();
}
希望有所帮助。