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(); ButtonbnCtoF = new Button(); ButtonbnFtoC = 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(); ButtonbnCtoF = new Button(); ButtonbnFtoC = 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#的強
|