There are 2 forms, one of which is a form filled with one picture (Form3), the second (Form1) is a main one. When you click on the button in Form1 - Maine collapses this way, opening F3:

Form3 formButton = new Form3(); formButton.Owner = this; formButton.Show(); formButton.Opacity = 0; while (this.Opacity != 0) { Thread.Sleep(30); this.Opacity -= 0.1; } while (formButton.Opacity != 1) { Thread.Sleep(30); formButton.Opacity += 0.1; } 

The problem is that when you appear while the loop is running, there is nothing on the form but holes from the elements. The elements themselves are loaded only when the program exits the loop.

Here is the reverse return of Maine:

 formMain.Opacity = 0; formMain.Show(); while (formMain.Opacity != 1) { Thread.Sleep(30); formMain.Opacity += 0.1; } while (this.Opacity != 0) { Thread.Sleep(30); this.Opacity -= 0.1; } 

Maybe someone has a simple or not a solution to this problem. I would like the whole interface to appear smoothly, not just the form, and at the end instantly the elements.

  • I think there is a little wrong to do. Tell what goals you are pursuing. Just creating 2 windows is very rarely justified, they usually hang up a dialogue or even build a whole business inside one form. And then it turns out that you asked the owner for the window, but you are trying to hide the owner, then why did you ask? - Arheus
  • It is necessary that the program was hidden from the screen, and left behind a tail for which it can be opened. I was thinking how to implement it in one form, but I didn’t have enough knowledge. The only presentation of the solution for one form is not suitable for owners of 2 or more monitors ( - V. Muz
  • And what should she do during the hidden / open state? - Arheus
  • Nothing hidden and open. It is simply Important telephone numbers for the company, for emergency communication. - V. Muz
  • one
    This winForms as I understand? The initialization of the components in the constructor is sitting, check. here Form3 formButton = new Form3 (); should already be initialized. However, drawing does not work,. the thread is busy. Call for forced rendering in a loop. Or opasity change the timer (more correct option), ie. not in the main thread. - Arheus

1 answer 1

As already mentioned in the comments, while c Thread.Sleep loops work inside the GUI thread, which causes the form to "freeze".

Let's do it with a timer.

There are several different timers in dotnet, we need one that is in the System.Windows.Forms.Timer - it can work with controls on the form directly, since its Tick event is called on the same thread.

Add two fields to your main form class:

 System.Windows.Forms.Timer timer; Form3 formButton; 

And the timer event handler:

 private void Timer_Tick(object sender, EventArgs e) { if (Opacity > 0) this.Opacity -= 0.1; else if (formButton.Opacity < 1) formButton.Opacity += 0.1; else { timer.Stop(); timer.Tick -= Timer_Tick; } } 

Now your code will look like this:

 formButton = new Form3(); formButton.Owner = this; formButton.Show(); formButton.Opacity = 0; timer = new System.Windows.Forms.Timer(); timer.Interval = 30; timer.Tick += Timer_Tick; timer.Start(); 

Notice that the form's Opacity property is of type double . Therefore, I replaced exact comparisons != 0 and != 1 with > 0 and < 1 - this is due to the specifics of the representation of real numbers - an exact comparison may not work.

Also pay attention to the need to unsubscribe the timer from the event: timer.Tick -= Timer_Tick; - This is necessary to avoid memory leaks.

If you wish, you can create a timer in the form designer by setting the necessary properties and events for it.