1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
| Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
' Définir le style du ComboBox et la méthode de dessin
ToolStripComboBox1.ComboBox.DropDownStyle = ComboBoxStyle.DropDown
ToolStripComboBox1.ComboBox.DrawMode = DrawMode.OwnerDrawFixed
ToolStripComboBox1.ComboBox.ItemHeight = 23
ToolStripComboBox1.Items.Add("h m s")
AddHandler ToolStripComboBox1.ComboBox.DrawItem, New DrawItemEventHandler(AddressOf ToolStripComboBox1_DrawItem)
AddHandler ToolStripComboBox1.ComboBox.MouseClick, New MouseEventHandler(AddressOf ToolStripComboBox1_MouseClick)
End Sub
Private Sub ToolStripComboBox1_DrawItem(sender As Object, e As DrawItemEventArgs)
e.DrawBackground()
Dim itemText As String = CType(ToolStripComboBox1.Items(e.Index), String)
e.Graphics.DrawString(itemText, ToolStripComboBox1.Font, Brushes.Black, e.Bounds.Left, e.Bounds.Top)
Dim crossRect As New Rectangle(e.Bounds.Right - 20, e.Bounds.Top, 15, 15)
e.Graphics.DrawString("❌", New Font("Arial", 12, FontStyle.Bold), Brushes.Red, crossRect.Left - 5, crossRect.Top + 2)
e.DrawFocusRectangle()
End Sub
Private Sub ToolStripComboBox1_MouseClick(sender As Object, e As MouseEventArgs)
Dim index As Integer = ToolStripComboBox1.SelectedIndex
If index >= 0 Then
Dim itemBounds As Rectangle = GetItemRectangle(ToolStripComboBox1, index)
Dim crossRect As New Rectangle(itemBounds.Right - 20, itemBounds.Top, 15, 15)
If crossRect.Contains(e.Location) Then
ToolStripComboBox1.ComboBox.Cursor = Cursors.Hand
ToolStripComboBox1.Items.RemoveAt(index)
If ToolStripComboBox1.Items.Count > 0 Then
ToolStripComboBox1.SelectedIndex = ToolStripComboBox1.Items.Count - 1
Else
ToolStripComboBox1.ComboBox.Cursor = Cursors.Default
ToolStripComboBox1.SelectedIndex = -1
End If
End If
End If
End Sub
Private Function GetItemRectangle(Cb As ToolStripComboBox, index As Integer) As Rectangle
Dim itemHeight As Integer = Cb.DropDownHeight
Dim top As Integer = index * itemHeight
Return New Rectangle(0, top, Cb.Width, itemHeight)
End Function |
Partager