频道直达 - 专题 - 新闻 - 技巧 - 组网 - 开发 - 安全 - web编程 - 图像 - 操作系统 - 数据库 - 教育 - 旅游 - 健康 - 时尚 - 驱动 - 软件 - 游戏 - 多媒体 - ERP - 讨论组

利用VC# 创作简单的多线程组件

来源:MSDN 作者: 出处:巧巧读书 2006-07-09 进入讨论组
上一页 1 2 3 4 5 6 下一页 
将用户输入传输到组件

  下一步是向 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);
URl收藏 http://www.qqread.com/csharp/k367144002.html进入讨论组讨论。
上一页 1 2 3 4 5 6 下一页 
收藏此文】【 】【打印】【关闭
相关图文阅读
频道图文推荐
健 康 咨 询
时 尚 咨 询
巧巧读书宗旨
相关专题
讨论组问题推荐
站内各频道最新更新文档
站内最新制作专题
热门关键字导读
Photoshop教 程照片处理 照片制作 PS快捷键 抠图
计 算 机 故 障XP系统修复
艺 术 与 设 计设计 流媒体 设计欣赏 边框
计 算 机 安 全ARP
站内频道文章精选
巧巧电脑频道编辑信箱  告诉我们您想看的专题或文章