Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在c中使用windows窗体隐藏/阻止选项卡#_C#_Tabcontrol_Winforms_Tabpage - Fatal编程技术网

C# 在c中使用windows窗体隐藏/阻止选项卡#

C# 在c中使用windows窗体隐藏/阻止选项卡#,c#,tabcontrol,winforms,tabpage,C#,Tabcontrol,Winforms,Tabpage,问题是,我有一个“登录窗口”和一个“主窗口”,按下“登录”按钮或“访问者”按钮后会调用该窗口 如果按“登录”按钮,整个系统将显示出来,如果我按“访问”按钮,一个选项卡将消失或被阻止或其他 private void visitant(object sender, EventArgs e) { mainwindow menu = new mainwindow(); menu.Show(); //mainwindow.tabPage1.Enabled

问题是,我有一个“登录窗口”和一个“主窗口”,按下“登录”按钮或“访问者”按钮后会调用该窗口

如果按“登录”按钮,整个系统将显示出来,如果我按“访问”按钮,一个选项卡将消失或被阻止或其他

private void visitant(object sender, EventArgs e)
{
        mainwindow menu = new mainwindow();
        menu.Show();

        //mainwindow.tabPage1.Enabled = false; //attempt1
        //mainwindow.tabPage1.Visible = false; //attempt1

        //System.Windows.Forms.tabPage1.Enabled = false;//attempt2
        //System.Windows.Forms.tabPage1.Visible = false;//attempt2

        this.Hide();
}
我在使用attempt1时遇到的错误有

错误1“System.mainwindow.tabPage1”由于其保护级别而不可访问“
错误2非静态字段、方法或属性“System.mainwindow.tabPage1”需要对象引用

我使用attempt2得到的是

错误1命名空间“System.Windows.Forms”中不存在类型或命名空间名称“tabPage1”(是否缺少程序集引用?)

正如您可能已经猜到的,“tabPage1”是我在按下visitant按钮时需要隐藏的选项卡

我想不出更多的细节,我会提供更多的信息


提前感谢。

假设您对名为tabControl1的tabPages使用System.Windows.Forms.TabControl,请使用以下命令:

tabControl1.TabPages.Remove(tabPage1);
如果要再次查看选项卡Page1,请使用:

tabControl1.TabPages.Add(tabPage1);

默认情况下,添加到表单中的控件不公开。您的“attempt1”代码将是正确的代码,除了此详细信息

EDIT:若要以这种方式进行修复,请将
tabPage1
的“Modifiers”属性更改为
Public
Internal
-这允许其他类从表单外部查看这些控件。)

但是,与使这些控件可见相比,更好的方法是在
mainwindow
类上创建一个新的公共方法,如下所示:

public void HideTab()
{
   tabPage1.Enabled = false;
   tabPage1.Visible = false;
}
然后,在示例代码中,在创建/显示表单后调用新方法:

 mainwindow menu = new mainwindow();
 menu.Show();
 menu.HideTab();

您需要通过声明公共属性来公开选项卡控件。 然后您可以使用
菜单
访问它,这是您的实例

更好的选择是在mainwindow中公开属性,如

public bool ShowTabPage1 { get; set; }
然后通过

private void visitant(object sender, EventArgs e)
{
        mainwindow menu = new mainwindow();
        menu.ShowTabPage1 = false;
        menu.Show();         

        this.Hide();
}

最后在主窗口窗体的加载事件中应用逻辑。

并没有解决他的问题-OP正在努力解决tabControl的可见性问题,而不是如何使用它(第一次尝试几乎是正确的)。