Question

I'm struggling with a simple show/hide function for my main form. I needed to work through different permits, where:

  1. If I start my app and no one has logged on, the tab page should dissapear,
  2. At the moment someone has logged on, should show the tab with their respective permits.

I found an answer at this thread and I tried to apply it, but it seems that I miss something.

My code for the function is the followed

public void TabPage1Permission(frmMain formMain, profile myProfile)
{
    if (myProfile.Equals(profile.Visitor))
    {
        formMain.tabPage1.Enabled = false;
        formMain.tabPage1.Visible = false;
    }
    else
    {
        formMain.tabPage1.Enabled = true;
        formMain.tabPage1.Visible = true;
    }
}

In this code:

  • profile is an enum with three values, in which only the value "visitor" doesn't have permits to see TabPage1.

    public enum profile { Visitor = 0, Administrator = 1, Editor = 2 };

  • The function that you see is called in my start code of the main form.

  • This function is in a class apart from the main form which I use to administrate the permits according to the requirements of my app.
  • I have changed the modifiers in all components inside my main form so I can use them out of the main code class of the form.
Was it helpful?

Solution

EDIT: Thanks to the comments of @KingKing, I have found another option in this other thread and I adapted it as it follows:

public void TabPage1Permission(frmMain formMain, profile myProfile, int index)
{
    if (myProfile.Equals(profile.Visitor))
    {
        formMain.tabControl.TabPages.Remove(formMain.TabPage1);
    }
    else
    {
        formMain.tabControl.TabPages.Insert(index, formMain.TabPage1);
    }
}

With this I check if the user is a visitor or not and then I show the tab or not in my main TabControl

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top