Dragging and dropping controls in RealBasic

Someone recently asked me to figure out how to make an application to drag & drop controls on a canvas in RealBasic (RealBasic 2005 to be exact). I thought that about a billion people would have done this by now and would have put their stuff on the web. This turned out not to be true.

The way to do it is actually trivial in any event-driven language, but in RealBasic it seems even easier 🙂

I will show the dragging and dropping of a pushbutton; you can figure out the rest for yourself. Do not pay attention to the quality of the code; I was only supposed to figure out how to do it, not win any prices!

Make a Window and put a PushButton on it. Now add some properties:

  • obj as Object
  • x as Integer
  • y as Integer
  • move as Boolean

Put in the MouseDown of the PushButton:

obj = me
return true

and in the MouseUp of the PushButton:

obj = nil
return true

Now put in the MouseMove of the Window:

dim ctl as RectControl

if obj<>nil then
ctl = RectControl(obj)
end if

if (not move and obj <> nil) then
dx = x – ctl.Left
dy = y – ctl.Top
move = true
MouseCursor = System.Cursors.ArrowAllDirections
end if

if (move and obj = nil) then
move = false
MouseCursor = System.Cursors.StandardPointer
end if

if (move and x >0 and y>0 and x < me.width and y < me.Height) then
ctl.Left = x – dx
ctl.Top = y-dy
me.refresh
end if

And that’s it 🙂

Be the first to leave a comment. Don’t be shy.

Join the Discussion

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>