日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

C#中?MessageBox的使用技巧_C 語言

作者:IC00 ? 更新時間: 2022-10-06 編程語言

前言

我們在學習Windows應用程序開發中,經常會用到消息對話框給用戶或者管理員一些的消息提示,它們都是基于對MessageBox類的消息對話框的一種應用,在C#中,MessageBox消息對話框位于System.Windows.Forms命名空間中。一般情況下,一個消息對話框包含信息提示文字內容,消息對話框標題文字,用戶響應按鈕及信息圖標的內容,我們可以根據自己的需求設置消息對話框。好了我們開始學習吧!!!!

1.創建窗體文件

注:在取文件名或者項目名盡量別和系統的變量名沖突,就是別取一樣的比如博主文件名取的MessageBox到后面又要改

1.1 設計界面

2.認識消息對話框的屬性和圖標

  AbortRetryIgnore	  在消息框對話框中提供“中止”、“重試”和“忽略”三個按鈕
  OK	  在消息框對話框中提供“確定”按鈕
  OKCancel	  在消息框對話框中提供“確定”和“取消”兩個按鈕
  RetryCancel	  在消息框對話框中提供“重試”和“取消”兩個按鈕
  YesNo	  在消息框對話框中提供“是”和“否”兩個按鈕
  YesNoCancel	  在消息框對話框中提供“是”、“否”和“取消”三個按鈕

3.MessageBox消息對話框實現效果

3.1 AbortRetryIgnore效果展示

消息提示框類型使用 AbortRetryIgnore在消息框對話框中提供“中止”、“重試”和“忽略”三個按鈕,圖標設置為Warning,警告圖標

3.2 OK效果展示

消息提示框類型使用OK在消息框對話框中提供“確定”按鈕,圖標設置為Asterisk,消息圖標

3.3 OKCancel效果展示

消息提示框類型使用OKCancel在消息框對話框中提供“確定”和“取消”兩個按鈕,圖標設置為Error,錯誤警告圖標

3.4 RetryCancel效果展示

消息提示框類型使用RetryCancel在消息框對話框中提供“重試”和“取消”兩個按鈕,圖標設置為Question,問號系統圖標

3.5 YesNo效果展示

消息提示框類型使用 YesNo在消息框對話框中提供“是”和“否”兩個按鈕,圖標設置為Question,問號系統圖標

3.6 YesNoCancel效果展示

消息提示框類型使用YesNoCancel在消息框對話框中提供“是”、“否”和“取消”三個按鈕,圖標為None空白圖標

4.代碼展示

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace TestMessageBox
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            MessageBox.Show("測試一下消息對話框在消息框對話框中提供“中止”、“重試”和“忽略”三個按鈕!", "測試測試", MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1);//第一個參數文本表示提示內容,第二個參數文本表示消息框標題,第三個參數MessageBoxButtons消息框的按鈕樣式,第四個參數MessageBoxIcon表示系統圖標,第五個參數MessageBoxDefaultButton表示提示框默認選擇的按鈕
        }

        private void button2_Click(object sender, EventArgs e)
        {
            MessageBox.Show("測試一下消息對話框在消息框對話框中提供“確定”按鈕!", "測試測試", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            MessageBox.Show("測試一下消息對話框在消息框對話框中提供“確定”和“取消”兩個按鈕!", "測試測試", MessageBoxButtons.OKCancel, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
        }

        private void button4_Click(object sender, EventArgs e)
        {
            MessageBox.Show("測試一下消息對話框在消息框對話框中提供“重試”和“取消”兩個按鈕!", "測試測試", MessageBoxButtons.RetryCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1);
        }

        private void button5_Click(object sender, EventArgs e)
        {
            MessageBox.Show("測試一下消息對話框在消息框對話框中提供“是”和“否”兩個按鈕!", "測試測試", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1);
        }

        private void button6_Click(object sender, EventArgs e)
        {
            MessageBox.Show("測試一下消息對話框在消息框對話框中提供“是”、“否”和“取消”三個按鈕!", "測試測試", MessageBoxButtons.YesNoCancel, MessageBoxIcon.None, MessageBoxDefaultButton.Button1);
        }
    }
}

總結

文章介紹了MessageBox.show的消息框的使用,它6個消息提示框按鈕展示樣式效果和它的屬性名還是很容易理解 的比如OK就是一個確定按鈕,消息提示框在C#窗體應用編程中使用頻率特別高,有時候就經常把這個消息提示框用來起到斷點的作用,博主雖然知道vs2019怎么用斷點,但是還是喜歡用這個MessageBox.show來輸出我需要的值,哈哈哈,MessageBox.show的輸出類型是String類型,如果你把整形輸出就要用Convert.ToString()轉一下。

原文鏈接:https://juejin.cn/post/7130242838310158344

欄目分類
最近更新