【问题标题】:Insert a line before/after a row during drag&drop in listview在列表视图中拖放期间在一行之前/之后插入一行
【发布时间】:2013-10-21 08:28:55
【问题描述】:

我想在单个列表视图中拖放期间显示一条蓝线。这应该有助于识别拖动的行是在实际行之前还是之后被删除。 (其他需要的事件,如 .DragEnter、.DragDrop 正在按预期工作)。

如何在没有 3rd 方列表视图控件的情况下在 VB.NET 中绘制这样一条蓝线?
到目前为止,我找到了“GiveFeedback”事件:

Sub listView1_GiveFeedback(ByVal sender As Object, ByVal e As GiveFeedbackEventArgs) Handles listView1.GiveFeedback
    e.UseDefaultCursors = False
    Windows.Forms.Cursor.Current = Cursors.Cross

    '<--- Show the blue insertion line until row is dropped

End Sub

有人可以告诉我缺少的蓝色插入线代码吗?

【问题讨论】:

  • 你应该展示你尝试过的东西。您显示的代码几乎不代表所需的任何内容(另外,不确定使用此事件的意义)。您需要跟踪元素被放置的确​​切位置,并在ListView中提出特定项目的格式更改。

标签: vb.net listview drag-and-drop


【解决方案1】:

感谢您为我指明了正确的方向!我在 .DragOver 事件中找到了所需的解决方案:

Sub ListView1_DragOver(sender As Object, e As System.Windows.Forms.DragEventArgs) Handles ListView1.DragOver
    Dim MousePointerCoordinates As Point = ListView1.PointToClient(Cursor.Position)
    Dim RowIndex As Integer = ListView1.InsertionMark.NearestIndex(MousePointerCoordinates)
    ListView1.InsertionMark.Index = RowIndex
End Sub

线条本身的颜色在 form.load 中定义

ListView1.InsertionMark.Color = Color.Blue 

【讨论】:

    【解决方案2】:

    这有望为您指明正确的方向:

    首先,设置ListView1.OwnerDraw = True

    然后添加以下内容:

    Private Sub ListView1_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawListViewItemEventArgs) Handles ListView1.DrawItem
        e.DrawDefault = True
    
        'Do checks here if drag
        Dim Pos As Point = Me.ListView1.PointToClient(Cursor.Position)
    
        If Me.ListView1.GetItemAt(Pos.X, Pos.Y) Is e.Item Then
            e.Graphics.DrawLine(Pens.Blue, e.Bounds.Left, e.Bounds.Top, e.Bounds.Right, e.Bounds.Top)
        End If
    
    End Sub
    

    请注意这是粗略的,您需要添加代码来检查是否发生拖动。移动鼠标时,您还需要使列表视图(或至少列表视图项的区域)无效。

    我建议您考虑创建一个继承自 listview 的新类,因为这样可以更轻松地管理您的代码

    希望这有助于为您指明正确的方向...

    【讨论】:

      【解决方案3】:

      我需要这样的东西,但有点不同。我想要实现的是画一条线,但根据索引的位置,线应该在特定项目之前或之后绘制。

      示例:我有一个包含 5 个项目的列表视图。

      • 场景 1:将项目 4 移动到位置 2。线在项目 2 之前绘制。
      • 场景 2:将项目 1 移动到位置 5。线在项目 5 之后绘制。

      这是我用来实现此目的的代码:

      Private Sub lvDisplayed_DragOver(sender As Object, e As DragEventArgs) Handles lvDisplayed.DragOver
          Dim MousePointerCoordinates As Point = Me.lvDisplayed.PointToClient(Cursor.Position)
          Dim RowIndex As Integer = lvDisplayed.InsertionMark.NearestIndex(MousePointerCoordinates)
          Me.lvDisplayed.InsertionMark.AppearsAfterItem = Me.lvDisplayed.SelectedIndices(0) <= RowIndex
          Me.lvDisplayed.InsertionMark.Index = RowIndex
      End Sub
      

      您还需要设置 InsertionMark.Color ,我已将其设置为 NEW

      Me.lvDisplayed.InsertionMark.Color = Color.DarkRed
      

      希望这会有所帮助,因为我经常寻找这样的东西。

      【讨论】:

        猜你喜欢
        • 2012-01-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多