列表删除/删除按钮

时间:2011-08-04 05:04:27

标签: winforms listbox listbox-control

我正在开发一个WinForms应用程序,我想要一个ListBox(或一个提供字符串列表的控件),这样当用户将鼠标悬停在某个项目上时,它将显示该特定项目的删除标志。

WinForms有没有可用的控件来执行此操作?

1 个答案:

答案 0 :(得分:0)

将ListBox DrawMode设置为OwnerDrawFixed(或OwnerDrawVariable),您可以使用鼠标事件自行处理:

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

根据需要重构。