Search code examples
c#winformsprogresssplash-screen

Splashscreen - Accessing Label and Progressbar


I have some trouble accessing the ProgressBar and a certain Label in from my splashScreen. I made an own Form for the screen. In my Form1 I have the following method:

    private void sign_Click(object sender, EventArgs e)
    {
        splashScreen splScreen = new splashScreen();
        Thread thrd = new Thread(new ThreadStart(loadingScreenStart));
        thrd.Start();
        splScreen.percentage.Text = "0%";
        var logIn = new LogIn(this);
        logIn.checkUserInput(this);

        thrd.Abort();
    }

      public void loadingScreenStart()
    {
        Application.Run(new splashScreen());
    }

In my LogIn class I did:

       public String checkUserInput(object sender)
    {
        splashScreen splScreen = new splashScreen();
        //won't change my Label and PrpgressBar

I know it's probably because I create a new instance of the Form so it's empty but how to do this right? I don't know... Hope someone can help me.


Solution

  • Same requirement I too had where I need to use the same object but was having a limitation where i cannot use the static class. For that i have created a static object of the class and using lock. Try if it solves your requirement.

        private static splashScreen m_instance = null;
        private static object m_instanceLock = new object();
    
        public static splashScreen GetInstance()
        {
            lock (m_instanceLock)
            {
                if (m_instance == null)
                {
                    m_instance = new splashScreen();
                }
            }
            return m_instance;
        }
    

    And whenever you want to create the object or access the already created object, you just need to give as:

    SomeClass someobj= SomeClass.GetInstance();