C# 入門指南 - EncryptGUI

我們規劃的 GUI 外觀如下圖




這需要三個 Label 、兩個 TextBox 跟七個 Button ,這些都設定成 EncryptGUI 的屬性 (property) ,然後建構子 (constructor) 中建立所有元件
using System;
using System.Windows.Forms;
using System.Drawing;

class EncryptGUI : Form {
    private Label inputLabel;
    private Label outputLabel;
    private TextBox inputField;
    private TextBox outputField;
    private Button newButton;
    private Button loadButton;
    private Button saveButton;
    private Button encodeButton;
    private Button decodeButton;
    private Button clearButton;
    private Button copyButton;
    private Label displayLabel;
    
    public EncryptGUI() {
        this.inputLabel = new Label();
        this.outputLabel = new Label();
        this.inputField = new TextBox();
        this.outputField = new TextBox();
        this.newButton = new Button();
        this.loadButton = new Button();
        this.saveButton = new Button();
        this.encodeButton = new Button();
        this.decodeButton = new Button();
        this.clearButton = new Button();
        this.copyButton = new Button();
        this.displayLabel = new Label();
        this.SuspendLayout();

        this.inputLabel.Location = new Point(10, 10);
        this.inputLabel.Font = new Font("Helvetica", 12F);
        this.inputLabel.Name = "inputlabel";
        this.inputLabel.Size = new Size(60, 20);
        this.inputLabel.Text = "Input:"; 
         
        this.outputLabel.Location = new Point(10, 40);
        this.outputLabel.Font = new Font("Helvetica", 12F);
        this.outputLabel.Name = "outputlabel";
        this.outputLabel.Size = new Size(60, 20);
        this.outputLabel.Text = "Output:"; 
         
        this.inputField.Location = new Point(80, 10);
        this.inputField.Name = "inputField";
        this.inputField.Size = new Size(480, 20);
        this.inputField.TabIndex = 0;
        this.inputField.Text = "";
        
        this.outputField.Location = new Point(80, 40);
        this.outputField.Name = "outputField";
        this.outputField.Size = new Size(480, 20);
        this.outputField.Enabled = false;
        this.outputField.Text = "";

        this.newButton.Location = new Point(10, 70);
        this.newButton.Name = "newButton";
        this.newButton.TabIndex = 1;
        this.newButton.Text = "New";
         
        this.loadButton.Location = new Point(90, 70);
        this.loadButton.Name = "loadButton";
        this.loadButton.TabIndex = 2;
        this.loadButton.Text = "Load";
         
        this.saveButton.Location = new Point(170, 70);
        this.saveButton.Name = "saveButton";
        this.saveButton.TabIndex = 3;
        this.saveButton.Text = "Save";
         
        this.encodeButton.Location = new Point(250, 70);
        this.encodeButton.Name = "encodeButton";
        this.encodeButton.TabIndex = 4;
        this.encodeButton.Text = "Encode";
         
        this.decodeButton.Location = new Point(330, 70);
        this.decodeButton.Name = "decodeButton";
        this.decodeButton.TabIndex = 5;
        this.decodeButton.Text = "Decode";
         
        this.clearButton.Location = new Point(410, 70);
        this.clearButton.Name = "clearButton";
        this.clearButton.TabIndex = 6;
        this.clearButton.Text = "Clear";
         
        this.copyButton.Location = new Point(490, 70);
        this.copyButton.Name = "copyButton";
        this.copyButton.TabIndex = 7;
        this.copyButton.Text = "Copy";
         
        this.displayLabel.Location = new Point(10, 100);
        this.displayLabel.Font = new Font("Courier", 12F);
        this.displayLabel.Name = "displaylabel";
        this.displayLabel.Size = new Size(500, 20);
        this.displayLabel.Text = "something happened..."; 

        this.ClientSize = new Size(580, 125);
        this.Controls.Add(this.inputLabel);
        this.Controls.Add(this.outputLabel);
        this.Controls.Add(this.inputField);
        this.Controls.Add(this.outputField);
        this.Controls.Add(this.newButton);
        this.Controls.Add(this.loadButton);
        this.Controls.Add(this.saveButton);
        this.Controls.Add(this.encodeButton);
        this.Controls.Add(this.decodeButton);
        this.Controls.Add(this.clearButton);
        this.Controls.Add(this.copyButton);
        this.Controls.Add(this.displayLabel);
        this.BackColor = Color.LightGray;
        this.Name = "demoguiname";
        this.Text = "demogui";

        this.ResumeLayout(false);
    }

    [STAThread]
    public static void Main() {
        Application.EnableVisualStyles();
        Application.Run(new EncryptGUI());
    }
}

/* 《程式語言教學誌》的範例程式
    http://pydoing.blogspot.com/
    檔名:encryptgui.cs
    功能:示範 C# 程式 
    作者:張凱慶
    時間:西元 2012 年 10 月 */


先留意到第 32 行
this.SuspendLayout();


SuspendLayout() 用來暫時停止配置視窗元件,這是因為接下來連續為每個視窗元件設定屬性,可隱藏多個 Layout 事件。然後到建構子的最後要呼叫 ResumeLayout() ,並以 false 當參數 (parameter) ,就會重新配置視窗元件
this.ResumeLayout(false);


下面 Location 屬性指定元件在視窗的座標, Font 屬性設定文字標籤的字型, Name 為名稱, TabIndex 為使用 tab 鍵的順序, Text 設定文字標籤。第 99 行
this.ClientSize = new Size(580, 125);


Form 的 ClientSize 屬性設定視窗大小。


編譯時記得加入 -pkg:dotnet 參數,接下來我們開始整合 Encrypt 吧!


中英文術語對照
屬性property
建構子constructor
參數parameter


您可以繼續參考
GUI 篇


相關目錄
回 C# 入門指南
回 C# 教材
回首頁


參考資料
http://msdn.microsoft.com/zh-tw/library/dd30h2yb.aspx
http://msdn.microsoft.com/zh-tw/library/ms229601%28v=vs.80%29.aspx
http://msdn.microsoft.com/zh-tw/library/ms229599%28v=vs.80%29.aspx
http://msdn.microsoft.com/zh-tw/library/ms229597%28v=vs.80%29.aspx
http://blogs.msdn.com/b/jfoscoding/archive/2005/04/07/406341.aspx
http://msdn.microsoft.com/zh-tw/library/ms182351%28v=vs.80%29.aspx
http://msdn.microsoft.com/zh-tw/library/system.windows.forms.control.suspendlayout%28v=vs.80%29.aspx
http://msdn.microsoft.com/zh-tw/library/w8k76wfs%28v=vs.80%29.aspx

沒有留言: