.NET 程式設計入門 (使用 C#)
講師︰唐士軒
CSIE, NTU 2005/06/01
MessageBox 類別
zMessageBox 類別提供錯誤或警告訊息等 的對話方塊,以提醒使用者注意
zShow 靜態方法
zShow (訊息文字, 標題文字, 顯示按鈕, 圖示);
zShow (string, string, MessageBoxButtons,
MessageBoxIcon)
MessageBox 類別–顯示按鈕
說明 顯示按鈕列舉常數
重試、取消 MessageBoxButtons.RetryCancel
MessageBoxButtons.YesNo 是、否
是、否、取消 MessageBoxButtons.YesNoCancel
異常終止 重試、略過 MessageBoxButtons.AbortRetryIgnore
確定、取消 MessageBoxButtons.OKCancel
MessageBoxButtons.OK 確定
MessageBox 類別–圖示
MessageBoxIcon.Question MessageBoxIcon.Stop MessageBoxIcon.Hand
圖示 圖示列舉常數
MessageBoxIcon.None 無 MessageBoxIcon.Error MessageBoxIcon.Warning MessageBoxIcon.Exclamation
MessageBoxIcon.Information MessageBoxIcon.Asterisk
MessageBox 類別–回傳值
z 呼叫 MessageBox.Show 方法時,當使用者按 下訊息方塊中其中一個按鈕時,會回傳一個 DialogResult 列舉型別的回傳值
5 4 3 2 1
回傳值 按鈕
列舉型別
DialogResult.Ignore 略過
重試 DialogResult.Retry
DialogResult.Abort 異常終止 DialogResult.Cancel 取消
確定 DialogResult.OK
實例探討 sample8-a1 (1)
z 程式功能
z 密碼設定 (限四碼)
z 程式內容
private void okBTN_Click(object sender, System.EventArgs e) {
if(pwTB.Text.Length != 4)
MessageBox.Show("密碼設定限四位數!!", "設定錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error);
else {
MessageBox.Show("密碼設定成功!!", "設定完成",
MessageBoxButtons.OK, MessageBoxIcon.Information);
Application.Exit();
}
實例探討 sample8-a1 (2)
z 程式內容
private void exitBTN_Click(object sender, System.EventArgs e)
{
DialogResult result;
result = MessageBox.Show("確定離開!?", "離開", MessageBoxButtons.OKCancel,
MessageBoxIcon.Question);
if(result == DialogResult.OK) Application.Exit();
else
RadioButton 控制項
z RadioButton 控制項提供單選選項按鈕的功能 z 同一個表單中具有多組單選選項時,可利用
GroupBox 或 Panel 來區隔 z 常用屬性
z CheckAlign–設定選項按鈕顯示位置 z Checked–按鈕是否選取
z Text–顯示文字
z 常用事件
z CheckedChanged–Checked 屬性改變時發生
實例探討 sample8-a2 (1)
z程式功能
z資料核對
z利用 GroupBox 將 RadioButton 區分為二組
z程式內容
private void exitBTN_Click(object sender, System.EventArgs e)
{
Application.Exit();
}
實例探討 sample8-a2 (2)
z 程式內容
private void sendBTN_Click(object sender, System.EventArgs e)
{
string checkStr = "您是" + (sex1RB.Checked? "男生":
"女生") + ",您的學歷為";
foreach(RadioButton rb in educationGB.Controls) if(rb.Checked) checkStr += rb.Text;
MessageBox.Show(checkStr, "資料核對");
CheckBox 控制項
z CheckBox 控制項提供複選核取方塊的功能 z 常用屬性
z CheckAlign–設定核取方塊顯示位置 z Checked–核取方塊是否選取
z Text–顯示文字
z ThreeState–設定為雙態或三態 z CheckState–取得目前勾選狀況
z 常用事件
z CheckedChanged–Checked 屬性改變時發生
z CheckStateChanged–CheckState 屬性改變時發生
實例探討 sample8-a3 (1)
z 程式功能
z 電腦採購
z 勾選需要配備立即更新總價
z 程式內容
private void monitorCB_CheckedChanged(object sender, System.EventArgs e)
{
int cost = monitorCB.Checked? 12000: -12000;
totalLB.Text = (int.Parse(totalLB.Text) + cost).ToString();
}
實例探討 sample8-a3 (2)
z 程式內容
private void boxCB_CheckedChanged(object sender, System.EventArgs e)
{
int cost = boxCB.Checked? 15000: -15000;
totalLB.Text = (int.Parse(totalLB.Text) + cost).ToString();
}
private void mouseCB_CheckedChanged(object sender, System.EventArgs e)
{
int cost = mouseCB.Checked? 500: -500;
totalLB.Text = (int.Parse(totalLB.Text) +
實例探討 sample8-a3 (3)
z 程式內容
private void
keyboardCB_CheckedChanged(object sender, System.EventArgs e)
{
int cost = keyboardCB.Checked? 500: -500;
totalLB.Text = (int.Parse(totalLB.Text) + cost).ToString();
}
課堂練習 sample8-b1
z程式功能
z電腦採購
zCPU 、 RAM 及週邊各有三種類可挑選
zCPU 及 RAM 為必備元件,若沒採購跳出錯 誤訊息視窗
z計算全部費用
z基本概念
z可利用迴圈讀取 GroupBox 中的元件簡化程
式碼
ListBox 控制項 (1)
zListBox 控制項提供文字項目清單供使用者 選擇
z常用屬性
zItems–元素內容 (ListBox.ObjectCollection 類別) zMultiColumn–設定多欄顯示
zColumnWidth–多欄顯示時每一個欄位寬度 zSelectionMode–設定清單可選取數目
zNone–不能選取 zOne–單選
zMultiSimple–複選 (點一下選取,再點一下取消)
ListBox 控制項 (2)
z常用屬性
zSelectedItem–取得被選取項目
zSelectedItems–取得被選取項目集合 zSelectedIndex–取得選取項目索引
zSelectedIndices–取得所有選取項目索引
z常用事件
zSelectedIndexChange–清單中選取項目改變
時發生
相關類別
zListBox.ObjectCollection 類別 z常用屬性
zCount–計算清單項目個數
z常用方法
zAdd –新增項目至清單
zRemove–刪除清單中某項目 zClear–清除全部項目
z用法
zEx︰listBox1.Items.Add("Alice");
實例探討 sample8-a4 (1)
z 程式功能
z 設定啟用功能
z 程式內容
private void Form1_Load(object sender, System.EventArgs e)
{
string[] funcStr = {"檔案","編輯","檢視","專案","建置
","偵錯","工具","視窗","說明"};
foreach(string str in funcStr) allLB.Items.Add(str);
實例探討 sample8-a4 (2)
z 程式內容
private void addBTN_Click(object sender, System.EventArgs e) {
if(allLB.SelectedItem == null)
MessageBox.Show("尚未選取欲新增功能!!", "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Warning);
else {
enableLB.Items.Add(allLB.SelectedItem);
allLB.Items.Remove(allLB.SelectedItem);
} }
實例探討 sample8-a4 (3)
z 程式內容
private void removeBTN_Click(object sender, System.EventArgs e) {
if(enableLB.SelectedItem == null)
MessageBox.Show("尚未選取欲刪除功能!!", "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Warning);
else {
allLB.Items.Add(enableLB.SelectedItem);
enableLB.Items.Remove(enableLB.SelectedItem);
} }
ComboBox 控制項
zComboBox 控制項提供下拉式選單功能,
為 ListBox 控制項的延伸 z常用屬性
zText–取得設定文字
zMaxLength–使用者可輸入最大長度
zMaxDropDownItems–下拉部份一次顯示最 大項目個數
zDropDownStyle
zDropDown–可編輯,箭頭按鈕顯示清單
zDropDownList–不可編輯,箭頭按鈕顯示清單
實例探討 sample8-a5 (1)
z程式功能
z地址輸入
z程式內容
private void addressTB_Enter(object sender, System.EventArgs e)
{
if(addressTB.Text == "輸入地址") addressTB.Text = "";
}
實例探討 sample8-a5 (2)
z 程式內容
private void area1CB_SelectedIndexChanged(object sender, System.EventArgs e)
{
area2CB.Items.Clear();
if(area1CB.SelectedIndex == 0) {
area2CB.Items.Add("信義區");
area2CB.Items.Add("大安區");
area2CB.Text = area2CB.Items[0].ToString();
} else {
area2CB.Items.Add("汐止市");
area2CB.Items.Add("板橋市");
area2CB.Text = area2CB.Items[0].ToString();
課堂練習 sample8-b2
z 程式功能
z 書籍分類清單
z 基本概念
z 利用 ComboBox 及 ListBox 完成
台式料理 日式料理
韓式料理 食譜
資料庫程式 設計
十天學會 JAVA C# 程式設
計入門 電腦
倚天屠龍記 神鵰俠侶
射鵰英雄傳 小說
TreeView 控制項
zTreeView 控制項提供樹狀結構檢視功能 z常用屬性
zNodes–節點內容
zSelectedNode–選取節點
zCheckBoxes–顯示核取方塊
zPathSeperator–傳回路徑的字串分隔字元
z常用事件
zAfterSelect–變更選取後
相關類別
zTreeNodeCollection 類別
zCount 屬性–取得節點數
zAdd 方法–增加節點至該點子節點中
zRemove 方法–刪除節點 (傳入 TreeNode)
zTreeNode 類別
zText–節點顯示文字
zRemove 方法–刪除節點
zFullPath–取得從根樹狀節點通往目前樹狀
節點的路徑。
實例探討 sample8-a6 (1)
z 程式功能
z 簡易資料夾檢索
z 程式內容
private void Form1_Load(object sender, System.EventArgs e)
{
string[] drives = Directory.GetLogicalDrives();
foreach(string drive in drives) diskCB.Items.Add(drive);
updateTreeView(diskCB.Text);
}
實例探討 sample8-a6 (2)
z 程式內容
private void diskCB_SelectedIndexChanged(object sender, System.EventArgs e)
{
updateTreeView(diskCB.Text);
}
private void updateTreeView(string disk) {
try {
dirTV.Nodes.Clear();
實例探討 sample8-a6 (3)
z 程式內容
int i=0;
foreach(string dir in dirs)
{
dirTV.Nodes.Add(dir);
i++;
try
{
string[] subDirs = Directory.GetDirectories(dir);
foreach(string subDir in subDirs)
dirTV.Nodes[i-1].Nodes.Add(subDir);
}
catch {}
}
}
課堂練習 sample8-b3
z程式功能
z班級資料表 z可新增班級
z重複新增不予動作
z基本概念
z利用 TreeView 顯示班級資訊
ListView 控制項
zListView 控制項提供項目清單檢視功能 z常用屬性
zColumns–設定包含欄位 zItems–清單項目內容
(ListView.ListViewItemCollection 類別) zView–清單檢視方式
zCheckBoxes–顯示核取方塊
zGridLines–顯示格線
相關類別 (1)
zListView.ListViewItemCollection 類別
zCount 屬性–取得項目個數 zAdd 方法–新增項目
zRemove 方法–移除指定項目 zClear 方法–清除所有項目
zListViewItem 類別
zSubItems 屬性–子項目
(ListViewItem.ListViewSubItemCollection 類別)
zText 屬性–設定取得項目內容
相關類別 (2)
zListViewItem.ListViewSubItemCollection 類別
zCount 屬性–取得子項目個數 zAdd 方法–新增子項目
zRemove 方法–移除指定子項目 zClear 方法–清除所有子項目
zListViewItem.ListViewSubItem 類別
zText 屬性–設定取得子項目內容
實例探討 sample8-a7 (1)
z 程式功能
z 學生名單
z 程式內容
private void clearBTN_Click(object sender, System.EventArgs e)
{
idTB.Text = "";
nameTB.Text = "";
studentLV.Items.Clear();
實例探討 sample8-a7 (2)
z 程式內容
private void addBTN_Click(object sender, System.EventArgs e)
{
if(idTB.Text != "" && nameTB.Text != "") {
ListViewItem item = new ListViewItem();
item.SubItems.Add(idTB.Text);
item.SubItems.Add(nameTB.Text);
studentLV.Items.Add(item);
} }