这是FORM1的代码: private void 添加阶层ToolStripMenuItem_Click(object sender, EventArgs e)
{
MainForm Mform = new MainForm();
TreeNodeForm tnForm = new TreeNodeForm(Mform);
tnForm.ShowDialog();
//this.sbCom.AddGroup(this.tstbName.Text);
//List<string> itemList = new List<string>();
//for (int i = 0; i < this.sbCom.Groups.Count; i++)
//{
// this.tscobDeleteCom.Items.Add(this.sbCom.Groups[i].Text);
//}
} public void AddGroup(string name)
{
this.sbCom.AddGroup(name);
}这是FORM2的代码public TreeNodeForm(MainForm MForm)
{
InitializeComponent();
this.MForms = MForm;
} private MainForm MForms; private void button1_Click(object sender, EventArgs e)
{
//try
//{
// MainFile mf = new MainFile();
// mf.Title = this.txtTreenode.Text;
// this.MForms.MC1.Channels.Add(mf.Title, mf);
// this.MForms.loadChannel();
//}
//catch (Exception ex)
//{
// MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
//}
if (Checknull())
{
this.MForms.AddGroup(this.txtTreenode.Text);
this.Close();
}
}注释的地方可以忽视现在的要求是在FORM2上面调用FORM1的一个方法,从而能够达成添加层叠的效果
可为什么我达不到这个效果呢?
{
MainForm Mform = new MainForm();
TreeNodeForm tnForm = new TreeNodeForm(Mform);
tnForm.ShowDialog();
//this.sbCom.AddGroup(this.tstbName.Text);
//List<string> itemList = new List<string>();
//for (int i = 0; i < this.sbCom.Groups.Count; i++)
//{
// this.tscobDeleteCom.Items.Add(this.sbCom.Groups[i].Text);
//}
} public void AddGroup(string name)
{
this.sbCom.AddGroup(name);
}这是FORM2的代码public TreeNodeForm(MainForm MForm)
{
InitializeComponent();
this.MForms = MForm;
} private MainForm MForms; private void button1_Click(object sender, EventArgs e)
{
//try
//{
// MainFile mf = new MainFile();
// mf.Title = this.txtTreenode.Text;
// this.MForms.MC1.Channels.Add(mf.Title, mf);
// this.MForms.loadChannel();
//}
//catch (Exception ex)
//{
// MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
//}
if (Checknull())
{
this.MForms.AddGroup(this.txtTreenode.Text);
this.Close();
}
}注释的地方可以忽视现在的要求是在FORM2上面调用FORM1的一个方法,从而能够达成添加层叠的效果
可为什么我达不到这个效果呢?
Form1 里面会有
public static frm;
public void 你的方法();
Form1的构造函数中 加条 frm = this;Form2 中调用 Form1.frm.你的方法();手工写的,自己看看。
{
Form2 frm = new Form2(this);
frm.Owner=this;
} pulbic void a()
{
//-----
}
Form2 Form1 frmM;
public Form2(Form1 frmm)
{
InitializeComponent();
frmM=frmm;
}
void b()
{
frmM.a(); //调用Form1中的方法a
}
sbCom是一个层叠控件
AddGroup是一个添加层的方法,这个是没有问题的
MainForm Mform = new MainForm();
是创建了一个新的对象,而不是当前对象
private void 添加阶层ToolStripMenuItem_Click(object sender, EventArgs e)
{
//MainForm Mform = new MainForm();这句代码应该不要
TreeNodeForm tnForm = new TreeNodeForm(this);//这里应该传当前窗体实例this,而不是你原来的新实例Mform对象
tnForm.ShowDialog();
//this.sbCom.AddGroup(this.tstbName.Text);
//List<string> itemList = new List<string>();
//for (int i = 0; i < this.sbCom.Groups.Count; i++)
//{
// this.tscobDeleteCom.Items.Add(this.sbCom.Groups[i].Text);
//}
}