Right-Button Select and Menu Choice

Local menus are used to make it easy to use the mouse to display and choose from a menu. Popup menus are usually displayed in response to a right-button click. This is a by product of declaring the popup menu for the listbox in the Object Inspector. While the popup is displayed, the right-button click does not make a selection so you must first press the left button to select then press the right button to display the menu. You will add the right-button select to the standard popup menu display.

Creating The Popup Menu

The first step in creating a popup menu is to select the popup component from the component pallet, and drop it on the listbox. Next edit the menu using the menu editor, and select your menu in the Object Inspector for your listbox (see diagram at the right). Once you have completed your menu, select the Events tab in the Object Inspector and double click on the Mouse Down handler to create a new function. You can use the function ItemAtPos to find out which item was selected when the right button is clicked. Finally add the code for the Mouse Down handler (see MouseDown given below).

void __fastcall TSDIAppForm::ListBox1MouseDown(TObject *Sender,
      TMouseButton Button, TShiftState Shift, int X, int Y)
{
       if(Button==mbRight)   // Check to see if right button pressed
       {
       
                tagPOINT t;
                t.x = X;
                t.y = Y;
                ListBox1->ItemIndex=ListBox1->ItemAtPos(t,true);
		//Sets the index of item.
		// If nothing selected deselects
       }
}   
//---------------------------------------------------------------------------

Complete Example

To get a complete example of the right-button listbox click here .