Vb.net 在拖动过程中在行之前/之后插入一行(&a);下拉列表视图
我想在单个列表视图中拖放时显示一条蓝线。这将有助于识别拖动的行是在实际行之前还是之后删除。(其他需要的事件,如.DragEnter、.DragDrop正在按预期工作) 如果没有第三方listview控件,如何在VB.NET中绘制这样一条蓝线?Vb.net 在拖动过程中在行之前/之后插入一行(&a);下拉列表视图,vb.net,listview,drag-and-drop,Vb.net,Listview,Drag And Drop,我想在单个列表视图中拖放时显示一条蓝线。这将有助于识别拖动的行是在实际行之前还是之后删除。(其他需要的事件,如.DragEnter、.DragDrop正在按预期工作) 如果没有第三方listview控件,如何在VB.NET中绘制这样一条蓝线? 到目前为止,我发现了事件“GiveFeedback”: 子listView1\u GiveFeedback(ByVal sender作为对象,ByVal e作为GiveFeedbackEventArgs)处理listView1.GiveFeedback e
到目前为止,我发现了事件“GiveFeedback”:
子listView1\u GiveFeedback(ByVal sender作为对象,ByVal e作为GiveFeedbackEventArgs)处理listView1.GiveFeedback
e、 UseDefaultCursors=False
Windows.Forms.Cursor.Current=游标.Cross
“这将有望为您指明正确的方向:
首先,设置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(或至少listviewitem的区域)无效
我建议您考虑创建一个从listview继承的新类,因为这将使管理代码更加容易
希望这有助于为您指明正确的方向……谢谢,为我指明了正确的方向!我在.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 with
ListView1.InsertionMark.Color = Color.Blue
我需要这样的东西,但有点不同。我想要达到的是画一条线,但根据索引的位置,这条线应该在特定项目之前或之后画
示例:我得到了一个包含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
希望这有帮助,因为我一直在寻找类似的东西。你应该展示你已经尝试过的东西。您显示的代码几乎不代表所需的任何内容(另外,不确定使用此事件的意义)。您需要跟踪元素被删除的确切位置,并在ListView中找到特定项的格式更改。
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
Me.lvDisplayed.InsertionMark.Color = Color.DarkRed