Popup menus in RealBasic

Popup (or contextual) menus appear when you right-click the mouse. They are a part of almost every Windows program. Nonetheless, it took me some effort to find out how to create popup menus in RealBasic with all the features that I needed. The examples supplied by Real Studio were either out-of-date or simplistic. The only other alternative was the user forum. I find it difficult to get useful information from forum threads, and I wish search engines didn’t include them. Many of the answers are off-the-point or even totally wrong. Spelling is iffy and grammar is almost non-existent. The layout is often confusing (i.e., each answer often shows all the previous correspondence), so it’s difficult to gauge where you are on the path to a solution. Finally, the worst case is when you have to register to see the answer. Registering gets old after the first 1500 sites.

Example of a popup menu in RealBasic

Example of a popup menu in RealBasic

This article shows how to set up a popup menu using the current routines of RealBasic. An important feature is setting the enable status of menu entries. Except in the simplest programs, entries must be disabled depending on the program context to avoid illegal operations (i.e., save a entity that hasn’t yet been created).

The example produces the popup menu shown in Fig. 1. Notice that some entries are grayed because rulers do not exist when the program opens. The code listed below is included in the MouseDown event of the associated window. Note the comment numbers added to the Append statement documenting the order of entries (separators don’t count). These indices are used in the code lines that set the enable status. It’s important to include the test

if (HitItem <> nil) then
end if

to avoid an error if the user clicks on a disabled menu entry.

if IsContextualClick then
// ===============================
// Define menu extries
// ===============================
dim base as new MenuItem
base.Append( new MenuItem( "Create ruler" ) )  '1
base.Append( new MenuItem( "Analyze graph" ) )  '2
base.Append( new MenuItem( "Analyze drawing" ) )  '3
base.Append( new MenuItem( MenuItem.TextSeparator ) )
base.Append( new MenuItem( "Flip ruler" ) )  '4
base.Append( new MenuItem( "Hide/show rulers" ) )  '5
base.Append( new MenuItem( "Close ruler" ) )  '6
base.Append( new MenuItem( "Save ruler" ) )  '7
base.Append( new MenuItem( "Load ruler" ) )  '8
base.Append( new MenuItem( MenuItem.TextSeparator ) )
base.Append( new MenuItem( "Instruction manual" ) )  '9
base.Append( new MenuItem( "About FP UniScale" ) )  '10
base.Append( new MenuItem( MenuItem.TextSeparator ) )
base.Append( new MenuItem( "Exit program" ) )  '11

// ===============================
// Set the enabled status
// ===============================
if (RulersHidden) then
base.item(4).enabled = False   'Flip ruler
base.item(5).enabled = True    'Hide/show rulers
base.item(6).enabled = False   'Close ruler
base.item(7).enabled = False   'Save ruler
base.item(8).enabled = False   'Load ruler
else
// Ruler1 closed, Ruler2 closed
if ((not RulerOpen1) and (not RulerOpen2)) then
base.item(4).enabled = False  'Flip ruler
base.item(5).enabled = False  'Hide/show rulers
base.item(6).enabled = False  'Close ruler
base.item(7).enabled = False  'Save ruler
base.item(8).enabled = True   'Load ruler
// Ruler1 open, Ruler2 closed
elseif (RulerOpen1 and (not RulerOpen2)) then
base.item(4).enabled = True   'Flip ruler
base.item(5).enabled = True   'Hide/show rulers
base.item(6).enabled = True   'Close ruler
base.item(7).enabled = True   'Save ruler
base.item(8).enabled = True   'Load ruler

// Ruler1 closed, Ruler2 open
elseif ((not RulerOpen1) and RulerOpen2) then
base.item(4).enabled = True   'Flip ruler
base.item(5).enabled = True   'Hide/show rulers
base.item(6).enabled = True   'Close ruler
base.item(7).enabled = True   'Save ruler
base.item(8).enabled = True   'Load ruler
// Ruler1 open, Ruler2 open
else
base.item(4).enabled = True   'Flip ruler
base.item(5).enabled = True   'Hide/show rulers
base.item(6).enabled = True   'Close ruler
base.item(7).enabled = True   'Save ruler
base.item(8).enabled = False  'Load ruler
end if
end if

// ===============================
// Carry out actions
// ===============================
dim hitItem as MenuItem
hitItem = base.PopUp
if (HitItem <> nil) then
select case (HitItem.Text)
case("Analyze graph")
// Action code
case("Analyze drawing")
// Action code
case("Create ruler")
// Action code
case ("Flip ruler")
// Action code
case ("Load ruler")
// Action code
case ("Save ruler")
// Action code
case ("Close ruler")
// Action code
case("Instruction manual")
// Action code
case("About FP UniScale")
// Action code
case("Exit program")
// Action code
end select
end if
return true
end if

Comments are closed.