public int varAddTwo;
public int varFact1;
public int varFact2;
public int varLoopValue;
public double varTotalCalculations = 0;
向 Calculator 组件添加方法和事件
为事件声明委托,组件将使用这些事件向窗体传递值。
注意 尽管您将声明 4 个事件,但由于其中的两个事件将具有相同的签名,因此只需要创建 3 个委托。
紧接着上一步输入的变量声明的下方,键入下列代码:
// This delegate will be invoked with two of your events.
public delegate void FactorialCompleteHandler(double Factorial, double TotalCalculations);
public delegate void AddTwoCompleteHandler(int Result, double TotalCalculations);
public delegate void LoopCompleteHandler(double TotalCalculations, int Counter);
声明组件将用来与应用程序进行通信的事件。为实现此目的,紧接着上一步输入的代码的下方,添加下列代码。
public event FactorialCompleteHandler FactorialComplete;
public event FactorialCompleteHandler FactorialMinusOneComplete;
public event AddTwoCompleteHandler AddTwoComplete;
public event LoopCompleteHandler LoopComplete;
紧接着上一步键入的代码的下方,键入下列代码:
// This method will calculate the value of a number minus 1 factorial
// (varFact2-1!).
public void FactorialMinusOne()
{
double varTotalAsOfNow = 0;
double varResult = 1;
// Performs a factorial calculation on varFact2 - 1.
for (int varX = 1; varX <= varFact2 - 1; varX++)
{
varResult *= varX;
// Increments varTotalCalculations and keeps track of the current
// total as of this instant.
varTotalCalculations += 1;
varTotalAsOfNow = varTotalCalculations;
}
// Signals that the method has completed, and communicates the
// result and a value of total calculations performed up to this
// point.
FactorialMinusOneComplete(varResult, varTotalAsOfNow);
}
// This method will calculate the value of a number factorial.
// (varFact1!)
public void Factorial()
{
double varResult = 1;
double varTotalAsOfNow = 0;
for (int varX = 1; varX <= varFact1; varX++)
{
varResult *= varX;
varTotalCalculations += 1;
varTotalAsOfNow = varTotalCalculations;
}
FactorialComplete(varResult, varTotalAsOfNow);
}
// This method will add two to a number (varAddTwo+2).
public void AddTwo()
{
double varTotalAsOfNow = 0;
int varResult = varAddTwo + 2;
varTotalCalculations += 1;
varTotalAsOfNow = varTotalCalculations;
AddTwoComplete(varResult, varTotalAsOfNow);
}
// This method will run a loop with a nested loop varLoopValue times.
public void RunALoop()
{
int varX;
double varTotalAsOfNow = 0;
for (varX = 1; varX <= varLoopValue; varX++)
{
// This nested loop is added solely for the purpose of slowing down
// the program and creating a processor-intensive application.
for (int varY = 1; varY <= 500; varY++)
{
varTotalCalculations += 1;
varTotalAsOfNow = varTotalCalculations;
}
}
LoopComplete(varTotalAsOfNow, varLoopValue);
}
将用户输入传输到组件
// 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 unt
copyright 2004-2009 www.qqcf.com all rights reserved. DIV+CSS架构 本站为学习性免费网站,没有任何商业性质,甚至连一个外部广告都没有投放 所有文章均来源于互联网,如发现有侵权,请来信告知,本站将会以最快的速度删除和改正,E-Mail:cftop@qq.com