By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,687 Members | 2,228 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,687 IT Pros & Developers. It's quick & easy.

Dragging text from a textbox

P: n/a
Hi

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)
Else
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
dropped.
'Delete the (selected) text if it was a move.
If effect = DragDropEffects.Move Then
If ctlCurrent.SelectionLength = 0 Then
ctlCurrent.Text = ""
Else
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
Else
'Display the move cursor
e.Effect = e.AllowedEffect And DragDropEffects.Move
End If
Else
'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
Sub

'Determine if it is a copy or move operation
If CBool(e.KeyState And 8) Then
' Display the copy cursor.
e.Effect = DragDropEffects.Copy
Else
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 =
e.Data.GetData(DataFormats.Text).ToString
End Sub

May 21 '07 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.