Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can use the <a href="http://msdn.microsoft.com/en-us/library/system.windows.forms.control.dodragdrop.aspx" rel="nofollow"><code>DoDragDrop</code></a> method to do a drag &amp; drop operation. It returns a <a href="http://msdn.microsoft.com/en-us/library/system.windows.forms.dragdropeffects.aspx" rel="nofollow"><code>DragDropEffects</code></a> value that specifies if the data actually has been dropped somewhere in which case you can clear the text box.</p> <p>Since a drag &amp; drop operation shouldn't start before the mouse has been moved a bit while pressing the mouse button you need to check for that in the <code>MouseDown</code> and <code>MouseMove</code> events. <a href="http://msdn.microsoft.com/en-us/library/system.windows.forms.systeminformation.dragsize.aspx" rel="nofollow"><code>SystemInformation.DragSize</code></a> tells you how far the mouse should be moved before a drag &amp; drop operation starts.</p> <p>In the <code>MouseDown</code> event check if you actually want to start a drag (i.e left button is pressed and text box actually contains text). Then create a rectangle using the mouse location and the size given by <code>SystemInformation.DragSize</code>. In the <code>MouseMove</code> event check if the mouse is dragged outside the rectangle and call <code>DoDragDrop</code>:</p> <pre><code>Private _dragStart As Boolean Private _dragBox As Rectangle Private Sub srcTextBox_MouseDown(sender As Object, e As MouseEventArgs) Handles srcTextBox.MouseDown ' a drag starts if the left mouse button is pressed and the text box actually contains any text _dragStart = e.Button = MouseButtons.Left And Not String.IsNullOrEmpty(srcTextBox.Text) If _dragStart Then Dim dragSize As Size = SystemInformation.DragSize _dragBox = New Rectangle(New Point(e.X - (dragSize.Width \ 2), e.Y - (dragSize.Height \ 2)), dragSize) End If End Sub Private Sub srcTextBox_MouseUp(sender As Object, e As MouseEventArgs) Handles srcTextBox.MouseUp _dragStart = False End Sub Private Sub srcTextBox_MouseMove(sender As Object, e As MouseEventArgs) Handles srcTextBox.MouseMove If Not _dragStart Or (e.Button And MouseButtons.Left) &lt;&gt; MouseButtons.Left Or _dragBox.Contains(e.X, e.Y) Then Return Dim data As New DataObject() data.SetData(srcTextBox.Text) ' you can optionally add more formats required by valid drag destinations: ' data.SetData(DataFormats.UnicodeText, Encoding.Unicode.GetBytes(srcTextBox.Text)) ' data.SetData("UTF-8", Encoding.UTF8.GetBytes(srcTextBox.Text)) ' data.SetData("UTF-32", Encoding.UTF32.GetBytes(srcTextBox.Text)) Dim dropEffect As DragDropEffects = srcTextBox.DoDragDrop(data, DragDropEffects.Move) If (dropEffect = DragDropEffects.Move) Then srcTextBox.Text = "" End If _dragStart = False _dragBox = Rectangle.Empty End Sub Private Sub destTextBox_DragOver(ByVal sender As Object, ByVal e As DragEventArgs) Handles destTextBox.DragOver If e.Data.GetDataPresent(GetType(String)) Then e.Effect = e.AllowedEffect And DragDropEffects.Move Else e.Effect = DragDropEffects.None End If End Sub Private Sub destTextBox_DragDrop(ByVal sender As Object, ByVal e As DragEventArgs) Handles destTextBox.DragDrop If e.Data.GetDataPresent(GetType(String)) Then destTextBox.Text = e.Data.GetData(GetType(String)).ToString() End If End Sub </code></pre> <p>Dragging the mouse in a TextBox usually starts text selection. The above code changes this behavior. Users can't use the mouse any more to select text. This is obviously not a good idea since users wouldn't expect that. To allow both text selection with the mouse and dragging you need to control the selection mechanism. This means you need to create your own text box class.</p> <p>I would suggest a different approach: Use a label that shows the value as the dragging source and/or destination. To allow editing you can create a hidden text box. If the user double clicks on the label you hide the label and show the text box. After the user finishes editing (by hitting enter or cancel) or if the text box looses focus you hide the text box and show the label again.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload