C#中的Dialog對話框
一、MessageBox彈出框
MessageBox.Show(<字符串> Text, <字符串> Title, <整型> nType,MessageBoxIcon);
- 第一個參數(shù)是 String 類型,表示提示框里面的 內(nèi)容;
- 第二個參數(shù)是String 類型,表示提示框的 標(biāo)題;
- 第三個參數(shù)是整數(shù)類型,表示消息框的類型 ,一般的都使用系統(tǒng)提供的幾種類型;
- 第四個參數(shù)是提示框的 圖標(biāo),比如說警告、提示、問題等等。
MessageBoxButtons類型:
- AbortRetryIgnore: 消息框包含“中止”、“重試”和“忽略”按鈕。
- OK :消息框包含“確定”按鈕。(默認)
- OKCancel : 消息框包含“確定”和“取消”按鈕。(上例所示)
- RetryCancel :消息框包含“重試”和“取消”按鈕。
- YesNo :消息框包含“是”和“否”按鈕。
- YesNoCancel :消息框包含“是”、“否”和“取消”按鈕
MessageBoxIcon圖標(biāo)樣式:
- MessageBoxIcon.Question
MessageBoxIcon.Asterisk
MessageBoxIcon.Information
MessageBoxIcon.Error
MessageBoxIcon.Stop
MessageBoxIcon.Hand
MessageBoxIcon.Exclamation
MessageBoxIcon.Warning
舉例
MessageBox.Show("用戶名或者密碼不能為空"); MessageBox.Show("用戶名或者密碼不能為空","登錄提示"); MessageBox.Show("用戶名或者密碼不能為空","登錄提示",MessageBoxButtons.OKCancel); MessageBox.Show("用戶名或者密碼不能為空","登錄提示",MessageBoxButtons.OKCancel,MessageBoxIcon.Exclamation);
二、WinForm自帶對話框
除PrintPreviewDialog外,所有的對話框都繼承于抽象類CommonDialog。
CommonDialog的繼承結(jié)構(gòu)
- 1、文件對話框(FileDialog) 它又常用到兩個:
- 打開文件對話框(OpenFileDialog)
- 保存文件對話(SaveFileDialog)
- 2、字體對話框(FontDialog)
- 3、顏色對話框(ColorDialog)
- 4、打印預(yù)瀏對話框(PrintPreviewDialog)
- 5、頁面設(shè)置(PrintDialog)
- 6、打印對話框(PrintDialog)
CommonDialog的方法
- OnHelpRequest(EventArgs): 引發(fā) HelpRequest 事件。
- Reset():在派生類中被重寫時,將通用對話框的屬性重置為默認值。
- ShowDialog(): 用默認的所有者運行通用對話框。
- ShowDialog(IWin32Window) : 運行具有指定所有者的通用對話框。
1、打開文件對話框(OpenFileDialog)
基本屬性
- InitialDirectory 對話框的初始目錄
- Filter 要在對話框中顯示的文件篩選器,例如,"文本文件(*.txt)|*.txt|所有文件(*.*)||*.*"
- FilterIndex 在對話框中選擇的文件篩選器的索引,如果選第一項就設(shè)為1
- RestoreDirectory 控制對話框在關(guān)閉之前是否恢復(fù)當(dāng)前目錄
- FileName 獲取或設(shè)置一個包含在文件對話框中選定的文件名的字符串。
- Title 將顯示在對話框標(biāo)題欄中的字符
- AddExtension 是否自動添加默認擴展名
- CheckPathExists 在對話框返回之前,檢查指定路徑是否存在
- DefaultExt 默認擴展名
- DereferenceLinks 在從對話框返回前是否取消引用快捷方式
- ShowHelp 啟用"幫助"按鈕
- ValiDateNames 控制對話框檢查文件名中是否不含有無效的字符或序列
示例
System.Windows.Forms.OpenFileDialog dlg = new System.Windows.Forms.OpenFileDialog(); dlg.Title = "打開文件"; dlg.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Templates); dlg.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"; dlg.FilterIndex = 2; dlg.RestoreDirectory = true; if (dlg.ShowDialog() == DialogResult.OK) { if (dlg.FileName != "") //如果dlg.Multiselect=true;可以是dlg.FileNames { MessageBox.Show("你選擇了" + dlg.FileName); } }
2、保存文件對話框(SaveFileDialog)
屬性
- Filter 要在對話框中顯示的文件篩選器,例如,"文本文件(*.txt)|*.txt|所有文件(*.*)|*.*"
- FilterIndex 在對話框中選擇的文件篩選器的索引,如果選第一項就設(shè)為1
- RestoreDirectory 控制對話框在關(guān)閉之前是否恢復(fù)當(dāng)前目錄
- AddExtension 是否自動添加默認擴展名
- CheckFileExists 獲取或設(shè)置一個值,該值指示如果用戶指定不存在的文件名,對話框是否顯示警告。
- CheckPathExists 在對話框返回之前,檢查指定路徑是否存在
- Container 控制在將要創(chuàng)建文件時,是否提示用戶。只有在ValidateNames為真值時,才適用。
- DefaultExt 缺省擴展名
- DereferenceLinks 在從對話框返回前是否取消引用快捷方式
- FileName 獲取或設(shè)置一個包含在文件對話框中選定的文件名的字符串。
- InitialDirector 對話框的初始目錄
- OverwritePrompt 控制在將要在改寫現(xiàn)在文件時是否提示用戶,只有在ValidateNames為真值時,才適用
- ShowHelp 啟用"幫助"按鈕
- Title 將顯示在對話框標(biāo)題欄中的字符
- ValidateNames 控制對話框檢查文件名中是否不含有無效的字符或序列
示例
System.IO.Stream stream; System.Windows.Forms.SaveFileDialog saveFileDialog1 = new System.Windows.Forms.SaveFileDialog(); saveFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"; saveFileDialog1.FilterIndex = 2; saveFileDialog1.RestoreDirectory = true; if (saveFileDialog1.ShowDialog() == DialogResult.OK) { if ((stream = saveFileDialog1.OpenFile()) != null) { // Code to write the stream goes here. stream.Close(); } }
3、打印預(yù)覽對話框和打印對話框
1、打印預(yù)覽對話框(PrintPreviewDialog)屬性:
AutoScrollMargin 獲取或設(shè)置自動滾動邊距的大小。
AutoScrollMinSize 獲取或設(shè)置自動滾動的最小尺寸。
DialogResult 獲取或設(shè)置窗體的對話框結(jié)果。
Document 獲取或設(shè)置要預(yù)覽的文檔。
HelpButton 獲取或設(shè)置一個值,該值指示是否應(yīng)在窗體的標(biāo)題框中顯示“幫助”按鈕。
2、打印對話框(PrintDialog)屬性:
AllowPrintToFile 禁止或使用"打印到文件"復(fù)選框
AllowSelection 禁止或使用"選定內(nèi)容"單選框
AllowSomePages 禁止或使用"頁"單選按鈕
Document 從中獲取打印機設(shè)置的PrintDocument
- PrintToFile 打印到文件"復(fù)選框是否選中
- ShowHelp 控制是否顯示"幫助"按鈕
- ShowNetWork 控制是否顯示"網(wǎng)絡(luò)"按鈕
3、示例:
private void printPreviewButton_Click(object sender, EventArgs e) { StreamReader streamToPrint = new StreamReader("PrintMe.Txt"); try { PrintDocument pd = new PrintDocument(streamToPrint); //假定為默認打印機 if (storedPageSettings != null) { pd.DefaultPageSettings = storedPageSettings; } PrintPreviewDialog dlg = new PrintPreviewDialog(); dlg.Document = pd; dlg.ShowDialog(); } finally { streamToPrint.Close(); } } private void printButton_Click(object sender, EventArgs e) { StreamReader streamToPrint = new StreamReader("PrintMe.Txt"); try { PrintDocument pd = new PrintDocument(streamToPrint); PrintDialog dlg = new PrintDialog(); dlg.Document = pd; DialogResult result = dlg.ShowDialog(); if (result == DialogResult.OK) pd.Print(); } finally { streamToPrint.Close(); } }
三、自定義對話框
1 模態(tài)窗口: ShowDialog():
打開模態(tài)窗口后,只要不關(guān)閉該窗口,鼠標(biāo)焦點或者光標(biāo)就會一直停留在該窗口上。只有關(guān)閉該窗口后,調(diào)用窗口才能繼續(xù)。
模態(tài)窗口關(guān)閉后,仍可以讀取模態(tài)窗口中的信息,如窗口的返回狀態(tài)等,以后還可以使用ShowDialog()使其可見。
2 非模態(tài)窗口: Show():
打開非模態(tài)窗口后,仍可以操作調(diào)用窗口。后面的代碼立即執(zhí)行。
關(guān)閉非模態(tài)窗口,該窗口將不復(fù)存在,會釋放窗口的所有資源,所以無法得到該窗口的任何信息。常用Hide()方法(等效于Visible=false)然后調(diào)用Show()方法使其可見。
3、對話框窗體:Form2
public Form1(string para)//獲取參數(shù) { InitializeComponent(); this.StartPosition = FormStartPosition.CenterParent;//啟動位置,父窗口中央 this.MaximizeBox = false; this.MinimizeBox = false; this.ShowIcon = false;//不顯示圖標(biāo) this.ControlBox = false; this.ShowInTaskbar = false; this.FormBorderStyle = FormBorderStyle.FixedDialog;//邊框樣式為固定對話框 this.btnOK.DialogResult = DialogResult.OK;//"Enter"為OK按鈕 this.btnCancel.DialogResult = DialogResult.Cancel;//“ESC”為Cancel按鈕 this.textBox1.Text = para; } public string ReturnText //定義一個公共屬性,供調(diào)用窗口Form1使用 { get { return this.textBox1.Text + "b"; } } private void Form1_Load(object sender, EventArgs e) { if (this.Owner.Name != "Form1")//Owner為調(diào)用窗體,即調(diào)用改對話框的窗體 MessageBox.Show("非法調(diào)用"); } private void BtnOK_Click(object sender, EventArgs e) { if (this.textBox1.Text.Trim().Length == 0) MessageBox.Show("無輸入"); this.textBox1.Focus(); this.DialogResult = DialogResult.None;//阻止隱藏對話框,對話框不消失 }
4、主窗體Form1:
Form f2 = new Form2("a"); if (f2.ShowDialog(this) == DialogResult.OK)//對應(yīng)Form2中的Owner,this為給對話框窗體傳值 this.textBox1.Text = f2.ReturnText; f2.Close(); f2.Dispose();
到此這篇關(guān)于C#對話框Dialog的文章就介紹到這了。希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C#使用List類實現(xiàn)動態(tài)變長數(shù)組的方法
這篇文章主要介紹了C#使用List類實現(xiàn)動態(tài)變長數(shù)組的方法,涉及C#中List類的使用技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-04-04C# Socket 發(fā)送&接收&返回 簡單應(yīng)用實例
下面小編就為大家分享一篇C# Socket 發(fā)送&接收&返回 簡單應(yīng)用實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2017-11-11