Hiding / locking tabs using window forms in C #

The fact is that I have a "login window" and "mainwindow", which is called after pressing the login button or the "VISITOR" button

If you press the login button, the whole system will exit, and if I press the VISITANT button, one tab should disappear or be locked or something like that.

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 try1,

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

and the one I get to use try2,

Error 1 The type or namespace name "tabPage1" does not exist in the namespace "System.Windows.Forms" (do you miss the assembly reference?)

as you probably guessed, "tabPage1" is the tab that I need to hide when I click the visitor button.

I can’t come up with more details, I will be there to provide additional information.

Thanks in advance.

+4
source share
3 answers

The controls you add to your form are not public by default. Your code "try1" will be the correct code, with the exception of this detail.

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

However, a better approach than visualizing these controls is to create a new public method in your mainwindow class, something like this:

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

Then in your code example, call the new method after creating / displaying the form:

  mainwindow menu = new mainwindow(); menu.Show(); menu.HideTab(); 
+3
source

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

 tabControl1.TabPages.Remove(tabPage1); 

If you want to view tabPage1 again, use:

 tabControl1.TabPages.Add(tabPage1); 
0
source

you need to open the tab control by declaring a public property. You can then access it using menu , which is an instance.

It’s better to choose that you open the property in mainwindow, for example

 public bool ShowTabPage1 { get; set; } 

and then set true or false to

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

finally apply the logic in the mainwindow form load event.

0
source

Source: https://habr.com/ru/post/1308023/


All Articles