Being able to drag and drop text to and from an editfield can be handy.
But there may be times when you don't want the user to be able to do
this. Perhaps you're building a testing application and you don't want
the user to take the answers with them or bring the answers with them
to the test. Disabling cut, copy and paste is not difficult but what
about disabling drag and drop? Well, here's how you can do it:
To disable dragging, put the following code in the mousedown event of
your editfield:
Dim pos As Integer
pos = me.CharPosAtXY(X, Y)
if pos >= me.selStart and pos < me.selStart + me.selLength then
me.selStart = pos
me.selLength = 0
return true
end if
The code above checks to see if the mousedown is occurring within the
selected text. If it is, then it sets the cursor position to the
location where the mouse was pressed and deselects any selected text
thereby preventing the drag.
Preventing a drop is a little more of a hack. Simply place a canvas
control on top of the editfield and put the following code in the open
event of the canvas control:
me.AcceptTextDrop
When the user tries to drop text on the editfield, the canvas will get
the text instead.
Thanks to Joe Strout and Mike Bailey for their help with this tip.
--
Geoff Perlman
President and CEO
REAL Software, Inc.
|