successive
successive

Reputation: 49

Show Different tabs in TabControl for the same form

I have two windows form.

  1. Login Screen
  2. Home Screen

On home screen, I have a tab control that have 4 tabs (tab pages).

On my login screen, I have a login functionality which allows user and admin to login. On home screen, in tabControl I have a tab page 'User Management' that I only want to display when admin logs in.

This is the login btn click function I have:

 private void button2_Click(object sender, EventArgs e)
        {
            Form1 mForm = new Form1();

            if (textBox3.Text.ToLower() == "admin" && textBox4.Text.ToLower() == "admin")
            {
                mForm.Show();
                this.Hide();
                
            }
            else
            {
                MessageBox.Show("Please enter valid id and password", "Incorrect Credentials", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
        }

I want to add an else condition that will validate "user" credentials but then I want to hide that one tab page on my Form1 windows form.

Is there any way to do that?

Upvotes: 1

Views: 264

Answers (1)

successive
successive

Reputation: 49

Thanks Mong Zhu. Your comments pushed me to the right direction.

I was able to solve this.

In my Home form I added a

public bool adminLogin = false;

and updated my login click btn as:

private void button2_Click(object sender, EventArgs e)
        {
            Form1 mForm = new Form1();

            if (textBox3.Text.ToLower() == "admin" && textBox4.Text.ToLower() == "admin")
            {
                mForm.adminLogin = true;
                mForm.Show();
                this.Hide();
                
            }
            else if (textBox3.Text.ToLower() == "user" && textBox4.Text.ToLower() == "user")
            {
                mForm.Show();
                this.Hide();
            }
            else
            {
                MessageBox.Show("Please enter valid id and password", "Incorrect Credentials", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
        }

And then in my Home form, in Form Load function, I added a check that if adminLogin is false then remove the tabPage that I did not want to show.

private void Form1_Load(object sender, EventArgs e)
        {
            if (adminlogin == false)
            {
                tabControl2.TabPages.Remove(tabPage3);
            }

        }

Upvotes: 2

Related Questions