一、MessageBox弹出框
MessageBox.Show(<字符串> Text, <字符串> Title, <整型> nType,MessageBoxIcon);
- 第一个参数是 String 类型,表示提示框里面的 内容;
 - 第二个参数是String 类型,表示提示框的 标题;
 - 第三个参数是整数类型,表示消息框的类型 ,一般的都使用系统提供的几种类型;
 - 第四个参数是提示框的 图标,比如说警告、提示、问题等等。
 
MessageBoxButtons类型:
- AbortRetryIgnore: 消息框包含“中止”、“重试”和“忽略”按钮。
 - OK :消息框包含“确定”按钮。(默认)
 - OKCancel : 消息框包含“确定”和“取消”按钮。(上例所示)
 - RetryCancel :消息框包含“重试”和“取消”按钮。
 - YesNo :消息框包含“是”和“否”按钮。
 - YesNoCancel :消息框包含“是”、“否”和“取消”按钮
 
MessageBoxIcon图标样式:
- 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的继承结构
- 1、文件对话框(FileDialog) 它又常用到两个:
- 打开文件对话框(OpenFileDialog)
 - 保存文件对话(SaveFileDialog)
 
 - 2、字体对话框(FontDialog)
 - 3、颜色对话框(ColorDialog)
 - 4、打印预浏对话框(PrintPreviewDialog)
 - 5、页面设置(PrintDialog)
 - 6、打印对话框(PrintDialog)
 
CommonDialog的方法
- OnHelpRequest(EventArgs): 引发 HelpRequest 事件。
 - Reset():在派生类中被重写时,将通用对话框的属性重置为默认值。
 - ShowDialog(): 用默认的所有者运行通用对话框。
 - ShowDialog(IWin32Window) : 运行具有指定所有者的通用对话框。
 
1、打开文件对话框(OpenFileDialog)
基本属性
- InitialDirectory 对话框的初始目录
 - Filter 要在对话框中显示的文件筛选器,例如,"文本文件(*.txt)|*.txt|所有文件(*.*)||*.*"
 - FilterIndex 在对话框中选择的文件筛选器的索引,如果选第一项就设为1
 - RestoreDirectory 控制对话框在关闭之前是否恢复当前目录
 - FileName 获取或设置一个包含在文件对话框中选定的文件名的字符串。
 - Title 将显示在对话框标题栏中的字符
 - 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 在对话框中选择的文件筛选器的索引,如果选第一项就设为1
 - RestoreDirectory 控制对话框在关闭之前是否恢复当前目录
 - AddExtension 是否自动添加默认扩展名
 - CheckFileExists 获取或设置一个值,该值指示如果用户指定不存在的文件名,对话框是否显示警告。
 - CheckPathExists 在对话框返回之前,检查指定路径是否存在
 - Container 控制在将要创建文件时,是否提示用户。只有在ValidateNames为真值时,才适用。
 - DefaultExt 缺省扩展名
 - DereferenceLinks 在从对话框返回前是否取消引用快捷方式
 - FileName 获取或设置一个包含在文件对话框中选定的文件名的字符串。
 - InitialDirector 对话框的初始目录
 - OverwritePrompt 控制在将要在改写现在文件时是否提示用户,只有在ValidateNames为真值时,才适用
 - ShowHelp 启用"帮助"按钮
 - Title 将显示在对话框标题栏中的字符
 - 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、打印预览对话框和打印对话框
1、打印预览对话框(PrintPreviewDialog)属性:
- 
AutoScrollMargin 获取或设置自动滚动边距的大小。
 - 
AutoScrollMinSize 获取或设置自动滚动的最小尺寸。
 - 
DialogResult 获取或设置窗体的对话框结果。
 - 
Document 获取或设置要预览的文档。
 - 
HelpButton 获取或设置一个值,该值指示是否应在窗体的标题框中显示“帮助”按钮。
 
2、打印对话框(PrintDialog)属性:
- 
AllowPrintToFile 禁止或使用"打印到文件"复选框
 - 
AllowSelection 禁止或使用"选定内容"单选框
 - 
AllowSomePages 禁止或使用"页"单选按钮
 - 
Document 从中获取打印机设置的PrintDocument
 
- PrintToFile 打印到文件"复选框是否选中
 - ShowHelp 控制是否显示"帮助"按钮
 - ShowNetWork 控制是否显示"网络"按钮
 
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 模态窗口: ShowDialog():
打开模态窗口后,只要不关闭该窗口,鼠标焦点或者光标就会一直停留在该窗口上。只有关闭该窗口后,调用窗口才能继续。
模态窗口关闭后,仍可以读取模态窗口中的信息,如窗口的返回状态等,以后还可以使用ShowDialog()使其可见。
2 非模态窗口: Show():
打开非模态窗口后,仍可以操作调用窗口。后面的代码立即执行。
关闭非模态窗口,该窗口将不复存在,会释放窗口的所有资源,所以无法得到该窗口的任何信息。常用Hide()方法(等效于Visible=false)然后调用Show()方法使其可见。
3、对话框窗体:Form2
public Form1(string para)//获取参数
{
    InitializeComponent();
    this.StartPosition = FormStartPosition.CenterParent;//启动位置,父窗口中央
    this.MaximizeBox = false;
    this.MinimizeBox = false;
    this.ShowIcon = false;//不显示图标
    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 //定义一个公共属性,供调用窗口Form1使用
{
    get { return this.textBox1.Text + "b"; }
}
private void Form1_Load(object sender, EventArgs e)
{
    if (this.Owner.Name != "Form1")//Owner为调用窗体,即调用改对话框的窗体
        MessageBox.Show("非法调用");
}
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)//对应Form2中的Owner,this为给对话框窗体传值
    this.textBox1.Text = f2.ReturnText;
f2.Close();
f2.Dispose();
到此这篇关于C#对话框Dialog的文章就介绍到这了。希望对大家的学习有所帮助,也希望大家多多支持。
	声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。
		
评论(0)