创建一个视窗应用程序主要包括以下的基本步骤:创建适合的表单,在表单上添加control,最后添加代码。完成上述过程所需用到的C# 以及 .NET framework我们可以在System.WinForms namespace中找到。
第一步,创建表单。
我们以 class System.WinForms 为起源,创建一个class,然后初始化属性。本文举例中,Class的定义起始如下
| public class TempConverter : System.WinForms.Form { . . . } |
下面是我们想要的主窗口式样
大小为180*90像素
不能随意修改视窗大小。
标题显示为 °C->°F / °F->°C
表单出现在屏幕中央
我们不需要"帮助"键(我们编制的应用程序过于简便,以至不需要此类帮助)
我们不需要让用户有扩大该程序视窗范围的权限(因为在给定尺寸里,什么都清晰可见)
通过设定TempConverter对象的属性值初始化表单。属性值的设定有两种方法:
一、使用方法设定属性值
二、通过属性变量直接设定。
以下代码。如果您想知道更多的有关于WinForms class的属性和方法的知识,则可以参阅.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; |
通过上述步骤,我们可以把所有的代码连接在一起,这样我们就能很方便的编辑、运行程序观看表单的外观。为此,我们要用到class 定义,要创建一个构造器(该构造器包含了上面所提到的代码并会初始化主视窗的外观),然后还需要建立一个main方法。具体建立步骤如下:
| 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.dll 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,以便当点击Tab键 时,可以显示focus并未被设置 · 设置TabIndex =X表示在点击TAB键x次后,focus会被设置在确定的控件上。 · text属性即是控件显现出来的文本。 · Controls.Add()将 控件添加到了表单上(快捷添加控件的方法是:.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; } |
第三行至第八行的指令将会试着将数值收集到Celsius文本框里。如果是double数值那么我们将会把数值存于dTempCel单元中,否则我们将清除两个文本框的内容的同时退出。下一步,利用dTempCel中的值,使用第九行的公式来存储华氏温度,结果显示在华氏文本框中。
重复相同的步骤来完成华氏button的代码:
| 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; } |
最后一步则是将其结合起来。具体步骤参照如下:
| 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); file://= 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; } } |









