Badiparmagi
Badiparmagi

Reputation: 1285

C# hide form controls using base form class

I have some forms that have menu control on them. This menu control should be visible or invisible based on the user that logged in to the system. I can hide this control using this code block:

    public myForm()
    {
        InitializeComponent();
        myMenu.Visible = CheckUserRole();
    }

It works perfectly. But I have several forms that have the same code structure and menu control. How can I create a base form and inheritance this structure to every form I have?

I have created a base form named BaseForm with a menu control then I call it in my forms like this:

public partial class myFrom : BaseForm
{
    public myForm() : base()
    {
        InitializeComponent();
    }...

but I don't know what to do with my base class?

Can you tell me how can I apply this structure in my project?

Thank you.

Upvotes: 0

Views: 75

Answers (1)

Enigmativity
Enigmativity

Reputation: 117175

I would have thought this would solve it:

public partial class BaseForm : Form
{
    public BaseForm()
    {
        InitializeComponent();
    }

    private void BaseForm_Load(object sender, EventArgs e)
    {
        button1.Visible = DateTime.Now.Millisecond % 2 == 0;
    }
}

public partial class MyForm : WindowsFormsApp7.BaseForm
{
    public MyForm() : base()
    {
        InitializeComponent();
    }

    private void MyForm_Load(object sender, EventArgs e)
    {
        button3.Visible = !button1.Visible;
    }
}

Upvotes: 2

Related Questions