網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
一、MessageBox彈出框
MessageBox.Show(<字符串> Text, <字符串> Title, <整型> nType,MessageBoxIcon);
- 第一個(gè)參數(shù)是 String 類(lèi)型,表示提示框里面的 內(nèi)容;
- 第二個(gè)參數(shù)是String 類(lèi)型,表示提示框的 標(biāo)題;
- 第三個(gè)參數(shù)是整數(shù)類(lèi)型,表示消息框的類(lèi)型? ,一般的都使用系統(tǒng)提供的幾種類(lèi)型;
- 第四個(gè)參數(shù)是提示框的 圖標(biāo),比如說(shuō)警告、提示、問(wèn)題等等。
MessageBoxButtons類(lèi)型:
- AbortRetryIgnore: 消息框包含“中止”、“重試”和“忽略”按鈕。
- OK :消息框包含“確定”按鈕。(默認(rèn))
- 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自帶對(duì)話框
除PrintPreviewDialog外,所有的對(duì)話框都繼承于抽象類(lèi)CommonDialog。
CommonDialog的繼承結(jié)構(gòu)
- 1、文件對(duì)話框(FileDialog) 它又常用到兩個(gè):
- 打開(kāi)文件對(duì)話框(OpenFileDialog)
- 保存文件對(duì)話(SaveFileDialog)
- 2、字體對(duì)話框(FontDialog)
- 3、顏色對(duì)話框(ColorDialog)
- 4、打印預(yù)瀏對(duì)話框(PrintPreviewDialog)
- 5、頁(yè)面設(shè)置(PrintDialog)
- 6、打印對(duì)話框(PrintDialog)
CommonDialog的方法
- OnHelpRequest(EventArgs):??? 引發(fā) HelpRequest 事件。
- Reset():在派生類(lèi)中被重寫(xiě)時(shí),將通用對(duì)話框的屬性重置為默認(rèn)值。
- ShowDialog():??? 用默認(rèn)的所有者運(yùn)行通用對(duì)話框。
- ShowDialog(IWin32Window) :?? 運(yùn)行具有指定所有者的通用對(duì)話框。
1、打開(kāi)文件對(duì)話框(OpenFileDialog)
基本屬性
- InitialDirectory 對(duì)話框的初始目錄
- Filter 要在對(duì)話框中顯示的文件篩選器,例如,"文本文件(*.txt)|*.txt|所有文件(*.*)||*.*"
- FilterIndex 在對(duì)話框中選擇的文件篩選器的索引,如果選第一項(xiàng)就設(shè)為1
- RestoreDirectory 控制對(duì)話框在關(guān)閉之前是否恢復(fù)當(dāng)前目錄
- FileName 獲取或設(shè)置一個(gè)包含在文件對(duì)話框中選定的文件名的字符串。
- Title 將顯示在對(duì)話框標(biāo)題欄中的字符
- AddExtension 是否自動(dòng)添加默認(rèn)擴(kuò)展名
- CheckPathExists 在對(duì)話框返回之前,檢查指定路徑是否存在
- DefaultExt 默認(rèn)擴(kuò)展名
- DereferenceLinks 在從對(duì)話框返回前是否取消引用快捷方式
- ShowHelp 啟用"幫助"按鈕
- ValiDateNames 控制對(duì)話框檢查文件名中是否不含有無(wú)效的字符或序列
示例
System.Windows.Forms.OpenFileDialog dlg = new System.Windows.Forms.OpenFileDialog();
dlg.Title = "打開(kāi)文件";
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、保存文件對(duì)話框(SaveFileDialog)
屬性
- Filter 要在對(duì)話框中顯示的文件篩選器,例如,"文本文件(*.txt)|*.txt|所有文件(*.*)|*.*"
- FilterIndex 在對(duì)話框中選擇的文件篩選器的索引,如果選第一項(xiàng)就設(shè)為1
- RestoreDirectory 控制對(duì)話框在關(guān)閉之前是否恢復(fù)當(dāng)前目錄
- AddExtension 是否自動(dòng)添加默認(rèn)擴(kuò)展名
- CheckFileExists 獲取或設(shè)置一個(gè)值,該值指示如果用戶指定不存在的文件名,對(duì)話框是否顯示警告。
- CheckPathExists 在對(duì)話框返回之前,檢查指定路徑是否存在
- Container 控制在將要?jiǎng)?chuàng)建文件時(shí),是否提示用戶。只有在ValidateNames為真值時(shí),才適用。
- DefaultExt 缺省擴(kuò)展名
- DereferenceLinks 在從對(duì)話框返回前是否取消引用快捷方式
- FileName 獲取或設(shè)置一個(gè)包含在文件對(duì)話框中選定的文件名的字符串。
- InitialDirector 對(duì)話框的初始目錄
- OverwritePrompt 控制在將要在改寫(xiě)現(xiàn)在文件時(shí)是否提示用戶,只有在ValidateNames為真值時(shí),才適用
- ShowHelp 啟用"幫助"按鈕
- Title 將顯示在對(duì)話框標(biāo)題欄中的字符
- ValidateNames 控制對(duì)話框檢查文件名中是否不含有無(wú)效的字符或序列
示例
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ù)覽對(duì)話框和打印對(duì)話框
1、打印預(yù)覽對(duì)話框(PrintPreviewDialog)屬性:
AutoScrollMargin 獲取或設(shè)置自動(dòng)滾動(dòng)邊距的大小。
AutoScrollMinSize 獲取或設(shè)置自動(dòng)滾動(dòng)的最小尺寸。
DialogResult 獲取或設(shè)置窗體的對(duì)話框結(jié)果。
Document 獲取或設(shè)置要預(yù)覽的文檔。
HelpButton 獲取或設(shè)置一個(gè)值,該值指示是否應(yīng)在窗體的標(biāo)題框中顯示“幫助”按鈕。
2、打印對(duì)話框(PrintDialog)屬性:
AllowPrintToFile 禁止或使用"打印到文件"復(fù)選框
AllowSelection 禁止或使用"選定內(nèi)容"單選框
AllowSomePages 禁止或使用"頁(yè)"單選按鈕
Document 從中獲取打印機(jī)設(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); //假定為默認(rèn)打印機(jī)
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();
}
}
三、自定義對(duì)話框
1 模態(tài)窗口: ShowDialog():
打開(kāi)模態(tài)窗口后,只要不關(guān)閉該窗口,鼠標(biāo)焦點(diǎn)或者光標(biāo)就會(huì)一直停留在該窗口上。只有關(guān)閉該窗口后,調(diào)用窗口才能繼續(xù)。
模態(tài)窗口關(guān)閉后,仍可以讀取模態(tài)窗口中的信息,如窗口的返回狀態(tài)等,以后還可以使用ShowDialog()使其可見(jiàn)。
2 非模態(tài)窗口: Show():
打開(kāi)非模態(tài)窗口后,仍可以操作調(diào)用窗口。后面的代碼立即執(zhí)行。
關(guān)閉非模態(tài)窗口,該窗口將不復(fù)存在,會(huì)釋放窗口的所有資源,所以無(wú)法得到該窗口的任何信息。常用Hide()方法(等效于Visible=false)然后調(diào)用Show()方法使其可見(jiàn)。
3、對(duì)話框窗體:Form2
public Form1(string para)//獲取參數(shù)
{
InitializeComponent();
this.StartPosition = FormStartPosition.CenterParent;//啟動(dòng)位置,父窗口中央
this.MaximizeBox = false;
this.MinimizeBox = false;
this.ShowIcon = false;//不顯示圖標(biāo)
this.ControlBox = false;
this.ShowInTaskbar = false;
this.FormBorderStyle = FormBorderStyle.FixedDialog;//邊框樣式為固定對(duì)話框
this.btnOK.DialogResult = DialogResult.OK;//"Enter"為OK按鈕
this.btnCancel.DialogResult = DialogResult.Cancel;//“ESC”為Cancel按鈕
this.textBox1.Text = para;
}
public string ReturnText //定義一個(gè)公共屬性,供調(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)用改對(duì)話框的窗體
MessageBox.Show("非法調(diào)用");
}
private void BtnOK_Click(object sender, EventArgs e)
{
if (this.textBox1.Text.Trim().Length == 0)
MessageBox.Show("無(wú)輸入");
this.textBox1.Focus();
this.DialogResult = DialogResult.None;//阻止隱藏對(duì)話框,對(duì)話框不消失
}
4、主窗體Form1:
Form f2 = new Form2("a");
if (f2.ShowDialog(this) == DialogResult.OK)//對(duì)應(yīng)Form2中的Owner,this為給對(duì)話框窗體傳值
this.textBox1.Text = f2.ReturnText;
f2.Close();
f2.Dispose();
原文鏈接:https://www.cnblogs.com/springsnow/p/9434005.html
相關(guān)推薦
- 2022-08-05 C語(yǔ)言示例講解while循環(huán)語(yǔ)句的用法_C 語(yǔ)言
- 2022-08-04 Python?編程操作連載之字符串,列表,字典和集合處理_python
- 2022-07-06 python數(shù)據(jù)挖掘Apriori算法實(shí)現(xiàn)關(guān)聯(lián)分析_python
- 2023-04-02 C語(yǔ)言中的線程信號(hào)控制詳解_C 語(yǔ)言
- 2022-11-07 關(guān)于react?父子組件的執(zhí)行順序_React
- 2022-12-24 Docker自定義網(wǎng)絡(luò)詳解_docker
- 2022-06-12 PostgreSQL數(shù)據(jù)庫(kù)事務(wù)插入刪除及更新操作示例_PostgreSQL
- 2022-11-03 C語(yǔ)言編寫(xiě)掃雷小程序_C 語(yǔ)言
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過(guò)濾器
- Spring Security概述快速入門(mén)
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支