在对象初始值设定项中初始化的字段或属性的名称必须以

时间:2013-11-13 07:45:54

标签: arrays vb.net winforms combobox

我正在尝试制作彩色玻璃窗。我选择在每个矩形和要绘制的线上添加8种颜色。将随机选择颜色以填充每个模拟的矩形/线。

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

这就是我在上面的图片中编码的方式,希望我能提供足够的信息来解决我的问题。我真的不明白为什么我一直都会收到错误。

2 个答案:

答案 0 :(得分:0)

相当令人困惑......试试这个:

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

答案 1 :(得分:0)

您似乎错误地初始化了List(Of Color)。试试这个:

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

看看here 了解更多信息。