如果用户名具有特定特权,则动态打开表单

时间:2018-10-14 17:27:24

标签: c# winforms startup

我有几种不同的形式。启动具有我从数据库中检索到的特定特权的程序的用户,只会打开与该许可相对应的特定形式,而没有任何选择。

在启动时适用于启动形式,如下方在program.cs中所示:

        static SplashScreen mySplashForm;
        static Mainview myMainForm;
        mySplashForm = new SplashScreen();
        if (mySplashForm != null)
        {
            Thread splashThread = new Thread(new ThreadStart(
                () => { Application.Run(mySplashForm); }));
            splashThread.SetApartmentState(ApartmentState.STA);
            splashThread.Start();
        }
        //Create and Show Main Form
        myMainForm = new Mainview();
        myMainForm.LoadCompleted += MainForm_LoadCompleted;
        Application.Run(myMainForm);
        if (!(mySplashForm == null || mySplashForm.Disposing || mySplashForm.IsDisposed))
            mySplashForm.Invoke(new Action(() => {
                mySplashForm.TopMost = true;
                mySplashForm.Activate();
                mySplashForm.TopMost = false;
            }));
    }
    private static void MainForm_LoadCompleted(object sender, EventArgs e)
    {
        if (mySplashForm == null || mySplashForm.Disposing || mySplashForm.IsDisposed)
            return;
        mySplashForm.Invoke(new Action(() => { mySplashForm.Close(); }));
        mySplashForm.Dispose();
        mySplashForm = null;
        myMainForm.TopMost = true;
        myMainForm.Activate();
        myMainForm.TopMost = false;
    }

我没有登录视图,仅使用以下代码在计算机上使用其登录名标识用户:

        public static string username = System.Environment.UserName;
        bool has = agents.Any<AgentItem>(uID => uID.UserId == username && uID.SmeCord == "y");
        if (has == true)

有没有什么好的方法可以让动态的方法从一开始就打开表单。因此,我不需要使用“静态Mainview myMainForm;”。还是需要打开一个表单,然后打开“ this.hide();”?

当bool对myMainForm为true时,已尝试将其绑定为新表单,以便它加载正确的表单。没有任何成功。 我最后一次尝试了以下操作,但仍然没有任何进展。

            bool has = agents.Any<AgentItem>(uID => uID.UserId == username && uID.SmeCord == "y");
        if (has == true)
        {
            Mainview myMainForm;
                            mySplashForm = new SplashScreen();
        if (mySplashForm != null)
        {
            Thread splashThread = new Thread(new ThreadStart(
                () => { Application.Run(mySplashForm); }));
            splashThread.SetApartmentState(ApartmentState.STA);
            splashThread.Start();
        }
        //Create and Show Main Form
        myMainForm = new Mainview();
        myMainForm.LoadCompleted += MainForm_LoadCompleted;
        Application.Run(myMainForm);
        if (!(mySplashForm == null || mySplashForm.Disposing || mySplashForm.IsDisposed))
            mySplashForm.Invoke(new Action(() => {
                mySplashForm.TopMost = true;
                mySplashForm.Activate();
                mySplashForm.TopMost = false;
            }));
        }

但是自从MainForm_LoadCompleted()函数以来,我仍然无法运行它。

目前,我要根据您所拥有的特权打开四种不同的表单。

已经搜索了一段时间,但是没有找到可以帮助我的东西。

我应该如何正确解决此问题?

1 个答案:

答案 0 :(得分:2)

我倾向于这样组织应用程序的方式是调用:

Application.Run(new MainForm())

然后在MainForm_Load事件中,打开SplashScreen。

public void MainForm_Load(object sender, EventArgs e)
{
    this.Hide(); // Hide the mainform
    using (SplashScreen splash = new SplashScreen())
    {
        if (splash.ShowDialog() == DialogResult.OK)
        {
            // Retrieve any properties from the splash screen here.
            this.Show();
        }
        else
        {
            // user doesn't have permission to use the application so close.
            this.Close();
        }
    }
}