Get the App
SLTechnology News&Howtos  ›  Development  › 

How to realize drag and drop between tables by VB.NET

Shulou Source: shulou.com Published: 2022-06-02 09:02:00 09月23日 Update

This article mainly shows you "VB.NET how to achieve inter-table drag and drop", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "VB.NET how to achieve inter-table drag and drop" this article.

VB.NET inter-table drag and drop

One case of drag and drop between VB.NET tables is to move items from one list to another. In this case, drag and drop will become easier. Add two ListView controls to the form and set their AllowDrop, Multiselect, and View properties to True, True, and List, respectively. And add the following code:

Private Sub ListView_ItemDrag (ByVal sender As Object, ByVal e As _ System.Windows.Forms.ItemDragEventArgs) Handles ListView1.ItemDrag, _ ListView2.ItemDrag Dim myItem As ListViewItem Dim myItems (sender.SelectedItems.Count-1) As ListViewItem Dim i As Integer = 0 'Loop though the SelectedItems collection for the source. For Each myItem In sender.SelectedItems' Add the ListViewItem to the array of ListViewItems. MyItems (I) = myItem ii = I + 1 Next 'Create a DataObject containg the array of ListViewItems. Sender.DoDragDrop (New _ DataObject (System.Windows.Forms.ListViewItem (), myItems), _ DragDropEffects.Move) End Sub Private Sub ListView_DragEnter (ByVal sender As Object, ByVal e As _ System.Windows.Forms.DragEventArgs) Handles ListView1.DragEnter, _ ListView2.DragEnter 'Check for the custom DataFormat ListViewItem array. If e.Data.GetDataPresent (System.Windows.Forms.ListViewItem ()) Then e.Effect = DragDropEffects.Move Else e.Effect = DragDropEffects.None End If End Sub Private Sub ListView_DragDrop (ByVal sender As Object, ByVal e As _ System.Windows.Forms.DragEventArgs) Handles ListView1.DragDrop, _ ListView2.DragDrop Dim myItem As ListViewItem Dim myItems () As ListViewItem = _ e.Data.GetData (System.Windows.Forms.ListViewItem ()) Dim i As Integer = 0 For Each myItem In myItems' Add the item to the target list. Sender.Items.Add (myItems (I). Text) 'Remove the item from the source list. If sender Is ListView1 Then ListView2.Items.Remove (ListView2.SelectedItems.Item (0) Else ListView1.Items.Remove (ListView1.SelectedItems.Item (0)) End If ii = I + 1 Next End Sub

You may not understand why the ListView control is used instead of the ListBox control in this example, which is a good question, because the ListBox control does not support multiple drag and drop. The ListView and TreeView controls have an ItemDrag event. In the above example, an ItemDrag event handle overrides two controls and is listed in the Handles clause. The Sender parameter indicates which control is initializing Drag. In the DoDragDrop method, a new DataObject is created and manipulated with an array.

The above is all the contents of the article "how to drag and drop between tables in VB.NET". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Tags: Controls types content articles two events examples situations arrays methods systems learning help usage clauses code parameters handles properties members Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information Shulou Tech Info macOS OPPO Reno Docker