Monday, June 30, 2008

Visual FoxPro 6 Right-click Shortcut

Right-click shortcut menu in a TextBox control is a norm in many programming languages. But, if you happen to be working in Visual FoxPro 6, it is not that easy.

Here is the way to make the shortcut I found somewhere (which I've lost the reference), that provides the Cut, Copy and Paste functions. The functions will be enabled or disabled depends on is there any characters selected:

Create a shortcut.prg as the following:
LPARAMETERS In_SelLength,In_Row,In_Col

DEFINE POPUP shortcut SHORTCUT RELATIVE FROM In_Row,In_Col TO In_Row+3.5,In_Col+20

*DEFINE BAR _med_undo OF shortcut PROMPT "\<Undo" ;
* KEY CTRL+Z, "Ctrl+Z" ;
* MESSAGE "Undoes the last command or action"

*DEFINE BAR 2 OF shortcut PROMPT "\-"

If In_SelLength > 0
DEFINE BAR _med_cut OF shortcut PROMPT "Cu\<t" ;
KEY CTRL+X, "Ctrl+X" ;
MESSAGE "Removes the selection and places it onto the Clipboard"

DEFINE BAR _med_copy OF shortcut PROMPT "\<Copy" ;
KEY CTRL+C, "Ctrl+C" ;
MESSAGE "Copies the selection onto the Clipboard"
ELSE
DEFINE BAR _med_cut OF shortcut PROMPT "\Cut" ;
KEY CTRL+X, "Ctrl+X" ;
MESSAGE "Removes the selection and places it onto the Clipboard"

DEFINE BAR _med_copy OF shortcut PROMPT "\Copy" ;
KEY CTRL+C, "Ctrl+C" ;
MESSAGE "Copies the selection onto the Clipboard"
ENDIF

IF LEN(_CLIPTEXT) > 0
DEFINE BAR _med_paste OF shortcut PROMPT "\<Paste" ;
KEY CTRL+V, "Ctrl+V" ;
MESSAGE "Pastes the contents of the Clipboard"
ELSE
DEFINE BAR _med_paste OF shortcut PROMPT "\Paste" ;
KEY CTRL+V, "Ctrl+V" ;
MESSAGE "Pastes the contents of the Clipboard"
ENDIF

ACTIVATE POPUP shortcut

Then in your form's TextBox KeyPress event, put this in:
LPARAMETERS nKeyCode, nShiftAltCtrl

IF nKeyCode = 93
THISFORM.SCALEMODE = 0
DO shortcut WITH THIS.SELLENGTH,THIS.HEIGHT / 2 + THIS.TOP,THIS.WIDTH / 2 + THIS.LEFT
THISFORM.SCALEMODE = 3
THIS.VALUE = STR(THISFORM.SCALEMODE)
ENDIF

In the same TextBox RightClick event, put this in:
DO shortcut WITH THIS.SELLENGTH,MROW(),MCOL()
Now when you run your form, a right-click menu will pop-up:

If you find this post helpful, would you buy me a coffee?


1 comment: