-
Oct 17th, 2010, 12:46 PM
#1
Thread Starter
Lively Member
Variable from two strings
i have:
Code:
Public Class Form1
Public tc1 = Color.Blue
Public tc2 = Color.Green
Public tc3 = Color.Orange
Public tc4 = Color.Yellow
Public tc5 = Color.Pink
Public tc6 = Color.Red
Public cSelNum As Integer
Private Sub tcTimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tcTimer.Tick
Dim cSel
If cSelNum = 6 Then
cSelNum = 1
Else
cSelNum += 1
End If
cSel = "tc" & cSelNum.ToString
Label1.ForeColor = cSel
End Sub
End Class
this is supposed to make the text flash different colors. I'm trying to get a certain variable ex: tc6 by taking the current number and putting tc before it. This obviously doesn't work. How can i fix this?
-
Oct 17th, 2010, 12:59 PM
#2
Re: Variable from two strings
There would be ways to make what you are describing work, but they would be kind of painful. Would you be willing to just have an array of colors? If so, then you wouldn't need to have anything other than the integer portion, as that would be the index into the array.
You might also consider that cycling through six colors may not be ideal. I have some code that cycles through every possible color in a kind of random pattern. The way I do it is to have three variables, one for Red, one for Green, and one for Blue. Every time the timer ticks, I add two small, but different, prime numbers to two of the three (I think I used 7 and 11), and subtract a different prime number from the third. When the value of any of the three colors gets over 255 or under 0, I flip the sign of the number that is being added to that color. If I was adding 7, I would begin adding -7. The larger the numbers used, the faster the colors change. It makes a pretty interesting effect, so I use it whenever I want a control to 'flash'. Rather than an annoying flash, I end up with a hypnotic effect.
My usual boring signature: Nothing
-
Oct 17th, 2010, 01:07 PM
#3
Re: Variable from two strings
Your code is not working because you are assigning a string "tc1" or so forth to the forecolor property instead of its value. What I suggest is that you use an array instead of 6 variables.
Here's your same code adapted to use the array of colors
vb.net Code:
Dim tc(6) As Color
Dim cSelNum As Integer
Private Sub tcTimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tcTimer.Tick
tc(1) = Color.Blue
tc(2) = Color.Green
tc(3) = Color.Orange
tc(4) = Color.Yellow
tc(5) = Color.Pink
tc(6) = Color.Red
If cSelNum = 6 Then
cSelNum = 1
Else
cSelNum += 1
End If
Label1.ForeColor = tc(cSelNum)
End Sub
Never try to construct the names of the variables using code to later use them as such and expect to obtain their value.
Last edited by kaliman79912; Oct 18th, 2010 at 01:12 AM.
-
Oct 17th, 2010, 01:23 PM
#4
Thread Starter
Lively Member
Re: Variable from two strings
for all 6 colors, i get:
EDIT: nevermind. fixed.
thanks!
-
Oct 17th, 2010, 01:26 PM
#5
Re: Variable from two strings
Make sure you only declare the array once. the rest of the lines (tc(1)=Color.Blue) etc. are not declarations, they are assigning values to the items in the array so do not put Dim keyword before each line.
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
|