单击仪表板中的菜单时 C# 窗体提示密码

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

我设计了一个包含按钮的仪表板表单。其中一些按钮有子菜单。由于我打算限制对某些子菜单的访问,因此我希望每次用户单击受限子菜单时,都会出现一个表单,提示用户在访问受限页面之前输入密码。

我已经制作了一个密码表格并编程为当点击一个子菜单时,密码表格将会出现。但是,一旦提交了正确的密码,它将如下所示:

我希望密码正确时看起来像这样:

我要重叠表格放在原来的位置/面板上。怎么做?

这是仪表板的代码:

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

这是一个需要密码的子菜单:

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

这是密码提示表单的代码:

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

在制作密码表单之前,我尝试通过在我的项目中引用 VisualBasic 中的 InputDialog 来使用它。我喜欢它,但是我无法设置输入的字体,而且我希望我的密码在输入时显示为 ********。这就是我制作自己的密码表格的原因。

public partial class PasswordInventView : Form

将窗体更改为 UserControl。

public partial class PasswordInventView : UserControl

并在主窗体中创建用户控件。

如果这不起作用,请创建用户控件并将 PasswordInventView 复制到 UserControl。 Ctrl + Alt + L > 右键单击​​项目 > 添加 > 新项 > Select 用户控件(Windows 表单)> 添加。将子窗体复制到用户控件。并在主窗体中创建用户控件。

搜索用户控件。