Search code examples
c#winformsstartup

Open form dynamically if username have specific privileges


I have several different forms. The user who launches the program that has a specific privilege that I retrieve from a database just opens a specific form that corresponds to that permission without getting any options.

Applies to a splash form at startup that looks like below in 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;
    }

I have no login view and only identifies the user using their login name on the computer with the following code:

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

Is there any good way to get a more dynamic way to open forms right from the start. So I do not need to use "static Mainview myMainForm;" or need to open a form and then "this.hide ();"?

Have tried to bind a new form when bool is true to myMainForm so it loads the correct form. Without any success. Last tries i did the following and still no progress.

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

But then I still won't be able to run it since MainForm_LoadCompleted() function.

For the moment, I have four different forms that I want to open depending on the privileges you have.

Have searched around for a while now but have not managed to find something that can help me.

How should I solve this on a correct way?


Solution

  • The way I tend to organize applications like this is to call:

    Application.Run(new MainForm())
    

    Then in the MainForm_Load event I open the 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();
            }
        }
    }