C#中的Dialog对话框怎么用

发布时间:2022-05-13 15:57:03 作者:iii
来源:亿速云 阅读:701

这篇文章主要讲解了“C#中的Dialog对话框怎么用”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“C#中的Dialog对话框怎么用”吧!

一、MessageBox弹出框

MessageBox.Show(<字符串> Text, <字符串> Title, <整型> nType,MessageBoxIcon);

MessageBoxButtons类型:

MessageBoxIcon图标样式:

举例

MessageBox.Show("用户名或者密码不能为空");
MessageBox.Show("用户名或者密码不能为空","登录提示");
MessageBox.Show("用户名或者密码不能为空","登录提示",MessageBoxButtons.OKCancel);
MessageBox.Show("用户名或者密码不能为空","登录提示",MessageBoxButtons.OKCancel,MessageBoxIcon.Exclamation);

二、WinForm自带对话框

除PrintPreviewDialog外,所有的对话框都继承于抽象类CommonDialog。

CommonDialog的继承结构

CommonDialog的方法

1、打开文件对话框(OpenFileDialog)

基本属性

示例

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)

属性

示例

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)属性:
2、打印对话框(PrintDialog)属性:
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对话框怎么用”的内容了,经过本文的学习后,相信大家对C#中的Dialog对话框怎么用这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!

推荐阅读:
  1. 自定义Dialog对话框
  2. Android对话框Dialog 的一点小问题

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

dialog

上一篇:Java IO流创建读取与写入操作是什么

下一篇:golang怎么读取ini、json、yaml配置文件

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》