473,320 Members | 1,856 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Dragging text from a textbox

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
0 1665

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Sparky | last post by:
Does anyone know if dragging the URL from Internet Explorer and dropping it into a text box in my own program is possible? erm ..and if so, how? Failing that, is there some other method I can use...
0
by: Fred Heida | last post by:
Hi, I try to implement dragging (and dropping) of text inside a rtf control (similar as word i guess) I have set AllowdDrop = true and setup the handlers for DragDrop, DragOver, DragEnter. is...
0
by: James Slade | last post by:
Hi there, I have a main form with a textbox on it. I have a secondary form that, when a button is clicked on the main form, pops up with a treeview. I want to be able to drag nodes onto the...
2
by: Christian Blackburn | last post by:
Hi Gang, I would like to be able to drag and drop documents onto my application and have them open immediately thereafter. Can somebody point me towards a KB article or example? I could find a...
2
by: Abubakar | last post by:
Hi, In a normal Windows.Forms.TextBox control, I want to be able to select a text (ie highlight, this is by default possible) and than be able to drag that text through my mouse pointer to another...
0
by: andytsummers | last post by:
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...
11
by: jon | last post by:
I'm trying to have a section where people can just start dragging files into a textbox and the full location of the file will be stored. MS-Dos has this function built into it, and it's written in...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.