[RESOLVED] Opacity increase slowly works but form is empty before it reaches max
Hi all o7
Consider following code to achieve a sort of fade-in effect:
Code:
Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles MyBase.Shown
Me.Invoke(Sub()
While Me.Opacity < 0.9
Me.Opacity += 0.00001 'BRIGHT UP BACKGROUND
End While
End Sub)
End Sub
It does the matter but the form is empty (All labels and pictures are invisible apparently) until while loop completes (I guess) How do I rectify this?
- Without Me.Invoke = not even fading.
- Tried "FormLoad" event = not even fading.
- Not a Timer control fan.
- Never tried "InitializeComponent()".
Re: Opacity increase slowly works but form is empty before it reaches max
Temporary worked out with a "Timer" as follows:
Code:
Private Sub FADEIN_Tick(sender As Object, e As EventArgs) Handles FADEIN.Tick
If Me.Opacity < 0.9 Then
Me.Opacity += 0.01
Else
'DO FINISHING THINGS OR CALL A FUNC.
FADEIN.Enabled = False
End If
End Sub
I convinced that when you are pursuing graphical goals, you have to switch over on bad things. Like more control, more inefficient ways to code.
Re: Opacity increase slowly works but form is empty before it reaches max
It’s a matter of threading. If you’re tying up the UI thread with a while loop, it can’t be doing anything else…
Re: Opacity increase slowly works but form is empty before it reaches max
InitializeComponent is a function that is generated for you. I'm not sure what you expected it to do, but you shouldn't be calling it yourself, aside from the one call in the constructor. Calling it at any other time would create some weird results.
A timer is pretty much the only option that seems likely to work for this. You are dealing with the UI, which means the UI thread. The loop you are running will cause the form to at least invalidate, but it never gets a chance to paint, because that would require the loop to pause. It's somewhat possible that you might achieve your goal with Application.DoEvents, and that might be pretty harmless in this particular case, but that's usually a bad thing to be doing. Therefore, the timer is the best option.
Re: Opacity increase slowly works but form is empty before it reaches max
Quote:
Originally Posted by
Shaggy Hiker
InitializeComponent is a function that is generated for you. I'm not sure what you expected it to do, but you shouldn't be calling it yourself, aside from the one call in the constructor. Calling it at any other time would create some weird results.
A timer is pretty much the only option that seems likely to work for this. You are dealing with the UI, which means the UI thread. The loop you are running will cause the form to at least invalidate, but it never gets a chance to paint, because that would require the loop to pause. It's somewhat possible that you might achieve your goal with Application.DoEvents, and that might be pretty harmless in this particular case, but that's usually a bad thing to be doing. Therefore, the timer is the best option.
Your loquacious answer filled in the blanks in my terse reply. I was going to mention DoEvents, but I thought it was better left unsaid without a good explanation… :D
Re: Opacity increase slowly works but form is empty before it reaches max
It's not usually a good suggestion to make, but in this case, where the form really can't be doing anything else, it just might be a good solution. I still prefer the timer, myself. Timers are quite efficient.
Re: Opacity increase slowly works but form is empty before it reaches max
FWIW this worked nicely
Code:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
Me.Visible = False
Me.DoubleBuffered = True
Me.Opacity = 0.0
End Sub
Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles Me.Shown
Me.Visible = True
Dim tsk As Task = Task.Run(Sub()
For x As Integer = 1 To 100
Threading.Thread.Sleep(10)
Me.BeginInvoke(Sub()
Me.Opacity += 0.01
End Sub)
Next
End Sub)
End Sub
Re: Opacity increase slowly works but form is empty before it reaches max
Re: [RESOLVED] Opacity increase slowly works but form is empty before it reaches max
When it come to presentation effects with WinForms... what I have learned.... Just dont do it!
Re: Opacity increase slowly works but form is empty before it reaches max
Quote:
Originally Posted by
pourkascheff
Perfect, thanks.
dBasnett’s answer looks all shiny and new, but in reality, it’s not more efficient than the Timer method.
It’s a secondary thread with 100 cross thread operations, so probably not as efficient as the timer IMO…
Re: Opacity increase slowly works but form is empty before it reaches max
Quote:
Originally Posted by
.paul.
dBasnett’s answer looks all shiny and new, but in reality, it’s not more efficient than the Timer method.
It’s a secondary thread with 100 cross thread operations, so probably not as efficient as the timer IMO…
Even more efficient would be to not do it at all. Its just not necessary.