Hiding/blocking tabs using windows forms in c#

Audel picture Audel · Apr 26, 2010 · Viewed 9.4k times · Source

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.

Answer

Dan Puzey picture Dan Puzey · Apr 26, 2010

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