用VB中的索引迭代结构

时间:2013-09-24 08:03:48

标签: vb.net data-structures iteration

我试图通过使用索引迭代结构。除了反思之外,还有一种简单的方法吗?

代码示例;

For j As Integer = 0 To usedRange.ColumnCount - 1
            ws.Cells(0, j).FillColor = Drawing.Color.DarkTurquoise
Next

假设我尝试使用当前索引值为每个单元格指定不同的颜色,例如Drawing.Color(j)

1 个答案:

答案 0 :(得分:0)

如果您想要枚举颜色:

    ' Get all the values from the KnownColor enumeration.
    Dim colorsArray As System.Array = [Enum].GetValues(GetType(KnownColor))
    Dim allColors(colorsArray.Length) As KnownColor
    Array.Copy(colorsArray, allColors, colorsArray.Length - 1)
    With dgvColors
        .ReadOnly = True
        .Columns.Add("ColorName", "ColorName")
        For i = 0 To allColors.Length - 1
            .Rows.Add(allColors(i).ToString)
            .Rows(i).Cells(0).Style.BackColor = Color.FromKnownColor(allColors(i))
        Next
        .Columns("ColorName").Width = 500
        .Rows.RemoveAt(.Rows.Count - 2) ' delete - bug in arrays
        .Rows.RemoveAt(.Rows.Count - 2) ' delete - bug in arrays
        .ClearSelection()
    End With

这是一个为DataGridView添加颜色的黑客 - 不确定这是合法代码,我只是将它用作实用程序。