在 C# 中使用 Windows 窗体隐藏/阻止选项卡
问题是我有一个“登录窗口”和一个“主窗口”,在按下登录按钮或“访客”按钮后调用,
如果按下登录按钮,整个系统就会出来,如果我按下访客按钮,一个选项卡应该消失或被阻止或其他什么。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
默认情况下,添加到表单的控件不公开可见。除了这个细节之外,您的“attempt1”代码将是正确的代码。
(编辑:要以这种方式修复此问题,请将
tabPage1
的“Modifiers”属性更改为Public
或Internal
- 这允许其他类从表单外部查看这些控件。)但是,比使这些控件可见更好的方法是在
mainwindow
类上创建一个新的公共方法,如下所示: ,在示例代码中,在创建/显示表单后调用新方法:
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 bePublic
orInternal
- 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:Then, in your sample code, call your new method after you create/show the form:
假设您正在为名为 tabControl1 的 tabPage 使用 System.Windows.Forms.TabControl,请使用以下命令:
如果要再次查看 tabPage1,请使用:
Assuming you are using a System.Windows.Forms.TabControl for your tabPages called tabControl1, use the following:
If you want to view the tabPage1 again, use:
您需要通过声明公共属性来公开选项卡控件。
然后您可以使用您的实例
menu
访问它。更好的选择是您在主窗口中公开一个属性
,然后通过
最终应用主窗口表单的加载事件中的逻辑将其设置为 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
and then set it to true or false by
finally apply the logic in load event of the mainwindow form.