1

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?

ProgrammingLlama
  • 36,677
  • 7
  • 67
  • 86
successive
  • 49
  • 3
  • How are exactly are you checking in your Login if a person is a User or an Admin? – IndieGameDev Nov 18 '20 at 07:57
  • I am using hard coded "admin" and "user". I am not looking at this from security point of view. I am just looking for a way to hide one tab age if user id and password are "admin" – successive Nov 18 '20 at 07:59
  • it would be logical that your `Form1` receives the information about admin or user from the login form. You would need a property in `Form1` that can be set from the login form – Mong Zhu Nov 18 '20 at 08:01
  • 1
    as for the hiding of a tab page, you cannot do this. you would need to remove it programmatically from the tab control. As is explained in [this answer](https://stackoverflow.com/a/3365490/5174469) – Mong Zhu Nov 18 '20 at 08:03

1 Answers1

2

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);
            }

        }
successive
  • 49
  • 3