1

任何人都可以建议什么是调用进程的更好方法,而无需在 c# menustrip 中的每个菜单项中进行编码

    private void SetupUserToolStripMenuItem_Click(object sender, EventArgs e)
    {

        UserMenu += "Setup User";
        UserRights=ValidateUser(UserMenu);

        if (UserRights == 1)
        {
            frmDataReader frmDR = new frmDataReader();
            frmDR.Show();
        }
        else
        {

            MessageBox.Show("No Permission");
        }

    private void SetupMasterToolStripMenuItem_Click(object sender, EventArgs e)
    {

        //MessageBox.Show(menuStrip1.  ToolStripMenuItem); 
        UserMenu += "Setup Master";
        UserRights=ValidateUser(UserMenu);

        if (UserRights == 1)
        {
            frmDataReader frmDR = new frmDataReader();
            frmDR.Show();
        }
        else
        {

            MessageBox.Show("No Permission");
        }
4

2 回答 2

1

为所有点击事件附加相同的事件处理程序,无需为每个按钮编写相同的代码。

于 2012-07-17T05:51:05.447 回答
1
{
    setupMasterToolStripMenuItem.Click += ToolStripMenuItem_Click;
    setupUserToolStripMenuItem.Click += ToolStripMenuItem_Click;
}


ToolStripMenuItem_Click(object sender, EventArgs e)
{
    ToolStripMenuItem toolStripMenuItem = ((ToolStripMenuItem) sender);
    // may be like this
    // or you can check 'setupUser/setupMasterToolStripMenuItem' 
    // is equal to 'toolStripMenuItem' or check 'Tag' of them
    UserMenu += toolStripMenuItem .Text;


    UserRights=ValidateUser(UserMenu);

    if (UserRights == 1)
    {
        frmDataReader frmDR = new frmDataReader();
        frmDR.Show();
    }
    else
    {
        MessageBox.Show("No Permission");
    }
}
于 2012-07-17T05:55:13.920 回答