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.
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