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

发布于 2024-08-30 10:31:46 字数 869 浏览 11 评论 0原文

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

如果按下登录按钮,整个系统就会出来,如果我按下访客按钮,一个选项卡应该消失或被阻止或其他什么。

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();
}

我使用 attempts1 时遇到的错误是

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

而我使用 attempts2 得到的对象引用是

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

因为您可能已经猜到“tabPage1”是我在按下访客按钮时需要隐藏的选项卡。

我想不出更多的细节,我会提供任何额外的信息,

提前致谢。

The thing is that i have a 'log in window' and a 'mainwindow' that is called after pressing the log in button or the "VISITANT" button

If pressing the log in button, the whole system will come out, and if i press the VISITANT button, one tab should disappear or be blocked or something.

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();
}

the errors i get for using the attempt1 are

Error 1 'System.mainwindow.tabPage1' is inaccessible due to its protection level'
Error 2 An object reference is required for the non-static field, method, or property 'System.mainwindow.tabPage1'

and the one i get for using the attempt2 is

Error 1 The type or namespace name 'tabPage1' does not exist in the namespace 'System.Windows.Forms' (are you missing an assembly reference?)

as you probably have guessed "tabPage1" is the tab i need to hide when pressing the visitant button.

I can't think of any more details, I will be around to provide any extra information

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

葵雨 2024-09-06 10:31:46

默认情况下,添加到表单的控件不公开可见。除了这个细节之外,您的“attempt1”代码将是正确的代码。

编辑:要以这种方式修复此问题,请将 tabPage1 的“Modifiers”属性更改为 PublicInternal - 这允许其他类从表单外部查看这些控件。)

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

public void HideTab()
{
   tabPage1.Enabled = false;
   tabPage1.Visible = false;
}

: ,在示例代码中,在创建/显示表单后调用新方法:

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

The controls you add to your form are, by default, not publicly visible. Your "attempt1" code would be the correct code, except for this detail.

(EDIT: to fix it this way, change the "Modifiers" property of tabPage1 to be Public or Internal - this allows other classes to see those controls from outside the form.)

However, a better approach than making these controls visible would be to create a new public method on your mainwindow class, something like this:

public void HideTab()
{
   tabPage1.Enabled = false;
   tabPage1.Visible = false;
}

Then, in your sample code, call your new method after you create/show the form:

 mainwindow menu = new mainwindow();
 menu.Show();
 menu.HideTab();
眼泪淡了忧伤 2024-09-06 10:31:46

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

tabControl1.TabPages.Remove(tabPage1);

如果要再次查看 tabPage1,请使用:

tabControl1.TabPages.Add(tabPage1);

Assuming you are using a System.Windows.Forms.TabControl for your tabPages called tabControl1, use the following:

tabControl1.TabPages.Remove(tabPage1);

If you want to view the tabPage1 again, use:

tabControl1.TabPages.Add(tabPage1);
噩梦成真你也成魔 2024-09-06 10:31:46

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

更好的选择是您在主窗口中公开一个属性

public bool ShowTabPage1 { get; set; }

,然后通过

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

        this.Hide();
}

最终应用主窗口表单的加载事件中的逻辑将其设置为 true 或 false。

you need to expose the tab control by declaring a public property.
Then you can access it using menu which is you instance.

Better option is that you Expose a property in mainwindow like

public bool ShowTabPage1 { get; set; }

and then set it to true or false by

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

        this.Hide();
}

finally apply the logic in load event of the mainwindow form.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文