Calling form after loading screen

Asked

Viewed 1,453 times

3

I have already checked some answers that have been made about closing a form after logging in and so on. I have tested the too many methods in my role, but I was unsuccessful.

So my problem does not consist of a login but a screen with a progression.

I am doing the following: I have a form that presents a screen "loading" (progressiBar) that after having loaded the entire bar, it should close this form and call a second form containing data.

The problem is, I can’t close the loading form (it is the program startup form). If you know any method ...

//primeiro form
public Form1()
{
   InitializeComponent();
}
private void timer1_Tick(object sender, EventArgs e)
{
   if (progressBar1.Value < 100)
   {
      progressBar1.Value = progressBar1.Value +2;
   }
   else if (progressBar1.Value == 100)
   {
      timer1.Enabled = false;
      Form2 f2 = new Form2();
      f2.Show(); // aqui chamo o segundo form
      //this.Close() // tentei essa função para fechar o Form1, mas sem sucesso também
   }
}
  • Consider your main form the form that will be open in the application, and not the form loading, before you open the main form, you display and close the loading

3 answers

2

In your main project, in case the project is set to boot your application, in the Program.Cs class write the code as follows that will work.

static class Program
{
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);

        FrmLogin frmLogin = new FrmLogin();

        if (frmLogin.ShowDialog() == DialogResult.OK)
            Application.Run(new FrmPrincipal());
    }
}

In this case I treat the screen as a dialog box and according to the return of the login screen button I open or not the main screen.

Create this other method and call it inside the constructor

    private void InitializeTimer()
    {
        // Call this procedure when the application starts.
        // Set to 1 second.
        timer1.Interval = 1000;
        timer1.Tick += new EventHandler(timer1_Tick);

        // Enable timer.
        timer1.Enabled = true;
    }

Being as follows:

     public Form1()
     {
         InitializeComponent();

         InitializeTimer();
     }

And this method that you had created:

    private void timer1_Tick(object sender, EventArgs e)
    {
        if (progressBar1.Value < 100)
        {
            progressBar1.Value = progressBar1.Value + 10;
        }
        else if (progressBar1.Value == 100 && timer1.Enabled)
        {
            timer1.Enabled = false;
            this.DialogResult = DialogResult.OK;
        }
    }

I tested and it worked.

  • Okay, I had tried this method before. The problem is that I want to leave the loading screen where there is only one progression and hide it or even close it to only show the main screen.

  • I made a new adjustment to work like you said

2


Simple, just do it:

private void timer1_Tick(object sender, EventArgs e)
{
   if (progressBar1.Value < 100)
   {
      progressBar1.Value = progressBar1.Value +2;
   }
   else if (progressBar1.Value == 100)
   {
      timer1.Enabled = false;
      Hide();
      Form2 f2 = new Form2();
      f2.Show(); // aqui chamo o segundo form
   }
}
  • Really very simple indeed, I appreciate the help !!

  • The Hide() method does not close the form as @user56742 had commented on the reply, it just hides the same.

  • Yes, but it’s like I closed the same, I believe it’s okay to use Hide in this problem.

  • Yes, problem does not have, is that the question was not clear there gave to imply that you wanted to close and not hide the form.

0

I didn’t quite understand your question. But do you want to start another form first instead of login, which is the initial of your program? If that’s the question, just change it http://prntscr.com/f6dpkg

Browser other questions tagged

You are not signed in. Login or sign up in order to post.