471,338 Members | 1,044 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,338 software developers and data experts.

Dragging text from a textbox


I have tried to implement drag and drop by using the following code
but I have several problems.
1. When you put your mouse over selected text the cursor is still the
i-beam and therefore does not show that the text can be dragged
2. When pressing the mouse button to start a drag the selected text
changes - I know this is cause the code does not initiate the
dodragdrop until the mouse leaves the control but if I change this
then you cannot select text at all.
3. I really want the textbox to behave like a HTML textbox in IE where
you can drag and drop to the same control - I think if 1 and 2 can be
resolved then this should be possible.

Can anyone help please - seems a basic thing but seems to have got
very complicated.

Thanks Andy.

Private Sub NESN_textbox_MouseMove(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseMove
'Nothing to do if no mouse button is pressed.
If e.Button = 0 Then Exit Sub

'Get a reference to the current textbox control
Dim ctlCurrent As TextBox = DirectCast(sender, TextBox)

'Nothing to do if the textbox is empty!
If ctlCurrent.TextLength = 0 Then Exit Sub

'Nothing to do if we are still inside the control's borders.
If e.X >= 0 AndAlso e.X < ctlCurrent.Width AndAlso _
e.Y >= 0 AndAlso e.Y < ctlCurrent.Height Then Exit Sub

'If we get here, it means that the mouse is
' being dragged outside the control.

'Store the text to be drag into a DataObject object
Dim objDO As New DataObject
If ctlCurrent.SelectionLength = 0 Then
'Exit Sub
objDO.SetData(DataFormats.Text, ctlCurrent.Text)
objDO.SetData(DataFormats.Text, ctlCurrent.SelectedText)
End If

'Starts the drag operation
Dim effect As DragDropEffects = _
DragDropEffects.Copy Or DragDropEffects.Move
effect = ctlCurrent.DoDragDrop(objDO, effect)

'This portion of code will run after the code has been
'Delete the (selected) text if it was a move.
If effect = DragDropEffects.Move Then
If ctlCurrent.SelectionLength = 0 Then
ctlCurrent.Text = ""
ctlCurrent.SelectedText = ""
End If
End If
End Sub

Private Sub NESN_textbox_DragOver(ByVal sender As Object, ByVal e
As System.Windows.Forms.DragEventArgs) Handles MyBase.DragOver
'Check the format of the data being dropped.
If e.Data.GetDataPresent(DataFormats.Text, True) Then
'Checks if the CTRL key is held
If CBool(e.KeyState And 8) Then
'Display the copy cursor.
e.Effect = e.AllowedEffect And DragDropEffects.Copy
'Display the move cursor
e.Effect = e.AllowedEffect And DragDropEffects.Move
End If
'Display the no-drop cursor.
e.Effect = DragDropEffects.None
End If
End Sub

Private Sub NESN_textbox_DragDrop(ByVal sender As Object, ByVal e
As System.Windows.Forms.DragEventArgs) Handles MyBase.DragDrop
'Check if data is present
If Not e.Data.GetDataPresent(DataFormats.Text, True) Then Exit

'Determine if it is a copy or move operation
If CBool(e.KeyState And 8) Then
' Display the copy cursor.
e.Effect = DragDropEffects.Copy
e.Effect = DragDropEffects.Move
End If

'Get a reference to the current textbox control
Dim ctlCurrent As TextBox = DirectCast(sender, TextBox)
'Paste the text to drop
ctlCurrent.SelectedText =
End Sub

May 21 '07 #1
0 1528

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by James Slade | last post: by
2 posts views Thread by Abubakar | last post: by
reply views Thread by andytsummers | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.