473,471 Members | 1,744 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 983

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...
1
by: bai | last post by:
Hi, I have some textboxes, combo box, images and table with data in an html. In the table I can sort the rows by dragging them. When I drag a row over textbox/image/label, the dragged object data is...
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: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.