我有一个表单1和表单2在窗口形式C#.in从1我有tabcontrol和添加新选项卡,检测和活动选项卡的功能在表单1功能是公共的工作正常,tabcontrol修改器也是公共的。但是,当我在表单2中调用相同的函数时,它不起作用。
// form 1 function
private void button_add_new_Click(object sender, EventArgs e)
{
add_tab(textbox1.text);
}
private void button_active_tab_Click(object sender, EventArgs e)
{
active_tab(textbox1.text);
}
private void button_delect_Click(object sender, EventArgs e)
{
delect(textbox1.text);
}
public void add_tab(string str)
{
TabPage myTabPage = new TabPage(str);
myTabPage.Name = str;
tabControl1.TabPages.Add(myTabPage);
}
public void delect(string st)
{
tabControl1.TabPages.RemoveByKey(st);
}
public void active_tab(string st)
{
tabControl1.SelectTab(st);
}
private void button_form2_show_Click(object sender, EventArgs e)
{
Form2 f = new Form2();
TabPage myTabPage = new TabPage("Form 2 ");
f.TopLevel = false;
myTabPage.Name = "Form 2 ";
myTabPage.Controls.Add(f);
f.Show();
tabControl1.TabPages.Add(myTabPage);
active_tab("Form 2 ");
}
// Form 2
答案 0 :(得分:0)
Pass the instance of Form1 to Form2 in the constructor. Now call the public method in form2 button event by using form1 instance.