父窗体内容,需要在子窗体获取的方法为
CheckFormIsOpen
private frmMenu frmMenu = new frmMenu(); public frmMain() { InitializeComponent(); } private void frmMain_Load(object sender, EventArgs e) { frmMenu.P_fm = this; frmMenu.Show(this.dockPanelMain); } public bool CheckFormIsOpen(string formname) { bool remark = false; for (int a = 0; a < base.MdiChildren.Length; a++) { if (formname.Equals(base.MdiChildren[a].GetType().Namespace + "." + base.MdiChildren[a].GetType().Name)) { base.MdiChildren[a].Activate(); remark = true; } } return remark; }
子窗体:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using WeifenLuo.WinFormsUI.Docking; namespace e24tt { public partial class frmMenu : DockContent { public frmMenu() { InitializeComponent(); } //FormSub: private frmMain fm; public frmMain P_fm { get { return fm; } set { fm = value; } } private void frmMenu_Load(object sender, EventArgs e) { trvMenu.ExpandAll(); } private void trvMenu_AfterSelect(object sender, TreeViewEventArgs e) { try { //FormSub: //在需要对其调用者(父)刷新时 if (this.trvMenu.SelectedNode.Text != null) { if (this.trvMenu.SelectedNode.Tag != null) { string formname = this.trvMenu.SelectedNode.Tag.ToString(); if (!fm.CheckFormIsOpen(formname)) { fm.OpenChildForm(formname); } } } fm.Text = "24TT设置 之 " + this.trvMenu.SelectedNode.Text; } catch { } } } }
这个方法解决了父子窗体传值问题
属性法
//FormFather:
//需要有一个公共的刷新方法
public void Refresh_Method()
{
//...
}
//调用时
FormSub f2 = new FormSub() ;
f2.P_F1 = this; //重点,赋值到子窗体对应属性
f2.Show() ;
//FormSub:
private FormFather p_f1;
public FormFather P_F1
{
get{return p_f1;}
set{p_f1 = value;}
}
//刷新时
p_f1.Refresh_Method() ;