No trošku som to upravil. Nezabudnúť na properties pre ComboBox DrawMode na OwnerDrawVariable
Public Class Form1
Dim CurrentColor As Color
Dim farba(10) As Color
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
farba(1) = Color.DarkCyan
farba(2) = Color.Yellow
farba(3) = Color.Red
For i = 1 To 20
ComboBox1.Items.Add(i)
Next
End Sub
Protected Sub Combobox1_MeasureItem(ByVal sender As Object, ByVal e As System.Windows.Forms.MeasureItemEventArgs) Handles ComboBox1.MeasureItem
'nahodna vyska poloziek
Dim myRandom As New Random
e.ItemHeight = myRandom.Next(25, 35)
End Sub
Protected Sub Combobox1_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles ComboBox1.DrawItem
If e.Index < 0 Then
e.DrawBackground()
e.DrawFocusRectangle()
Exit Sub
End If
' vlastné farby
CurrentColor = CType(farba(e.Index Mod 3 + 1), Color)
' get a square using the bounds height
Dim SizeRect As Rectangle = New Rectangle(2, e.Bounds.Top + 2, e.Bounds.Width, e.Bounds.Height - 2)
Dim ComboBrush As Brush
' call these methods first
e.DrawBackground()
e.DrawFocusRectangle()
' change brush color if item is selected
If e.State = Windows.Forms.DrawItemState.Selected Then
ComboBrush = Brushes.Yellow
Else
ComboBrush = Brushes.Green
End If
' draw a rectangle and fill it
e.Graphics.DrawRectangle(New Pen(CurrentColor), SizeRect)
e.Graphics.FillRectangle(New SolidBrush(CurrentColor), SizeRect)
' draw a border
SizeRect.Inflate(1, 1)
e.Graphics.DrawRectangle(Pens.Red, SizeRect)
' draw the Color name
e.Graphics.DrawString(ComboBox1.Items(e.Index), ComboBox1.Font, ComboBrush, e.Bounds.Height + 5, ((e.Bounds.Height - ComboBox1.Font.Height) \ 2) + e.Bounds.Top)
End Sub
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
ComboBox1.BackColor = CurrentColor
End Sub
End Class
|