将用户输入传输到组件
下一步是向 frmCalculations 添加代码,以接收用户输入,以及从 Calculator 组件接收值和向它传输值。
实现 frmCalculations 的前端功能
在代码编辑器中打开 frmCalculations。
找到 public class frmCalculations 语句。紧接着 { 的下方键入:
| Calculator Calculator1; |
找到构造函数。紧接着 } 之前,添加以下行:
| // Creates a new instance of Calculator. Calculator1 = new Calculator(); |
在设计器中单击每个按钮,为每个控件的单击事件处理程序生成代码大纲,并添加代码以创建这些处理程序。
完成后,单击事件处理程序应该类似于以下形式:
| private void btnFactorial1_Click(object sender, System.EventArgs e) // Passes the value typed in the txtValue to Calculator.varFact1. { Calculator1.varFact1 = int.Parse(txtValue.Text); // Disables the btnFactorial1 until this calculation is complete. btnFactorial1.Enabled = false; Calculator1.Factorial(); } private void btnFactorial2_Click(object sender, System.EventArgs e) { Calculator1.varFact2 = int.Parse(txtValue.Text); btnFactorial2.Enabled = false; Calculator1.FactorialMinusOne(); } private void btnAddTwo_Click(object sender, System.EventArgs e) { Calculator1.varAddTwo = int.Parse(txtValue.Text); btnAddTwo.Enabled = false; Calculator1.AddTwo(); } private void btnRunLoops_Click(object sender, System.EventArgs e) { Calculator1.varLoopValue = int.Parse(txtValue.Text); btnRunLoops.Enabled = false; // Lets the user know that a loop is running lblRunLoops.Text = "Looping"; Calculator1.RunALoop(); } |
在上一步添加的代码的下方,键入以下代码以处理窗体将从 Calculator1 接收的事件:
| protected void FactorialHandler(double Value, double Calculations) // Displays the returned value in the appropriate label. { lblFactorial1.Text = Value.ToString(); // Re-enables the button so it can be used again. btnFactorial1.Enabled = true; // Updates the label that displays the total calculations performed lblTotalCalculations.Text = "TotalCalculations are " + Calculations.ToString(); } protected void FactorialMinusHandler(double Value, double Calculations) { lblFactorial2.Text = Value.ToString(); btnFactorial2.Enabled = true; lblTotalCalculations.Text = "TotalCalculations are " + Calculations.ToString(); } protected void AddTwoHandler(int Value, double Calculations) { lblAddTwo.Text = Value.ToString(); btnAddTwo.Enabled = true; lblTotalCalculations.Text = "TotalCalculations are " + Calculations.ToString(); } protected void LoopDoneHandler(double Calculations, int Count) { btnRunLoops.Enabled = true; lblRunLoops.Text = Count.ToString(); lblTotalCalculations.Text = "TotalCalculations are " + Calculations.ToString(); } |
在 frmCalculations 的构造函数中,紧挨在 } 之前添加下列代码,以处理窗体将从 Calculator1 接收的自定义事件:
| Calculator1.FactorialComplete += new Calculator.FactorialCompleteHandler(this.FactorialHandler); Calculator1.FactorialMinusOneComplete += new Calculator.FactorialCompleteHandler(this.FactorialMinusHandler); Calculator1.AddTwoComplete += new Calculator.AddTwoCompleteHandler(this.AddTwoHandler); Calculator1.LoopComplete += new Calculator.LoopCompleteHandler(this.LoopDoneHandler); |
相关图文阅读
频道图文推荐
健 康 咨 询
时 尚 咨 询
相关专题
- C#变得越来越臃肿是不可避免的? (7次浏览)
- C#与EXCEL的数据交互(一) (5次浏览)
- C# 3.0新特性之扩展方法 (1次浏览)
- C#调用QTP自动化对象模型的实例 (0次浏览)
- 利用 C# 实现任务栏通知窗口 (0次浏览)
- 深入C#学习系列之不可小瞧的using关键字 (0次浏览)
- 如何用.NET技术在线生成网站LOGO (0次浏览)
- C#实现遗传算法 模拟花朵的进化 (0次浏览)
- c#里的运算符重载 (0次浏览)
- QTP调用自己开发的.net类库 (0次浏览)



