Search code examples
c#winformsuser-interfacedashboard

C# forms prompt password when clicking a menu in a dashbaord


I designed a dashboard form that contains buttons. Some of these buttons have sub-menus. Since I'm planning to limit the access to some sub-menus, I want that every time a user clicks a restricted sub-menu, a form will appear that will prompt the user to enter the password before accessing the restricted page.

enter image description here

I already made a password form and programmed that when a sub-menu is clicked, the password form will appear. However, once the correct password is submitted, it will look like this:

enter image description here

I want that it will look like this when password is correct: enter image description here

I want the overlapping form placed on the original position / panel. How to do that?

Here's the code for the dashboard:

public CFMenu()
        {
            InitializeComponent();
            customizeDesign();
        }
        private void customizeDesign()
        {
            panelInventory.Visible = false;
            panelSales.Visible = false;
        }
        private void hideSubMenu()
        {
            if(panelInventory.Visible == true)
                panelInventory.Visible = false;
            if(panelSales.Visible == true)
                panelSales.Visible = false;
        }
        private void showSubMenu(Panel subMenu)
        {
            if(subMenu.Visible == false)
            {
                hideSubMenu();
                subMenu.Visible = true;
            }
            else
                subMenu.Visible = false;
        }
        private void CFMenu_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'casaFrancaDataSet.Sales' table. You can move, or remove it, as needed.
            this.salesTableAdapter.Fill(this.casaFrancaDataSet.Sales);
            timer1.Start();
            label4.Text = DateTime.Now.ToLongTimeString();
            label5.Text = DateTime.Now.ToLongDateString();
        }
        private void btnInventory_Click(object sender, EventArgs e)
        {
            showSubMenu(panelInventory);
        }
        private void btnSales_Click(object sender, EventArgs e)
        {
            showSubMenu(panelSales);
        }
        private void button1_Click(object sender, EventArgs e)
        {
            openPanelForm(new CFInventoryAdd());
            hideSubMenu();
        }
        private void button2_Click(object sender, EventArgs e)
        {
            openPanelForm(new PasswordInventView());
            hideSubMenu();


            //string password = Interaction.InputBox("Enter Password: ", "Inventory View");
            //if(password == "hello")
            //{
            //    openPanelForm(new CFInventoryView());
            //    hideSubMenu();
            //}
            //else
            //{
            //    MessageBox.Show("Incorrect Password!");
            //}  
        }
        private void button3_Click(object sender, EventArgs e)
        {
            openPanelForm(new CFInventoryUpdate());
            hideSubMenu();
        }
        private void button7_Click(object sender, EventArgs e)
        {
            openPanelForm(new CFSalesAdd());
            hideSubMenu();
        }
        private void button6_Click(object sender, EventArgs e)
        {
            openPanelForm(new CFSalesView());
            hideSubMenu();
        } 
        private void button8_Click(object sender, EventArgs e)
        {
            openPanelForm(new CFCalculate());
            hideSubMenu();
        }
        private void button5_Click(object sender, EventArgs e)
        {
            openPanelForm(new CFSalesUpdate());
            hideSubMenu();
        } 
        private void button9_Click(object sender, EventArgs e)
        {
            openPanelForm(new CFReport());
            hideSubMenu();
        }
        private void timer1_Tick(object sender, EventArgs e)
        {
            label4.Text = DateTime.Now.ToLongTimeString();
            timer1.Start();
        }
        private Form activeForm = null;
        private void openPanelForm(Form childForm)
        {
            if (activeForm != null)
                activeForm.Close();
            activeForm = childForm;
            childForm.TopLevel = false;
            childForm.FormBorderStyle = FormBorderStyle.None;
            childForm.Dock = DockStyle.Fill;
            panelForm.Controls.Add(childForm);
            panelForm.Tag = childForm;
            childForm.BringToFront();
            childForm.Show();
        }

Here's a sub-menu that requires a password:

private void button2_Click(object sender, EventArgs e)
        {
            openPanelForm(new PasswordInventView());
            hideSubMenu();
        }

Here's the code for the Password Prompt form:

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

        private void button1_Click(object sender, EventArgs e)
        {
            var password = txtPassword.Text;

            if (password == "1234")
            {
                CFInventoryView f2 = new CFInventoryView();
                f2.Show();
                this.Visible = false;
            }
            else
            {
                MessageBox.Show("Username or Password is incorrect!");
                txtPassword.Clear();
            }
        }

Before making the password form, I tried using the InputDialog from VisualBasic by referencing it on my project. I liked it however I can't set the font of the input, and I wanted that my password will appear as ******** when typed. That's why I made my own password form.


Solution

  • public partial class PasswordInventView : Form
    

    Change form to UserControl.

    public partial class PasswordInventView : UserControl
    

    And create user control in main form.

    If this not works go create user control and copy PasswordInventView to UserControl. Ctrl + Alt + L > Right click project > Add > New Item > Select User Control (Windows Forms) > Add. Copy chield form to user control. And create user control in main form.

    Search about user control.