iT邦幫忙

0

Day 21視窗應用程式進階控制通用控制項:MessageBox (訊息框)&ComboBox (下拉式選單)

c#
  • 分享至 

  • xImage
  •  

MessageBox(訊息框)
1.什麼是MessageBox
MessageBox是一種「對話框(Dialog Box)」,不用拉控制項到畫面
用來顯示訊息、警告、錯誤、詢問使用者選擇等
它會暫停程式,直到使用者按下按鈕(如「確定」或「取消」)才繼續執行

2.常見的語法格式(多種重載)
https://ithelp.ithome.com.tw/upload/images/20251006/20178925RvZDNN9sdj.png

按鈕與圖示可用選項
3.MessageBoxButtons 按鈕種類:

  • OK只有:「確定」
  • OKCancel:「確定」與「取消」
  • YesNo:「是」與「否」
  • YesNoCancel:「是」、「否」、「取消」
  • RetryCancel:「重試」與「取消」
  • AbortRetryIgnore:「中止」、「重試」、「忽略」

4.MessageBoxIcon 圖示類型:

  • Information:提示(藍色圓圈 i)
  • Warning:警告(黃色三角形)
  • Error:錯誤(紅色叉叉)
  • Question:詢問(問號)
  • None:無圖示

範例
程式碼
https://ithelp.ithome.com.tw/upload/images/20251006/201789256tMl4mt6op.png

執行程式跳出MessageBox
https://ithelp.ithome.com.tw/upload/images/20251006/20178925svIQJWaJmA.png

選擇是直接結束程式
https://ithelp.ithome.com.tw/upload/images/20251006/20178925UVKtk1uFLB.png

選擇否跳出提示已取消離開
https://ithelp.ithome.com.tw/upload/images/20251006/20178925C2ldyXkK8H.png
https://ithelp.ithome.com.tw/upload/images/20251006/20178925JhMVqLamqA.png

按下確認跳出畫面
https://ithelp.ithome.com.tw/upload/images/20251006/20178925Wsob7oRHGk.png

補充:DialogResult 判斷值
在有多個按鈕的訊息框中,Show() 會回傳一個 DialogResult:

  • 確定:DialogResult.OK
  • 取消:DialogResult.Cancel
  • 是:DialogResult.Yes
  • 否:DialogResult.No
  • 重試:DialogResult.Retry
  • 忽略:DialogResult.Ignore
  • 中止:DialogResult.Abort

ComboBox(下拉式選單)
1.什麼是ComboBox
ComboBox是一種下拉式清單控制項,允許使用者:

  • 直接從清單中選擇一個項目
  • 自行輸入內容(可設定是否允許)
    就像表單中常見的「性別」、「城市」或「年份」選擇欄位

2.基本結構與外觀
在設計視窗中加入ComboBox之後,
你會看到一個可展開的小箭頭 ▼
使用者可以從下拉清單選擇項目

  1. 常見屬性(Properties)
    https://ithelp.ithome.com.tw/upload/images/20251006/201789254DK44sm6KD.png

4.DropDownStyle屬性說明

  • DropDown:可輸入文字,也可選項目(預設)
  • DropDownList:僅能從清單中選擇,不能輸入
  • Simple:清單永遠展開顯示(較少用)
    https://ithelp.ithome.com.tw/upload/images/20251006/20178925Bg2Z5Zqj9X.png

5.常用方法(Methods)
https://ithelp.ithome.com.tw/upload/images/20251006/20178925Pa5HlRQCHo.png

6.事件(Events)
https://ithelp.ithome.com.tw/upload/images/20251006/20178925NeSuXNHlKN.png

範例
控制項配置
https://ithelp.ithome.com.tw/upload/images/20251006/20178925Jv4oQlobrz.png

程式碼
https://ithelp.ithome.com.tw/upload/images/20251006/20178925d82yf9qHeX.png

選擇縣市,也可自行輸入
https://ithelp.ithome.com.tw/upload/images/20251006/201789258Gd5De3zQ2.png

選擇性別,此控制項DropDownStyle屬性改為DropDownList不能輸入
https://ithelp.ithome.com.tw/upload/images/20251006/20178925r9RI9mXlpw.png

按下確定按鈕顯示選擇結果
https://ithelp.ithome.com.tw/upload/images/20251006/201789259xvpSp4I66.png


圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言