0

iam not good at ENG so you will see some Incorrect spelling and bad grammar. and i am using Visual Studio 2015.

Hello. first of all iam new to programming.

well i got into some trouble with "this.Close();" and i need some help...

i am trying to simply close a loading form after the progress bar hits the value of 100 but it wont work.

here is the code:

 private void pbar_timer_Tick(object sender, EventArgs e)
    {
        progressBar1.Value += 10;

            if(progressBar1.Value==100)
        {
            pbar_timer.Stop();
            new main().ShowDialog();
            this.Close();

        }

    }

Well. i tried many ways but it wont work... the loading form will still hang in the screen and when the main form comes up it wont go.. :|

please help me if you know what should i do...

Thanks.

David Marshal
  • 43
  • 1
  • 1
  • 5
  • Just set a debugger breakpoint on this.Close() and you'll understand what ShowDialog() does a lot better. Why this code is necessary is hard to guess, maybe [this](https://stackoverflow.com/a/10769349/17034). – Hans Passant Mar 20 '20 at 16:01

1 Answers1

2

The ShowDialog() method block your current flow, until new "new main()" is closed. Use Show() instead.

  • thanks. it worked but now it is closing every form. how can i fix that. – David Marshal Mar 20 '20 at 16:17
  • The application will be totally closed if the starting form is closed. DO NOT CLOSE THE STARTING FORM (you can but to achieve it will be more complicated). Try use `Hide()` instead of `Close()`, but you must keep a referrence to that form (a common way is using a static variable, another way is make the starting form a property of `new main()`), so that you can `Show()` it later to `Close()`. Another common way is `this.Hide(); new main().ShowDialog(); this.Show();` – Trần Hoàn Mar 20 '20 at 16:33
  • but how can i unhide an hided form? I used" show() " but it didnt work. – David Marshal Mar 20 '20 at 16:43