Hiding/blocking tabs using windows forms in c#

2019-06-28 00:52发布

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.

3条回答
太酷不给撩
2楼-- · 2019-06-28 00:55

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);
查看更多
你好瞎i
3楼-- · 2019-06-28 01:08

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();
查看更多
时光不老,我们不散
4楼-- · 2019-06-28 01:17

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.

查看更多
登录 后发表回答