建立子窗体MDI,并且确保不要重复打开子窗体

 

//直接上代码form1

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

//form1:
//委托方式子窗口和父窗口交换数据
namespace WindowsFormsApp1
{

public delegate void ShowMessageService(string msg);//定义一个委托
public partial class Form1 : Form
{

Form3 f3 = null;
private Form2 childForm = new Form2 ();
public Form1()
{
InitializeComponent();
this.IsMdiContainer = true ;
}
private void Form1_Load(object sender, EventArgs e)
{

childForm.showMessageChild = new ShowMessageService(UpdateLabel1);//这个是子窗体定义
childForm.Show();
}
public void UpdateLabel1(string msg)
{

this.textBox1 .Text = msg;
}
private void button1_Click(object sender, EventArgs e)
{

ShowMessageService sms = new ShowMessageService(childForm.UpdateLabel2);
this.BeginInvoke(sms, textBox1 .Text);

}

 

private void button2_Click(object sender, EventArgs e)
{

 


}

private void settingToolStripMenuItem_Click(object sender, EventArgs e)
{
if(f3 ==null|| f3.IsDisposed)  //这里确保不要重复打开子窗体
{
this.textBox1.Visible = false;
this.button1.Visible = false;
this.button2.Visible = false;
this.FormBorderStyle = FormBorderStyle.Sizable;

Form3 myForm3 = new Form3();
myForm3.MdiParent = this;
myForm3.WindowState = FormWindowState.Maximized;//子窗体最大化
f3 = myForm3;
myForm3.Show();


}
else
{
f3.Activate();///激活显示
}


}

private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{


this.Close();


}

private void 测试ToolStripMenuItem_Click(object sender, EventArgs e)
{

 

 


}
}
}

 

//form2

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;


//form2:
//委托方式子窗口和父窗口交换数据
namespace WindowsFormsApp1
{
public partial class Form2 : Form
{

public ShowMessageService showMessageChild;
public Form2()
{
InitializeComponent();
}

private void Form2_Load(object sender, EventArgs e)
{

}
public void UpdateLabel2(string msg)
{

this.textBox1 .Text = msg;
}

private void button1_Click(object sender, EventArgs e)
{

this.BeginInvoke(showMessageChild, textBox1 .Text );

 

}

private void button2_Click(object sender, EventArgs e)
{

 

 

}
}
}

 

 

//form3

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
public partial class Form3 : Form
{


public Form3()
{
InitializeComponent();
//this.MaximizeBox = false;
// this.MinimizeBox = false;
this. ControlBox = false;
this.FormBorderStyle = FormBorderStyle.None;

}

private void Form3_Load(object sender, EventArgs e)
{


}

private void button1_Click(object sender, EventArgs e)
{
DialogResult dr= MessageBox.Show("保存成功");//弹出对话框,等待用户确认
if (dr == DialogResult.OK) this.Close();
}

private void button2_Click(object sender, EventArgs e)
{

this.Dispose();
}
}
}

 

posted @ 2020-08-11 11:38  Jiangxuhua  阅读(48)  评论(0)    收藏  举报