tips
[Top] [All Lists]

REALbasic Tip: Disabling drag and drop in an editfield

To: REALbasic-tips at lists dot realsoftware dot com
Subject: REALbasic Tip: Disabling drag and drop in an editfield
From: Geoff Perlman <geoff at realsoftware dot com>
Date: Tue, 11 May 2004 14:10:09 -0500
Cc:
Delivered-to: REALbasic-tips at lists dot realsoftware dot com
List-help: <mailto:realbasic-tips-request@lists.realsoftware.com?subject=help>
List-id: REALbasic Tips <realbasic-tips.lists.realsoftware.com>
List-post: <mailto:realbasic-tips@lists.realsoftware.com>
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.

<Prev in Thread] Current Thread [Next in Thread>
  • REALbasic Tip: Disabling drag and drop in an editfield, Geoff Perlman <=