ComboBox Item Select

ComboBox Item Select

最近有朋友問到

我有一個ComboBox的下拉,那我滑鼠移過去的話能不能把值先呈現在旁邊(利用label來呈現)

 

這個是非常簡單的   只要2個Step

Step1:

在ComboxBox的DrawMode屬性設成OwnerDrawFixed

Step 2:

在ComboBox1的DrawItem事件寫如下的程式碼

 


    Private Sub ComboBox1_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles ComboBox1.DrawItem

        e.DrawBackground()

        e.Graphics.DrawString(ComboBox1.Items(e.Index).ToString(), e.Font, Brushes.Black, e.Bounds.X, e.Bounds.Y)

        If e.State = DrawItemState.Selected Then

            Me.lblText.Text = ComboBox1.Items(e.Index).ToString()

        End If

    End Sub

 


reference

http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=3562789&SiteID=1