image ResEx Logo
ResExcellence www : Powered by Google
Cell Phone Themes Icons Mighty Mouse Cursors Software Reviews Widgets & Widgets

Articles
   3D
   Audio
   Custom Controls
   General RB
   Graphics
   Hacks
   Mac OS X
   Menus
   Novelty
   Printing
   REALbasic 2005
   REALbasic 2006
   Registration
   Resources
   Reviews
   Serial
   Speech
   Sockets
   XML
   Video
Resource Links
News
   Current News
   February 2006
   January 2006
   December 2005
   November 2005
   October 2005
   September 2005
   August 2005
   July 2005
   June 2005
   May 2005
   April 2005
   March 2005









REALbasic for Dummies
by Erick Tejkowski


Learning REALbasic through Applications
by Clayton E., Crooks II


REALbasic for Macintosh
by Michael Swaine


REALbasic Cross-Platform Application Development
by Mark S. Choate





Older files are in Stuffit 5 or greater format. Newer files are ".Zip". Download StuffIt Expander
Tell us about a bad link. Thank You!

Listbox Menu by Seth Willits
11-22-03

Printer Version



In this tutorial, we're going to create a listbox which displays a contextual menu when an item is control or right-clicked on since this behavior is often used in applications and if you're not using it, you probably could be! If you've never used a contextual menu before, you'll be amazed how simple it is to implement them.

Begin by creating a new project with a listbox in the window and a contextual menu. The listbox should contain some default items like the example below does.

Contextual menus are used through 3 simple steps. The first, is when a mouse click occurs, check if the click was a contextual menu click (CMM click) using the global IsCMMClick() function. If IsCMMClick() returns true then either the click was a control-click or a right mouse button click and you'll need to display the menu by calling the contextual menu's Open() method. The Open() method synchronously displays the menu waiting for the user's choice until the next line of code executes.

The last step is handling the user's choice which is done via the Action event which is fired when the user actually clicks on an item in the menu. If the user clicks outside of the bounds of the menu and in consequence does not select any menu item, then the Action event is not fired. The item As String paramter to the event is the text of the menu item's title which was chosen. Using a Select statement as below is usually the easiest way to handle the selection.


Sub Action(item As String)
   Select case item
   case "MsgBox"
      MsgBox ListBox1.Cell(Listbox1.ListIndex, 0)
   case "Title"
      self.Title = ListBox1.Cell(Listbox1.ListIndex, 0)
   case "Remove"
      ListBox1.RemoveRow Listbox1.ListIndex
   end Select
End Sub

The code above will determine which menu item was chosen (MsgBox, Title, or Remove) and then take the appropriate action. Menu items are created just like popup menus:


Sub Open()
   me.AddRow "MsgBox"
   me.AddRow "Title"
   me.AddSeparator
   me.AddRow "Remove"
End Sub

To handle the click, we'll use the CellClick event of the listbox since it's the only event which lets know when a row is clicked on. We use the IsCMMClick() method as I said but notice that I also set the ListIndex of the list. The reason for this is that REALbasic doesn't update the ListIndex and redraw the row to show it is selected until after this event and all others (like the Change event) have been fired. To work around this, we force the change and an update.


Function CellClick(row as Integer, column as Integer,
                   x as Integer, y as Integer) As Boolean
   if IsCMMClick then
      me.ListIndex = row
      me.Refresh
      ContextualMenu1.Open
   end if
End Function

That's it. Pretty simple eh?




Cell Phone Themes Icons Mighty Mouse Cursors Software Reviews Widgets & Widgets

Maintained by the Staff of ResExcellence. This entire site ©1997-2006 ResExcellence
Privacy Statement? Sure we gotta Privacy Statement. [an error occurred while processing this directive]