prob with array of colors from vb6 to vb.net

B

B

Hello,

I am trying to migrate a vb6 app to vb.net. In one piece
of the vb6 app I have a label where the label backcolor
changes every 5 seconds on a timer. So I created an array
of colors as follows and then run the array in the timer
event in vb6. Not working in vb.net. How would I do this
in vb.net?

vb6 code:
-----------------------------------------
Const ORANGISH As Long = &HC0C0FF
Const ORYEL As Long = &HC0E0FF
Const YELLOWISH As Long = &HC0FFFF
Const GREENISH As Long = &HC0FFC0
Const LIGHTBLUISH As Long = &HFFFFC0
Const PURPLISH As Long = &HFFC0C0
Const PINKISH As Long = &HFFC0FF
Const WHITE As Long = &HFFFFFF
Dim arrColors As Variant

Private Sub Form_Load()
arrColors = Array(ORANGISH, ORYEL, YELLOWISH, LIGHTBLUISH,
PURPLISH, PINKISH, WHITE)
End Sub


Private Sub Timer1_Timer()
Static i
lblRunning.BackColor = arrColors(i)
i = i + 1
If i > 5 Then i = 0
End Sub
------------------------------------------------------

Here is my vb.net conversion
-----------------------------------------
constants...
Dim arrColors() As Long = {ORANGISH, ORYEL, YELLOWISH,
GREENISH, LIGHTBLUISH, PURPLISH, PINKISH, WHITE}

Private Sub Timer1_Tick(...) Handles Timer1.Tick
Static i As Integer
lblRunning.BackColor = CType(arrColors(i),
System.Drawing.Color)
i += 1
End Sub
-------------------------------------------------

The error message for vb.net says that it cannot convert a
long (arrColors(i)) to System.Drawing.Color. So how could
I achieve the same functionality in the vb.net app as in
the vb6 app above?

Thanks
 
C

Cor Ligthert

B,

I think that this sample shows the most easy
(I took only three colors)

\\\
Private arrColors(2) As Drawing.Color
Private WithEvents mytimer As New Windows.Forms.Timer
Private Sub Form2_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
mytimer.Interval = 5000
mytimer.Enabled = True
arrColors(0) = Drawing.Color.Yellow
arrColors(1) = Drawing.Color.Blue
arrColors(2) = Drawing.Color.Red
Label1.BackColor = Drawing.Color.Red
End Sub
Private Sub mytimer_Tick(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles mytimer.Tick
Static i As Integer
Label1.BackColor = arrColors(i)
i = i + 1
If i > 2 Then i = 0
End Sub
///

I hope this helps

Cor
 
B

B

Thanks. That was the trick. (for the sake of my own
posterity I thought I would add this comment) Actually, to
keep the array of colors as numbers I had to do it this
way:

lblRunning.BackColor = ColorTranslator.FromOle(CType
(arrColors(i), Integer))

To use ColorTranslator.FromHtml I would have to set each
constant as a String and create a String array.

Const WHITE As String = "&HFFFFFF"
....

Either way works fine. Many thanks for your help.
 
H

Herfried K. Wagner [MVP]

B said:
I am trying to migrate a vb6 app to vb.net. In one piece
of the vb6 app I have a label where the label backcolor
changes every 5 seconds on a timer. So I created an array
of colors as follows and then run the array in the timer
event in vb6. Not working in vb.net. How would I do this
in vb.net?

Store your colors in 'Color' instances directly. You can use
'Color.FromArgb' to construct a color from RGB data, or you can use
predefined colors available in 'Color.*'.
 
B

B

Yes. This is the best way. I was thinking about FromArgB
but wasn't sure about the implementation. Here is what I
found on the net which gives me a whole variety of colors

Private m_Rnd As New Random

Public Function RandomRGBColor() As Color
Return Color.FromArgb(255, m_Rnd.Next(0, 255), _
m_Rnd.Next(0, 255), m_Rnd.Next(0, 255))
End Function

Thanks for this suggestion. I am amazed at how there are
so many different ways to accomplish something in vb.net.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top