中国IT动力,最新最全的IT技术教程
最新100篇 | 推荐100篇 | 专题100篇 | 排行榜 | 搜索 | 在线API文档 | 网通镜像
首 页 | 程序开发 | 操作系统 | 软件应用 | 图形图象 | 网络应用 | 精文荟萃 | 教育认证 | 硬件维护 | 未整理篇 | 站长教程
ASP JS PHP工程 ASP.NET 网站建设 UML J2EESUN .NET VC VB VFP 网络维护 数据库 DB2 SQL2000 Oracle Mysql
服务器 Win2000 Office C DreamWeaver FireWorks Flash PhotoShop 上网宝典 CorelDraw 协议大全 网络安全 微软认证
硬件维护  CPU  主板  硬盘  内存  显卡  显示器  键盘鼠标  声卡音箱  打印机  机箱电源  BIOS  网卡  C#  Java  Delphi  vs.net2005
  当前位置:> 程序开发 > 编程语言 > .NET > C#
C#编程入门三部曲:第三步 增加响应用户事件代码
作者:未知 时间:2004-02-14 12:12 出处:eNet硅谷动力 责编:chinaitpower
              摘要:暂无
第三步 增加响应用户事件代码

还有最后一步就可以大功告成了,就是增加一个方法来捕捉按钮点击事件。这里就是指从摄氏到华氏的按钮点击代码:

private void bnCtoF_Click(Object sender, EventArgs e) {
double dTempCel = 0;
double dTempFah = 0;
try { dTempCel = tTempCel.Text.ToDouble(); }
catch(Exception) {
tTempCel.Clear();
tTempFah.Clear();
return;
}
dTempFah = 1.8*dTempCel+32;
tTempFah.Text = dTempFah.ToString();
tTempFah.Focus();
tTempFah.SelectionStart = 0;
tTempFah.SelectionLength = 0;
tTempCel.Focus();
tTempCel.SelectionStart = 0;
tTempCel.SelectionLength = 0;
}

第四行到第八行(也就是try 区中的一切)取回Celsius(摄氏)文本框中的数值。如果它是一个双字节数,就将其存储在dTempCel中,否则就清除两个文本框并退出。接着,用存储在dTempCel

中的值,我们用第9 行中的公式将相同的温度存储在Fahrenheit中。将这个新的数值在 Fahrenheit(华氏)文本框中显示, 然后将光标放在每个文本框中,以便将指针设置到开头。(如果不将指针设置到开头,我们就会看到一个长长的数字的结尾,要看开头就必须滚动鼠标)。

以下是Fahrenheit按钮的代码,它将完成同样的任务,只不过是相反的处理:

private void bnFtoC_Click(Object sender, EventArgs e) {
double dTempCel = 0;
double dTempFah = 0;
try { dTempFah = tTempFah.Text.ToDouble(); }
catch(Exception) {
tTempCel.Clear();
tTempFah.Clear();
return;
}
dTempCel = (dTempFah-32)/1.8;
tTempCel.Text = dTempCel.ToString();
tTempCel.Focus();
tTempCel.SelectionStart = 0;
tTempCel.SelectionLength = 0;
tTempFah.Focus();
tTempFah.SelectionStart = 0;
tTempFah.SelectionLength = 0;
}

接着,我们需要将适当的点击事件捕捉方法与按钮的 Click事件联系起来。要完成这一步,我们将以下两行放在类的构造器中:

bnCtoF.Click += new EventHandler(this.bnCtoF_Click);
bnFtoC.Click += new EventHandler(this.bnFtoC_Click);

最后,请看完整的代码:

using System;
using System.WinForms;

public class TempConverter : System.WinForms.Form {

Label lTempFah = new Label();
Label lTempCel = new Label();
TextBox tTempFah = new TextBox();
TextBox tTempCel = new TextBox();
Button bnCtoF = new Button();
Button bnFtoC = new Button();

public TempConverter() {
this.SetSize(180,90);
this.BorderStyle = FormBorderStyle.FixedDialog;
this.Text = " +C -> +F / +F -> +C ";
this.StartPosition = FormStartPosition.CenterScreen;
this.HelpButton = false;
this.MaximizeBox = false;
tTempCel.TabIndex = 0;
tTempCel.SetSize(50,25);
tTempCel.SetLocation(13,5);
lTempCel.TabStop = false;
lTempCel.Text = "C";
lTempCel.SetSize(25, 25);
lTempCel.SetLocation(65,5);
tTempFah.TabIndex = 1;
tTempFah.SetSize(50,25);
tTempFah.SetLocation(90,5);
lTempFah.TabStop = false;
lTempFah.Text = "F";
lTempFah.SetSize(25,25);
lTempFah.SetLocation(142,5);
bnCtoF.TabIndex = 2;
bnCtoF.Text = "C to F";
bnCtoF.SetSize(70,25);
bnCtoF.SetLocation(13,35);
bnCtoF.Click += new EventHandler(this.bnCtoF_Click);
bnFtoC.TabIndex = 3;
bnFtoC.Text = "F to C";
bnFtoC.SetSize(70,25);
bnFtoC.SetLocation(90,35);
bnFtoC.Click += new EventHandler(this.bnFtoC_Click);
this.Controls.Add(tTempCel);
this.Controls.Add(lTempCel);
this.Controls.Add(tTempFah);
this.Controls.Add(lTempFah);
this.Controls.Add(bnCtoF);
this.Controls.Add(bnFtoC);
//= new Control [] { tTempCel, lTempCel, tTempFah, lTempFah, bnCtoF, bnFtoC };
}

public static void Main() {
Application.Run( new TempConverter() );
}

private void bnCtoF_Click(Object sender, EventArgs e) {
double dTempCel = 0;
double dTempFah = 0;
try { dTempCel = tTempCel.Text.ToDouble(); }
catch(Exception) {
tTempCel.Clear();
tTempFah.Clear();
return;
}
dTempFah = 1.8*dTempCel+32;
tTempFah.Text = dTempFah.ToString();
tTempFah.Focus();
tTempFah.SelectionStart = 0;
tTempFah.SelectionLength = 0;
tTempCel.Focus();
tTempCel.SelectionStart = 0;
tTempCel.SelectionLength = 0;
}

private void bnFtoC_Click(Object sender, EventArgs e) {
double dTempCel = 0;
double dTempFah = 0;
try { dTempFah = tTempFah.Text.ToDouble(); }
catch(Exception) {
tTempCel.Clear();
tTempFah.Clear();
return;
}
dTempCel = (dTempFah-32)/1.8;
tTempCel.Text = dTempCel.ToString();
tTempCel.Focus();
tTempCel.SelectionStart = 0;
tTempCel.SelectionLength = 0;
tTempFah.Focus();
tTempFah.SelectionStart = 0;
tTempFah.SelectionLength = 0;
}
}

结 语

到此为止,你看到了如何用C#进行编程的一个完整过程。这个例子虽然很简单,但是麻雀虽小,五脏俱全,理解其中的原理后,就可以大显身手,充分发挥C#的强大功能了。
关闭本页
 
首页 | 投资与合作 | 服务条款 | 隐私政策 | 收藏本站 | 设为首页 | 新用户注册 | 免责声明 | 使用帮助
Copyright ©2005-2008 chinaitpower.com All rights reserved. www.chinaitpower.com 版权所有