Windows应用程序进阶2(非模态对话框 通用对话框)

本文介绍了Windows应用程序中非模态对话框和通用对话框的使用,包括如何添加、设置对话框,实现主窗体与对话框的交互,以及详细讲解了消息框、打开/保存文件对话框、颜色选择对话框、字体对话框和打印组件的使用方法及实例。
摘要由CSDN通过智能技术生成

窗体应用程序

非模态对话框

弹出后用户可继续在应用程序中进行其他操作,通常用于显示用户需要经常访问的控件和数据。
非模态对话框使用Show()方法显示,模态对话框使用ShowDialog()方法显示

添加、设置对话框

添加窗体ModalessDialogBox
设置属性与模态对话框类似,其中ControlBox为true,FormBorderStyle为FixedToolWindow。
添加控件,不需添加两个按钮。

添加对话框属性

在ModalessDialogBox.cs文件中添加两个属性,为只写
具体代码:

        public Color DataColor
        {
   
            set
            {
   
                //清空radioButton选中标记
                radioButton1.Checked = false;
                radioButton2.Checked = false;
                radioButton3.Checked = false;
                if(value == Color.Red)
                    radioButton1.Checked = true;
                if(value == Color.Yellow)
                    radioButton2.Checked = true;
                else if(value == Color.Blue)
                    radioButton3.Checked= true;
            } 
        }
        public string DataTitle
        {
   
            set
            {
   
                textBox1.Text = value;
            } 
        }

实现控件功能

非模态对话框经常与其他窗体进行交互操作,所以当对话框的状态发生改变时需要实时通知其他窗体(模态非模态的很大区别),即非模态对话框不用点击确定,即可在主窗体上反映对话框中的设置。

主窗体标题与文本框同步
        private void textBox1_TextChanged(object sender, EventArgs e)
        {
   
            //得到父窗体
            Form1 fatherForm = (Form1)this.Owner;
            if(fatherForm != null)
                fatherForm.Text = textBox1.Text;
        }
每个选择发生改变时重新进行判定
        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
   
            if (radioButton1.Checked)
            {
   
                //得到父窗体
                Form1 fatherForm = (Form1)this.Owner;
                //设置父窗体
                fatherForm.BackColor = Color.Red;
            }
        }

        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {
   

            if (radioButton2.Checked)
            {
   
                Form1 fatherForm = (Form1)this.Owner;
                fatherForm.BackColor = Color.Yellow;
            }
        }

        private void radioButton3_CheckedChanged(object sender, EventArgs e)
        {
   
            if (radioButton3.Checked)
            {
   
                Form1 fatherForm = (Form1)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值