提问者:小点点

C#表单在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来尝试使用它。我喜欢它,但我不能设置输入的字体,我希望我的密码将显示为********时,键入。这就是为什么我做了我自己的密码表单。


共1个答案

匿名用户

public partial class PasswordInventView : Form

将窗体更改为UserControl。

public partial class PasswordInventView : UserControl

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

如果这不起作用,创建user control,并将PasswordInventView复制到UserControl。Ctrl+Alt+L>右键单击“项目”>“添加”>“新建项”>“选择用户控件(Windows窗体)”>“添加”。将chield表单复制到用户控件。并在main窗体中创建用户控件。

关于用户控件的搜索。