How to show the form "smoothly" a second time in winforms?

I have a form that needs to be shown by clicking on the button in the main form, and I want users to close the second form, and the main form is displayed again in the center of the screen. I used the codes below for this:

private void button_Click(object sender, EventArgs e) { this.Hide(); //Hides the main form . form2.ShowDialog(); //Shows the second form . this.Show(); // Re-shows the main form after closing the second form ( just in the taskbar , not on the screen ) . this.StartPosition = FormStartPosition.CenterScreen; // I write this code because I want to show the main form on the screen , not just in the taskbar . } 

these commands do what I want, but the problem is that after closing the second form, the main form will be shown with a small jump, like blinking! (It is not continuous, it is only on the first.) I want to do it smoothly, without any blinks at the first. How is this possible?

Thanks in advance.

+6
source share
1 answer

Try adjusting the transparency smoothly (the main form should have the AllowTransparency property for true ). This is a very simple way to do this synchronously (blocking the main thread), but since it will last very short, it should be fine; No need for Application.DoEvents ():

 double opacity = 0.00; while (opacity < 1) { Opacity = opacity; // update main form opacity - transparency opacity += 0.04; // this can be changed } Opacity = 1.00 // make sure Opacity is 100% at the end 

--- EDIT

Note that you can do the same to hide a different shape: just set the initial opacity to 1.00 instead of 0.00 and then decrease (- =) instead of increasing in the loop:

 form2.Opacity -= opacity 
+3
source

Source: https://habr.com/ru/post/973277/


All Articles