I have a form that should be shown by clicking on a button in main form and I want when users close the second form, main form be shown again on center of the screen. I used below codes to do 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 be shown with a small jump, like a blink! (It's not continuous , it's just at the first.) What I want is do this smoothly, without any blink at the first. How is it possible?
Thanks in advance.
Now go to Solution Explorer and select your project and right-click on it and select a new Windows Forms form and provide the name for it as form2. Now click the submit button and write the code. When you click on the submit button a new form will be opened named form2.
The trick is to use Application. Run() without parameters and Application. Exit() at the point where you want to exit the application. Now when you run the application, Form1 opens up.
Try setting the opacity smoothly (main form must have the AllowTransparency
property to true
). This is very basic way to do it synchronously (blocking the main thread) but as it will last very little time this should be ok; 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 you can do the same to hide the other form: just set initial opacity to 1.00 instead of 0.00 and then decrement ( -= ) instead of incrementing in the loop:
form2.Opacity -= opacity
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With