Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 848
Reyhaneh Sharifzadeh Avatar asked Aug 03 '14 13:08

Reyhaneh Sharifzadeh


People also ask

How do I open another form in Winforms?

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.

How do I toggle between forms in C#?

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.


1 Answers

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
like image 170
evilmandarine Avatar answered Oct 01 '22 01:10

evilmandarine