2011-06-14 39 views
1

我想所有的刷子的循環,這基本上是僞代碼,這將解釋什麼是我想要做的事:通過「刷」

For Each B in Brushes 
    'Something with Brushes 
End For 

刷是一種類型,但這樣我怎麼就能夠要做到這一點?

+0

所有刷子添加到列表並循環訪問 – 2011-06-14 21:30:10

回答

3

不使用反射:

Dim brushes = [Enum].GetValues(GetType(KnownColor)) _ 
    .Cast(Of KnownColor)() _ 
    .Where(Function(k) k >= KnownColor.Transparent AndAlso k < KnownColor.ButtonFace) _ '//Exclude system colors 
    .Select(Function(k) New SolidBrush(Color.FromKnownColor(k))) 

編輯(托馬斯評論)

爲了獲得顏色名稱(用於刷)

Dim brushColorNames = [Enum].GetValues(GetType(KnownColor)) _ 
    .Cast(Of KnownColor)() _ 
    .Where(Function(k) k >= KnownColor.Transparent AndAlso k < KnownColor.ButtonFace) _ '//Exclude system colors 
    .Select(Function(k) k.ToString()) 
+0

+1遠比我的答案好 – 2011-06-15 06:17:38

4

你能做到這一點嗎?

dim brush as new Brush() 'needs a proper brush instance, not sure where there is one, so this line won't work 
Dim type As Type = GetType(System.Drawing.Brushes) 
Dim properties As PropertyInfo() = type.GetProperties(BindingFlags.Static) 
For Each [property] As PropertyInfo In properties 
    Console.WriteLine("{0} = {1}", [property].Name, [property].GetValue(brush, Nothing)) 
Next 
+2

您需要導入System.Reflection – 2011-06-14 21:32:36

+1

需要是'type.GetProperties(BindingFlags.Static或BindingFlags.Public)'else不會返回任何東西 – Magnus 2011-06-14 21:42:34

+1

我認爲在這種情況下,你應該在兩個參數中都用Nothing來調用GetValue。你沒有從你創建的畫筆中獲得價值。 – NerdFury 2011-06-14 21:46:17

2

這取決於你想要用刷子做什麼。

For Each b in GetType(Brushes).GetProperties 
    Dim colorName = b.Name ' If you want color names (AliceBlue through YellowGreen) 
    Dim brushValue = b.GetValue(Nothing, Nothing) ' Gives you a Brush 
    Dim brushColor = brushValue.Color ' Gives you the hex color of the brush (AliceBlue = #FFF0F8FF) 
Next 
+0

Colorname是我想要的,謝謝你:D! – 2011-06-14 22:48:30

+0

@Thomas如果顏色是你想要的,你應該枚舉顏色枚舉而不是畫筆,請檢查我的答案。 – Magnus 2011-06-14 22:52:30

+0

我需要畫筆,我指的是他的變量名colorName :) – 2011-06-14 23:41:25