-2

I am trying to make a stained glass window. I choose 8 colors to be added on each rectangle and line to be drawn. The colors will randomly be selected to fill a rectangle/line for each simulation.

Dim cbColorsInit As New List(Of Color) With {Color.Red, Color.Green, Color.Blue, Color.Yellow, Color.Black, Color.White, Color.Cyan, Color.Magenta}

Dim cbControls As New List(Of CheckBox)

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

    addRed(CheckBox1)
    addGreen(CheckBox2)
    addBlue(CheckBox3)
    addYellow(CheckBox4)
    addBlack(CheckBox5)
    addWhite(CheckBox6)
    addCyan(CheckBox7)
    addMagenta(CheckBox8)
End Sub

Private Sub addRed(ByVal cb As CheckBox)
    cbControls.Add(cb)

    cbControls(cbControls.Count - 1).Tag = cbControls.Count - 1
End Sub
Private Sub addGreen(ByVal cb As CheckBox)
    cbControls.Add(cb)

    cbControls(cbControls.Count - 1).Tag = cbControls.Count - 1
End Sub
Private Sub addBlue(ByVal cb As CheckBox)
    cbControls.Add(cb)

    cbControls(cbControls.Count - 1).Tag = cbControls.Count - 1
End Sub
Private Sub addYellow(ByVal cb As CheckBox)
    cbControls.Add(cb)

    cbControls(cbControls.Count - 1).Tag = cbControls.Count - 1
End Sub
Private Sub addBlack(ByVal cb As CheckBox)
    cbControls.Add(cb)

    cbControls(cbControls.Count - 1).Tag = cbControls.Count - 1
End Sub
Private Sub addWhite(ByVal cb As CheckBox)
    cbControls.Add(cb)

    cbControls(cbControls.Count - 1).Tag = cbControls.Count - 1
End Sub
Private Sub addCyan(ByVal cb As CheckBox)
    cbControls.Add(cb)

    cbControls(cbControls.Count - 1).Tag = cbControls.Count - 1
End Sub
Private Sub addMagenta(ByVal cb As CheckBox)
    cbControls.Add(cb)

    cbControls(cbControls.Count - 1).Tag = cbControls.Count - 1
End Sub

Dim ran As New Random
Private Function GetRandomColor() As Color

    Dim chkd As List(Of CheckBox) = cbControls.Where(Function(c) c.Checked).ToList
    Dim rv As Color
    If chkd.Count > 0 Then
        Dim rndnum As Integer = ran.Next(chkd(rndnum).Tag)
        rv = cbColorsInit()
    Else
        Stop
    End If
    Return rv
End Function

this is how i coded in the images above, hope i gave enough information on my problem. i really don't understand why i keep getting an error.

Dan Puzey
  • 33,626
  • 4
  • 73
  • 96
Jones
  • 3
  • 4

2 Answers2

0

Pretty confusing...try this out:

Dim ran As New Random
Dim cbControls As New List(Of CheckBox)

Dim cbColorsInit As New List(Of Color)(New Color() {Color.Red, Color.Green, Color.Blue, Color.Yellow, Color.Black, Color.White, Color.Cyan, Color.Magenta})

Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
    addCB(CheckBox1)
    addCB(CheckBox2)
    addCB(CheckBox3)
    addCB(CheckBox4)
    addCB(CheckBox5)
    addCB(CheckBox6)
    addCB(CheckBox7)
    addCB(CheckBox8)
End Sub

Private Sub addCB(ByVal cb As CheckBox)
    cbControls.Add(cb)
    cb.Tag = cbControls.Count - 1
End Sub

Private Function GetRandomColor() As Color
    Dim chkd As List(Of CheckBox) = cbControls.Where(Function(c) c.Checked).ToList
    Dim rv As Color
    If chkd.Count > 0 Then
        Dim rndnum As Integer = ran.Next(chkd.Count)
        rv = cbColorsInit(chkd(rndnum).Tag)
    Else
        Stop
    End If
    Return rv
End Function
Idle_Mind
  • 38,363
  • 3
  • 29
  • 40
  • i seem to be getting two more Errors "Expression expected", and another one "Comma, ')', or a valid expression continuation expected" any help on those? – Jones Nov 13 '13 at 09:45
  • line 4(Expression expected) and line 6(Comma, ')', or a valid expression continuation expected) – Jones Nov 13 '13 at 13:36
  • You must have lost an underscore "_" or something that allows it span multiple lines. I changed it to one long line, see if that helps. – Idle_Mind Nov 13 '13 at 13:50
0

You appear to be initializing your List(Of Color) incorrectly. Try this:

Dim cbColorsInit As New List(Of Color) From {
    Color.Red, 
    Color.Green, 
    Color.Blue, 
    Color.Yellow, 
    Color.Black, 
    Color.White, 
    Color.Cyan, 
    Color.Magenta
}

Take a look here for more info.

Community
  • 1
  • 1
Ric
  • 12,855
  • 3
  • 30
  • 36
  • i seem to be getting another error "syntax error", "End of statement expected" and a Declaration expected error. – Jones Nov 13 '13 at 09:48
  • So you removed the answer from mine because you couldn't work it out?? – Ric Nov 13 '13 at 14:05
  • no hard feeling but i thought i should mark the one that worked sorry about that if your touched. – Jones Nov 13 '13 at 14:17
  • No hard feelings at all, but both answers that were given were correct with the exception of line continuation. Programming by its nature will lead to many errors that need to be corrected and if this was challenging then worse is to come! – Ric Nov 13 '13 at 14:22