Fade-In Form

  • Thread starter Thread starter Ivan Weiss
  • Start date Start date
I

Ivan Weiss

Hey guys I am trying to fade in a form using the following code:

Public Class frmAbout
Inherits System.Windows.Forms.Form

Dim i As Integer 'Public Variable

#Region The Windows Form code...


Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Timer1.Tick
If i < 100 Then
i = i + 1
Me.Opacity = i / 100
Else
Timer1.Stop()
End If
End Sub
End Class

I tried adjusting my timer to various values (currently 500000 as I am
not sure what this number corresponds to) and my timer is started in my
form initialization code. Yet my form does not fade in, it kinda blinks
and just appears. In the function that creates the form I have:

Dim objAbout = New frmAbout()
objAbout.MdiParent = Me
objAbout.Show()

Any ideas on why my form is not fading in?
(THe opacity is set to 0 in the properties window in design view of the
form)

-Ivan
 
Ivan:

Also, you may want to move the Sleep increment down to 70 or even 50
depending on how fast/slow your machine works, but this can defintiely do it
without flicker.

HTH,

Bill
 
I tried inserting this code into my form_load and removing my other code
utilizing the timer:

Dim i As Double = 0.0

For i = 0.0 To i >= 1.0
Me.Opacity = i
i = i + 0.1
System.Threading.Thread.Sleep(500)
Next
Me.Opacity = 1

However, it does the same thing. It kind of hesitates and than just
shows up, there is no fade in.

-Ivan
 
I think what is happening is that when I call the show method of the
About form from my menu (main MDI Form) it is running this code before
the form actually shows. I am not sure but I think that is what the
problem is.

The structure is:

User Clicks on Help-->About Menu Item
I create a new About form object and set its mdi parent to the main MDI
form.
I than call objAbout.Show()
the code to fade-in my form is in the frmAbout_Load event.

Maybe it is this structure that is giving me my trouble?

-Ivan
 
Change the 500 to 50 for instance. It may be your video card isn't
processing it quickly. I've run this on quite a few machines and at 500
it's somewhat choppy but it's not that bad. At 100 it's pretty smooth and
at 70 it's the exact effect I want. If you go too low, it happens too
quickly. Too slow and it's choppy.

You may also want to play with the increment. I have it set at .1. but you
can change it to .05 for instance, or even .01 to fine tune it. If you use
a smaller increment though, don't use 500 for the thread, it'll be 10 times
as long at .01 and twice as long at .05. I'd move the sleep to 100 first,
if that doesn't do it, try 70, then maybe 50. If it's still choppy, go back
start at like 200 and play with the increment. It's going to be a matter of
playing with it, but remember that you may want to make it adjustable b/c
different video cards draw things at different speeds and the users may want
the ability to manipulate it themseleves.

One other thing.... Try adding a DoEvents in the loop. Normally I refrain
from this, but I'm thinking it may help in this case.

Let me know.

Bill
 
put an application.doevents() in the loop, this causes all the events in the
queue to process... sometimes that holds up paining of forms if you dont do
it when its in a loop
 
I tried moving my code to the activated method after reading a reference
noticing that the form load method runs before the form is painted so
the code would be useless there.

No luck with the following code:

Maybe e-mailing you the actual code would help. I am so confused with
this problem.

Private Sub frmAbout_Activated(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Activated
Dim i As Double = 0.0

While i < 1.0
'Application.DoEvents()
Me.Opacity = i
i = i + 0.01
System.Threading.Thread.Sleep(50)
End While
'Me.Opacity = 1
End Sub

-Ivan
 
do somethign similar to this

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

Dim fadeThread As New System.Threading.Thread(AddressOf fadeIn)

fadeThread.Start()

End Sub



Public Sub fadeIn()

Application.DoEvents()

Me.Refresh()

For x As Double = 0 To 1.0 Step 0.01

Me.Opacity = x

System.Threading.Thread.Sleep(50)

Next

End Sub
 
you will have to set the initial opacity to zero also, using a thread like
this makes the form usable while it fades in also... change the sleep time
to make it go faster also
 
just a little bug to that, the opacity only reaches .99 at the end, you
might want to make a better loop or set the opacity at the end of the fadein
sub to be 1..
also the step will change how much the fade does to, like .01 does 100
levels of fade, while .1 does only 10...
 
Hi Ivan,

That is the answer - I was having the same problem - simply do not set a
parent and the form will fade in just fine.

HTH,

Bernie Yaeger
 
For i = 0.0 To i >= 1.0

This syntax makes no sense to me. The i >= 1.0 clause will evaluate to a
boolean False. So you're really saying For i = 0.0 To False. That makes
no sense to me. Do you have Option Strict On?

Try this:

Dim i as Double

For i = 0.0 to 1.0 Step 0.01
Me.Opacity = i
System.Threading.Thread.Sleep(500)
Next

Does that make any difference?
 
u know that works and all but it will never reach 1.0 ;) stepping by .01
will get you up to .99 then drop out... duno why exactly.. but it does...
look at the code i posted... it only getsup to 99% opacity when i tested it
many times... had to manually set it to 1.0 myself to get 100%
 
Back
Top