How to 'fade in' a Form when i show it using ShowDialog?

  • Thread starter Thread starter babylon
  • Start date Start date
B

babylon

I think I need to do something like:
this.Opacity = 0;
for (int i=0;i<100; i++)
{
this.Opacity = i;
System.Threading.Thread.Sleep(10);
}

but, where should I put it?
thx....
 
* "babylon said:
I think I need to do something like:
this.Opacity = 0;
for (int i=0;i<100; i++)
{
this.Opacity = i;
System.Threading.Thread.Sleep(10);
}

For example, in the form's 'Load' event handler. Notice that you first
have to show the form and then start the "fade in".
 
I tried, it didn't work,
the form displayed in black for 1 second before it is showed normally....
i tried to attach the codes to the activated event....same when the form is
loaded for the first time.
but it works afterward....
 
Did you set the opacity of the form in the designer? Also, when referring
to opacity in code, the value must be between 0 and 1. Here is code that
worked for me (VS.Net 2003):

Set the forms opacity in the designer to whatever the start value is,
otherwise the form is shown fully opaque for an instant. Here I used 4%



Private Sub Form1_Activated(...) Handles MyBase.Activated
For x As Decimal = .04 To 1.0 Step 0.04
Me.Opacity = x
Me.Refresh()
Next
End Sub

I guess in C# it would be (untested):

for (decimal i=0.04;i<1.0; i+=0.04)
{
this.Opacity = i;
this.Refresh();
System.Threading.Thread.Sleep(10);
}

Others may have a better way, though.
 
Hi,

The following code is tried using a timer event.

bool blnCloseInitiated = false;

double checkValue = -1;

private void Form2_Load(object sender, System.EventArgs e) {

checkValue = 1;

this.Opacity = 0;

timer1.Start();

}

private void timer1_Tick(object sender, System.EventArgs e) {

timer1.Stop();

if (this.Opacity != checkValue) {

this.Opacity += checkValue == 1 ?.05 :-0.1;

// Console.WriteLine(this.Opacity);

if (blnCloseInitiated && this.Opacity == checkValue) {

this.Close();

}

timer1.Start();

}

}

private void Form2_Closing(object sender, System.ComponentModel.CancelEventArgs e) {

if (!(blnCloseInitiated)) {

this.Enabled = false;

blnCloseInitiated = e.Cancel = true;

checkValue = 0.0;

timer1.Start();

}

}


Regards,
Sankalp


babylon said:
I tried, it didn't work,
the form displayed in black for 1 second before it is showed normally....
i tried to attach the codes to the activated event....same when the form is
loaded for the first time.
but it works afterward....
 
Back
Top