Implement drag and drop in VB.NET

zhaozj2021-02-16  61

The first time I sent an article in 9CBS. I hope everyone will help the top, encourage new hands, thank you http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dv_vstechart/html/vbtchimpdragdrop .asp implements drag and drop in VB.NET Implementing Drag and Drop in Visual Basic .NET Steve Hoag Visual Basic® .NET TEAM Microsoft Corporation September 2003 Summary: This article explains how to implement drag and drop in VB.NET Windows users are generally divided into two categories: a type of habitual keyboard operation, a type of habitual mouse operation. Programmers generally provide shortcuts (in commands or letters) or shortcuts (combinations of CTRL plus letters), but those mouse users have been ignored. Because the programmers are generally used to use the keyboard, they can understand the characteristics of the keyboard operation, but each programmer should also consider providing mouse support. One thing that mouse users expect to be able to achieve dragging in the application. If you pay attention to some large applications or Windows itself, the drag and drop operation can be seen everywhere. For example, users may have been accustomed to dragging and dropping files in Windows Explorer, or dragging and dropping text in Word. Although the drag and drop operation can be seen everywhere, only a very few programmers implement drag and drop function in the programs they have written, the most likely reason is that they think that it is difficult to achieve drag and drop may be more difficult than the imagination. This article lists how to move text, images, or files between forms, forms, or even applications, showing that it is very easy to implement drag and drop in VB.NET. Drag and drop how to drag and drop, actually use the mouse to copy the split, so you have to have a source that can be copied or moved, but also has a destination that can paste. During these two operations, the data is saved in memory. Copying the paste is cutting version, while dragging and drop is an object of DataObject, which is essentially private shear board. Below is a typical time series: 1. Dragging is initialized by calling the DodragDrop method of the source control, DodragDrop has two parameter data, specifying the data that will be transmitted, and specify the allowed operation (copy) Or move) This automatically creates a new DataObject object 2, and then inspire the GiveFeedback event in turn. In most cases, you don't need to worry about the GiveFeedback event, however you want to customize the mouse pointer during drag and drop, you can add your code in these places. 3, any control with the allowdrop attribute and is set to true is an implied DROP object. The AllowDrop property can be set in the properties window when design, or you can load it automatically in the Form_Load event. 4. When the mouse is moved to a control, the DrageNter event of this control is simultaneously stimulated. The GetDataPresent method is used to confirm whether the dragged data is suitable for the target control, and the Effect property is used to display the appropriate mouse pointer. 5. If the user releases the mouse on a valid target control, the DragDrop event is also excited. The code in the DragDrop event handle releases the data from the DataObject object and display it in the target control. What changes from VB6 to VB.NET? (Omitted) A simple amount of drag and drop text drag and drop operation is a very useful situation is from a TextBox control to copy text to another TextBox control. Of course, you can implement (Ctrl C and Ctrl V) using only the keyboard, but drag and drop easier because it only requires the movement of the mouse to complete.

1. Add two text boxes to a form and set the allowdrop properties of the second TextBox control to True to add the following code. Private MouseIsDown As Boolean = False Private Sub TextBox1_MouseDown (ByVal sender As Object, ByVal e As _ System.Windows.Forms.MouseEventArgs) Handles TextBox1.MouseDown 'Set a flag to show that the mouse is down. MouseIsDown = True End Sub Private Sub TextBox1_MouseMove (ByVal sender As Object, ByVal e As _ System.Windows.Forms.MouseEventArgs) Handles TextBox1.MouseMove If MouseIsDown Then 'Initiate dragging. TextBox1.DoDragDrop (TextBox1.Text, DragDropEffects.Copy) End If MouseIsDown = False End Sub Private Sub TextBox2_DragEnter (ByVal sender As Object, ByVal e As _ System.Windows.Forms.DragEventArgs) Handles TextBox2.DragEnter 'Check the format of the data being dropped. If (e.Data.GetDataPresent (DataFormats.Text)) Then' Display the copy cursor. e.Effect = DragDropEffects.Copy Else 'Display the no-drop cursor. e.Effect = DragDropEffects.None End If End Sub Private Sub TextBox2_DragDrop (ByVal sender As Object, ByVal e As _ System.Windows.Forms. Drageventargs) Handles TextBox2.drag Drop 'Paste the text = E.Data.getdata (DataFormats. End Sub In the above example, the mousedown event is used to determine whether the mouse is pressed, and the dodragDrop method is used in the MouseMove event. Although you can initialize DRAG in the mousedown event, however, this will bring out unexpected results. When the user clicks the control, the NO-DRAG pointer will be displayed. The dodragdrop method has two parameter data, which represents the first TextBox Text property. AllowedEffects, this example is only copied. In the mousemove event, the Mouseisdown flag is set to false, although there is no need in this example, but if you have a lot of controls support drag and drop, you will get a runtime exception. In the DrageNter event, the getDataPresent method checks the data format being dragged, in this case, so the Effect property is set to copy, and the COPY pointer is also displayed. In the DragDrop event, the getData method is used to get text from DataObject and give it to the target text box.

Drag images Although drag and drop images are not used as drag and drop text, it is still very useful in many applications. In fact, there is no difference between the two, but it is only a change in the data type. 1. Add two PictureBox controls in Form.

2, add the following code in the code form Private Sub Form1_Load (ByVal sender As System.Object, ByVal e As _ System.EventArgs) Handles MyBase.Load 'Enable dropping. PictureBox2.AllowDrop = True End Sub Private Sub PictureBox1_MouseDown (ByVal sender As Object, ByVal e As _ System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown If Not PictureBox1.Image Is Nothing Then 'Set a flag to show that the mouse is down. m_MouseIsDown = True End If End Sub Private Sub PictureBox1_MouseMove ( ByVal sender As Object, ByVal e As _ System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove If m_MouseIsDown Then 'Initiate dragging and allow either copy or move. PictureBox1.DoDragDrop (PictureBox1.Image, DragDropEffects.Copy or _ DragDropEffects.Move ) End If m_MouseIsDown = False End Sub Private Sub PictureBox2_DragEnter (ByVal sender As Object, ByVal e As _ System.Windows.Forms.DragEventArgs) Handles PictureBox2.DragEnter If e.Data.GetDataPresent (DataFormats.Bitmap) Then 'Check for the CTRL Key. if E.KEYSTAT e = 9 Then e.Effect = DragDropEffects.Copy Else e.Effect = DragDropEffects.Move End If Else e.Effect = DragDropEffects.None End If End Sub Private Sub PictureBox2_DragDrop (ByVal sender As Object, ByVal e As _ System.Windows. Forms.DragEventArgs) Handles PictureBox2.DragDrop 'Assign the image to the PictureBox. PictureBox2.Image = e.Data.GetData (DataFormats.Bitmap)' If the CTRL key is not pressed, delete the source picture. If Not e.KeyState = 8 THEN PICTUREBOX1.IMAGE = NOTHING End If End Sub Notes notice that the second Picturebox control in the example above is set in the Form1_Load event, because PictureBox does not have the abturebox attribute when designing.

In the mousedown event, the code first detects whether there is a picture to be assigned to the PictureBox; if not, when you move the picture, the next click will lead an accident. It should also be noted that the code detects whether the CTRL button is pressed in the DRAGENTER and DRAGDROP events, thereby deciding whether to be replicated or moving pictures. Why is the value difference? In the DrageNter event, when the left mouse button is pressed, the resulting value is 1, and the value of Ctrl is added to the value of 9. See KeyState enumerated list DragEventArgs.KeyState Property (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemwindowsformsdrageventargsclasskeystatetopic.asp). So far, these two examples have handled drag and drop between different controls of the same form, but also applicable on different forms of the same application. Drag files in Windows are typically replicated or moving files, and Windows fully supports this feature, and this is also a preferred way to operate files for many users. In addition, the user has become accustomed to dragging the file to a program to open the file, which is turned on by dragging a DOC file to Word. In this example, the ListBox control is operated with a file dragged from the Windows Explorer. Was added to form a ListBox control, and set its AllowDrop property is True, and add the following code: Private Sub ListBox1_DragEnter (ByVal sender As Object, ByVal e As _ System.Windows.Forms.DragEventArgs) Handles ListBox1.DragEnter If e. Data.GetDataPresent (DataFormats.FileDrop) Then e.Effect = DragDropEffects.All End If End Sub Private Sub ListBox1_DragDrop (ByVal sender As Object, ByVal e As _ System.Windows.Forms.DragEventArgs) Handles ListBox1.DragDrop If e.Data. GetDataPresent (DataFormats.FileDrop) Then Dim MyFiles () As String Dim i As Integer 'Assign the files to an array. MyFiles = e.Data.GetData (DataFormats.FileDrop)' Loop through the array and add the files to the list. For i = 0 to myfiles.length - 1 ListBox1.items.add (MyFiles (i)) Next End End End Sub You may have noticed the Effect attribute in the DrageNter event being set to Dragdropeffects.All. Because the file itself is not true, it is copied or moved, so the source control is set to which AllowedEffects is not a relationship, so specify all the file to any fileDrop. In the above example, the FileDrop format contains the full path to each drag file. The following example tells a special case of dragging and drop: dragging and dropping in two lists. Another situation in which the show is dragged and dropd to drag and drop is from a list of mobile items to another list. In this case, drag and drop will become simpler.

Add two ListView controls to the form and set their allowdrop, multiselect, and view attributes, respectively, True, List.

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 i = 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 PR ivate 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 the listview2.items.remove (ListView2.SelectedItems.Item (0)) Else ListView1.Items.remove (ListView1.SelectedItems.Item (0)) end if i = i 1 Next End Sub You may I don't understand why this example is used by a ListView control instead of a listbox control.

转载请注明原文地址:https://www.9cbs.com/read-24218.html

New Post(0)