472,985 Members | 2,633 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,985 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 1638

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: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

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.