Private Sub frmDiv_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles MyBase.Load
[COLOR=darkgreen]'list all color names into combobox1[/COLOR]
Dim listColor As New List(Of Color)
Dim c As Color
For Each known As KnownColor In [Enum].GetValues(GetType(KnownColor))
c = Color.FromKnownColor(known)
If Not c.IsSystemColor Then listColor.Add(c)
Next
ComboBox1.DataSource = listColor
ComboBox1.SelectedIndex = 3
End Sub
[COLOR=blue]'click button adds new treenode[/COLOR]
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles Button1.Click
[COLOR=darkgreen]'get a color[/COLOR]
Dim c As Color = ComboBox1.SelectedItem
[COLOR=darkgreen]'check if image exists[/COLOR]
If Not ImageList1.Images.ContainsKey(c.Name) Then
[COLOR=darkgreen]'draw and add to imagelist[/COLOR]
Dim bmp As New Bitmap(16, 16) [SIZE=2][COLOR=#008000]'or: Dim bmp As New Bitmap(ImageList1.ImageSize.Width, ImageList1.ImageSize.Height)
[/COLOR][/SIZE] Dim g As Graphics = Graphics.FromImage(bmp)
Dim rctf As RectangleF = bmp.GetBounds(GraphicsUnit.Pixel)
rctf.Inflate(-1, -1)
g.DrawEllipse(New Pen(c), rctf)
g.Dispose()
ImageList1.Images.Add(c.Name, bmp)
End If
[COLOR=darkgreen]'get image index[/COLOR]
Dim ix As Integer = ImageList1.Images.IndexOfKey(c.Name)
[COLOR=darkgreen]'create treenode and add to treeview[/COLOR]
Static nodeix As Integer
nodeix += 1
Dim nodetext As String = String.Format("node {0} imageindex {1}", nodeix.ToString, ix.ToString)
Dim t As New TreeNode(nodetext, ix, ix)
TreeView1.Nodes.Add(t)
End Sub