[英]How to call two methods from one button on a WPF form (C#)
我如何以一种允许我在表单上单击一个按钮时运行两种方法的方式进行编码。
单击 btnCalculate 时,运行 CalculateArea() 和CalculatePerimeter()。
我不太确定我在这里遗漏了什么,我是把东西放错了地方还是我遗漏了什么。
using System;
using System.Windows.Forms;
namespace PracticalExam1
{
public partial class frmMain : Form
{
public frmMain()
{
InitializeComponent();
Rectangle r = new Rectangle();
}
public class Rectangle
{
public float Length;
public float Width;
public float Area;
public float Perimeter;
}
public void frmMain_Load(object sender, EventArgs e)
{
}
public void CalculateArea(float length, float width, float area)
{
area = length * width;
txtArea.Text = Convert.ToString(area);
}
public void CalculatePerimeter(float length, float width, float perimeter)
{
perimeter = length * 2 + width * 2;
txtPerimeter.Text = Convert.ToString(perimeter);
}
private void btnCalculate_Click(object sender, EventArgs e)
{
CalculateArea();
CalculatePerimeter();
}
}
}
这就是我编写CalculateArea方法的方式...
private double CalculateArea(double h, double b)
{
return h * b;
}
double area = CalculateArea(12.5, 20);
您不需要传递area
参数...只需返回结果并将其分配给一个变量。 如果您理解我的意思,那么您也应该能够使用CalculatePerimeter
方法。
休息一下编码并修改 C# 方法和变量
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.