c#中其他表单的访问选项卡控件 [英] access tab control for other form in c#

查看:39
本文介绍了c#中其他表单的访问选项卡控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个窗体 1 和窗体 2 在窗口窗体 C#.in 从 1 我有 tabcontrol 和添加新选项卡、检测和活动选项卡的功能,它们在窗体 1 功能中工作正常是 public ,tabcontrol 修饰符也是 public .但是当我在表单 2 中调用相同的函数时,它不起作用.

I have a form 1 and form 2 in window form C#.in from 1 i have tabcontrol and function of add new tab , detect , and active tab which work fine in form 1 function are public , tabcontrol modifier is also public . But when i call same function in form 2 it does not work.

// 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 

推荐答案

在构造函数中将 Form1 的实例传递给 Form2.现在使用 form1 实例调用 form2 按钮事件中的公共方法.

Pass the instance of Form1 to Form2 in the constructor. Now call the public method in form2 button event by using form1 instance.

这篇关于c#中其他表单的访问选项卡控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆