Hi everyone,

I want to know if its possible to do the following: I have 2 users and 2 forms. When user A (Admin) logs in on the Log In form, the Main Form appears with ALL its buttons enabled (For editing purposes). BUT when user B (Normal user) logs in the SAME Main From appears BUT the editing buttons are then disabled...

How can I go about this?

All I have at the moment is this:

In the logging in form

private void button1_Click(object sender, EventArgs e)
        {
            if(textBox1.Text == "Admin" && textBox2.Text == "Admin")
            {
                //Load main form

                MainForm f = new MainForm();
                f.ShowDialog();
                //f.Disable(button1);
            }

            else if(textBox1.Text == "User" && textBox2.Text == "User")
            {
                //Load main form with disabled buttons

                MainForm f = new MainForm();
                f.ShowDialog();
                //f.Disable(button1);
            }

            else if (textBox1.Text != "Admin" && textBox2.Text != "Admin")
            {
                MessageBox.Show("Please try logging in correctly for security purposes.");
            }

            else if (textBox1.Text != "User" && textBox2.Text != "User")
            {
                MessageBox.Show("Please try logging in correctly for security purposes.");
            }
        }

Dani AI

Generated

Solid direction already from , and useful alternatives from (separate forms) and (group admin controls). For a small-to-medium WinForms app the cleanest tradeoff is one MainForm that configures itself from a single, explicit role value. That keeps UI layout in one place, avoids duplicated forms, and makes permission logic easy to test and extend.

A simple, robust pattern is: represent roles with an enum or small role object, pass that into MainForm, and run one ApplyPermissions method right after InitializeComponent. This centralizes UI changes and makes future roles easy to add.

public enum Role { User, Admin }

public partial class MainForm : Form
{
    private readonly Role _role;
    public MainForm(Role role) { InitializeComponent(); _role = role; ApplyPermissions(); }

    void ApplyPermissions()
    {
        adminPanel.Visible = (_role == Role.Admin);
        // or iterate controls with a Tag value to toggle many controls
    }
}

Group admin-only buttons inside a container (Panel/GroupBox) or mark them via the Tag property. That makes toggling all admin controls trivial and avoids missing a control later. Prefer hiding (Visible = false) when users shouldn't see a feature at all; use Enabled = false when indicating a feature exists but is not available.

Important security notes: disabling or hiding UI is only a convenience. Any operation that modifies data must still validate the caller’s role in the business/service layer (for example, check the current principal before saving or deleting). Do not rely on client-side UI state for authorization. Also avoid plaintext credential checks; use hashed+salted passwords or a proper auth provider. Finally, choose modal vs modeless presentation deliberately: open the main UI modeless and close the login form if the login is complete, or use a modal login if it must block until success.

Recommended Answers

All 3 Replies

Member Avatar for Member #46692

Best to have two forms rather than suppressing textboxes and fields, it could get messy as your application grows. That's my personal opinion.

If you must use one form you can make the Admin controls unique, maybe have them in a panel. Now it's a simple matter of making the panel disabled and invisible to disable the the admin controls.

Hi Guys, thanks for the replies. All I did was to alter the load function of the form with Admin and User security settings. So if a Admin is logging in the buttons are all set to enabled. And if the User is logging in the buttons are disabled. Here is the simple code.

Thanks again!

private void MainForm_Load(object sender, EventArgs e)
{
    dataGridView1.DataSource = _HogHouse;

        //Read the current available data in the list that has been saved previously
        _HogHouse.ReadDataFromFile();

        if (Globals.mSecurityLevel == 'U')
        {
            button1.Enabled = false;
            button2.Enabled = false;
        }
        else if (Globals.mSecurityLevel == 'A')
        {
            button1.Enabled = true;
            button2.Enabled = true;
        }
}  
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.