c#是微软.net架构的主力开发语言,它功能广泛而强大,web开发人员应该毫不犹豫地
拥抱它。本文就通过一个摄氏温度与华氏温度间相互转换的例子对c#
的gui编程进行介绍,旨在带领你快速步入c#之强大与神奇的编程世界。
准备条件
要理解本文所举例程,首先要对c#和面向对象的编程有一个基本的了解。关于 c#的基本
知识,请参阅 c#入门
这篇文章。要编译并运行举例的应用程序,就需 下载
.net framework sdk,它当前的一个版本是beta 1。
作为程序开发者,我们都知道创建一个典型的基于 windows 的应用程序应该包含以下这
些基本步骤:创建一个适当的表单;向表单中增加控件;最后增加响应用户事件的代码
。
c#和 .net 框架出现后,完成这些步骤所需要的工具都可以在system.winforms 名子空
间中找到。
第一步 创建一个表单
这非常简单,只需要创建一个从 system.winforms.form 类中衍生出来的类,并对适当
的属性进行初始化就可以。在我们的例子中,类定义是这样开始的:
public class tempconverter : system.winforms.form {
.
.
.
}
以下是我们希望的主窗口(表单)视图:
我们希望表单具备如下特征:
- 窗口大小为 180乘 90象素
- 不给用户改变窗口大小的能力
- 标题栏中显示的标题是 +c -> +f / +f -> +c
- 初始状态下表单显示在屏幕的中心
- 不想要帮助按钮(应用程序太简单,不需要帮助按钮)
- 不给用户提供将应用程序最大化的能力
(因为在给定的窗口尺寸内,一切都是可视的,因此不需要最大化)
将表单初始化成给定的规格涉及到对 tempconverter 对象的某些属性进行设置。有些属
性有改变值的方法,而其它属性则要通过更新适当的实例变量来直接修改。下面是有关
代码。如果想要得到关于winforms
类的属性和方法的更多信息,那么 .net framework sdk 所提供的文档可以算是一个很
好的参考资料。
this.setsize(180,90);
this.borderstyle = formborderstyle.fixeddialog;
this.text = " +c -> +f / +f -> +c ";
this.startposition = formstartposition.centerscreen;
this.helpbutton = false;
this.maximizebox = false;
现在把这些代码放在一起进行编译和运行,看看表单运行后是什么样子。这里要使用类
定义,创建一个构造器(其中要包含以上的代码来初始化主窗口的外观),并且要创建
一个主方法来创建类的一个例示。以下是完成这一工作的代码:
public class tempconverter : system.winforms.form {
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;
}
public static void main() {
application.run( new tempconverter() );
}
}
以上只有 main() 方法所在行是新的代码。
application.run(new tempconverter());
上面这一行的意思是用新表单来启动应用程序。
假设源文件叫做tempconverter.cs,那么执行以下的命令编译代码:
csc /r:system.dll /r:microsoft.win32.interop.dll /r:system.winforms.dl
l tempconverter.cs
这里不再详细讲解编译命令,因为当visual studio .net可用时,就不必要发出命令行
的编译命令了。
第二步 向表单中增加控件
接着的一步是向表单中增加控件。我们为每个控件创建一个实例变量,对这些新实例变
量进行初始化,最后把每个控件都放在表单中。这里是增加了控件之后表单的样子,以
及更新过的代码:
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);
bnftoc.tabindex = 3;
bnftoc.text = "+f to +c ";
bnftoc.setsize(70,25);
bnftoc.setlocation(90,35);
this.controls.add(ttempcel);
this.controls.add(ltempcel);
this.controls.add(ttempfah);
this.controls.add(ltempfah);
this.controls.add(bnctof);
this.controls.add(bnftoc);
}
以上代码首先创建两个标签、两个文本框和两个按钮,然后对每个控件进行初始化并将
其加入表单中。具体的含义如下:
- setsize() 初始化控件的尺寸
- setlocation() 初始化表单中控件的位置
- 设置控件的tabstop 属性为false表示这个控件从不被聚焦
- 设置tabindex 为 x 表示当敲击tab键x次后聚焦此控件
- 控件的text 属性表示显示在其上的文字信息
- this.controls.add() 表示在表单上放置一个控件,要快速地添加每个控件,可以这
么书写:this.controls = new
control[] { ttempcel, ltempcel, ttempfar?.}
第三步 增加响应用户事件代码
还有最后一步就可以大功告成了,就是增加一个方法来捕捉按钮点击事件。这里就是指
从摄氏到华氏的按钮点击代码:
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#的强大功能了。
