开发者

List with remove/delete button

开发者 https://www.devze.com 2023-03-24 22:44 出处:网络
I am developing a WinForms application and 开发者_开发问答I want a ListBox (or a control which provides list of strings) such that when the user hovers the mouse over an item it will show a delete sig

I am developing a WinForms application and 开发者_开发问答I want a ListBox (or a control which provides list of strings) such that when the user hovers the mouse over an item it will show a delete sign for that particular item.

Is there any control available for WinForms to do this?


Setting the ListBox DrawMode to OwnerDrawFixed (or OwnerDrawVariable), you can just handle this yourself with the Mouse events:

Public Class Form1

  Private _MouseIndex As Integer = -1

  Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
    ListBox1.Items.Add("String #1")
    ListBox1.Items.Add("String #2")
  End Sub

  Private Sub ListBox1_DrawItem(ByVal sender As Object, ByVal e As DrawItemEventArgs) Handles ListBox1.DrawItem
    e.DrawBackground()

    If e.Index > -1 Then
      Dim brush As Brush = SystemBrushes.WindowText
      If (e.State And DrawItemState.Selected) = DrawItemState.Selected Then
        brush = SystemBrushes.HighlightText
      End If
      e.Graphics.DrawString(ListBox1.Items(e.Index), e.Font, brush, e.Bounds.Left + 20, e.Bounds.Top)

      If e.Index = _MouseIndex Then
        e.Graphics.DrawString("X", e.Font, brush, e.Bounds.Left + 2, e.Bounds.Top)
      End If
    End If

  End Sub

  Private Sub ListBox1_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) Handles ListBox1.MouseDown
    If _MouseIndex > -1 AndAlso ListBox1.IndexFromPoint(e.Location) = _MouseIndex AndAlso e.Location.X < 20 Then
      Dim index As Integer = _MouseIndex
      If MessageBox.Show("Do you want to delete this item?", "Confirm", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.Yes Then
        ListBox1.Items.RemoveAt(index)
        ListBox1.Invalidate()
      End If
    End If
  End Sub

  Private Sub ListBox1_MouseLeave(ByVal sender As Object, ByVal e As EventArgs) Handles ListBox1.MouseLeave
    If _MouseIndex <> -1 Then
      _MouseIndex = -1
      ListBox1.Invalidate()
    End If
  End Sub

  Private Sub ListBox1_MouseMove(ByVal sender As Object, ByVal e As MouseEventArgs) Handles ListBox1.MouseMove
    Dim index As Integer = ListBox1.IndexFromPoint(e.Location)

    If index <> _MouseIndex Then
      _MouseIndex = index
      ListBox1.Invalidate()
    End If
  End Sub

End Class

Refactor as needed.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号