|
-
Jan 21st, 2019, 11:51 PM
#1
Thread Starter
Member
How can I make a label fade through colors with a timer?
Hi.
I have a project which I'm working on, and I would like the label to fade through colours for a certain amount of time, until the main form shows. I'm using this code but it's not working (no fade effect).
Code:
Public Class Welcome
Dim Red As Integer = 0
Dim Green As Integer = 0
Dim Blue As Integer = 0
Dim CountUp As Boolean = True
Private Sub Welcome_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
If CountUp = True Then
If Red < 253 Then
Red = Red + 1
Else
CountUp = False
End If
End If
If CountUp = False Then
If Red > 0 Then
Red = Red - 1
Else
CountUp = True
End If
End If
Label1.ForeColor = Color.FromArgb(Red, Blue, Green)
End Sub
End Class
What am I doing wrong here?
-
Jan 21st, 2019, 11:56 PM
#2
Re: How can I make a label fade through colors with a timer?
Do you ever actually start/enable the Timer? If so, do some debugging, i.e. set a breakpoint and step through the code see what it actually does.
-
Jan 21st, 2019, 11:58 PM
#3
Thread Starter
Member
Re: How can I make a label fade through colors with a timer?
Yes it's already enabled through the Timer's settings not code.
-
Jan 22nd, 2019, 12:20 AM
#4
Re: How can I make a label fade through colors with a timer?
By the way, does this call really make sense:
Code:
Label1.ForeColor = Color.FromArgb(Red, Blue, Green)
-
Jan 22nd, 2019, 12:21 AM
#5
Re: How can I make a label fade through colors with a timer?
What do you have the Timer Interval set to?
At its lowest value, 1, the real interval will probably be 64 hz (15.625), so will take 4 seconds or thereabouts to fade in one direction.
If you left it at 100, then it will take 25 seconds or so to fade.
I made it small (1), and made the font larger to see it easier, and saw that it changes between black and red and back at about 8 seconds per cycle.
-
Jan 22nd, 2019, 12:26 AM
#6
Re: How can I make a label fade through colors with a timer?
Is this form created on the same thread as the main form? If so, that's your issue. The UI thread is busy creating your main form so it can't animate your Label at the same time. There is splash screen functionality built into VB.NET Windows Forms so you should use it, outing your Timer and Label on the splash screen. It is created on a separate thread so it can be animated while the startup form is created and displayed.
-
Jan 22nd, 2019, 12:37 AM
#7
Re: How can I make a label fade through colors with a timer?
I would do it this way. Timer set to 1:
Code:
Dim Direction as integer = 1
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Red = Red + Direction
If Red > 254 Or Red < 1 Then Direction = -Direction
Label1.ForeColor = Color.FromArgb(Red, Green, Blue)
End Sub
You could make Direction larger for a faster change but you would then need to reduce the limits (254 and 1) correspondingly.
-
Jan 22nd, 2019, 11:10 AM
#8
Re: How can I make a label fade through colors with a timer?
I've done this a few times. Rather than adding 1, I add a small prime number, as I felt that there are too many colors for people to really see the difference if all you are adding is 1. I'm also adding to R, G, and B, though different amounts to each one, which means that they are going in different directions at all time. The result can be a bit hypnotic. Eventually, every color should be reached, but there is no consistent pattern to it.
The code looks like this:
Code:
Private curB As Integer
Private curR As Integer
Private curG As Integer
Private dirB As Integer = 1
Private dirR As Integer = 1
Private dirG As Integer = -1
Private rd As New Random
Private Sub UpdateBackground()
curB += 13 * dirB
curR += 7 * dirR
curG += 11 * dirG
If curB > 255 Then
curB = 255 - (curB - 255)
dirB = -1
ElseIf curB < 0 Then
curB = -curB
dirB = 1
End If
If curR > 255 Then
curR = 255 - (curR - 255)
dirR = -1
ElseIf curR < 0 Then
curR = -curR
dirR = 1
End If
If curG > 255 Then
curG = 255 - (curG - 255)
dirG = -1
ElseIf curG < 0 Then
curG = -curG
dirG = 1
End If
SomeLabel.BackColor = System.Drawing.Color.FromArgb(curR, curG, curB)
SomeLabel.Refresh()
End Sub
My usual boring signature: Nothing
 
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|