窗体应用程序
非模态对话框
弹出后用户可继续在应用程序中进行其他操作,通常用于显示用户需要经常访问的控件和数据。
非模态对话框使用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)