- MDI父窗体
MDI窗体设计器本身是这个样子
之后首先在父窗体里定义静态全局变量窗体一和窗体二
eg:public static ReadFile readFileForm;
public static StationPic stationPic;
之后用的时候实例化就行
readFileForm = new ReadFile();

- 一些必须的方法
1)判断MDI子窗体是否存在
private bool HaveOpened(Form MdiParentForm,string MdiChildFormType){bool bReturn = false;for(int i = 0;i<MdiParentForm.MdiChildren.Length;i++){if(MdiParentForm.MdiChildren[i].GetType().Name==MdiChildFormType){MdiParentForm.MdiChildren[i].BringToFront();bReturn = true;break;}}return bReturn;}
2)防止同时生成多个同一类型的子窗体
public void OpenChildForm(Form formChild){formChild.Name = formChild.GetType().FullName;bool isOpened = false;foreach (Form form in this.MdiChildren){//如果要显示的子窗体已经在子窗体的子窗体数组中,就把新建的多余的销毁if (formChild.Name == form.Name){form.Activate();formChild.Dispose();isOpened = true;break;}}if (!isOpened){formChild.MdiParent = this;formChild.Show();}}
- 例子1:想让父窗体的打开按钮读取文件并且赋值于子窗体的TextBox中
可以直接将在子窗体里定义一个字符串还有属性
public string StrValue
{
get { return this.Content.Text; }
set { this.Content.Text = value; }
}
在父窗体的打开文件里写读文件的方法
OpenFileDialog dialog = new OpenFileDialog{Multiselect = true,//该值确定是否可以选择多个文件Title = "请选择文件夹",Filter = "文件(*.xml,*.txt)|*.xml;*.txt"};if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK){fileName = dialog.FileName;}if (fileName.Contains("txt")){readFileForm.StrValue = FormatTxtText(dialog.FileName);}}readFileForm.Text = dialog.SafeFileName;
