FREE PowerBuilder Training

Advanced PowerBuilder

HomePrevious Lesson: Communication Between Objects
Next Lesson: Summary

Displaying Popup Menu on the DataWindow

You have learned about variable scoping in the previous section. Remember, we painted "m_popup_menu" menu in the "Menu Painter" session's exercises. We can use that menu as the popup menu for the DataWindow "dw_product". Whenever the user clicks with the right mouse button on the DataWindow, invoke the popup menu at the place where the mouse pointer was.

To achieve this functionality, declare an instance variable at the "w_product_master" window. Open "w_product_master" and select 'Declare/Instant Variables' from the menu. Type the following declaration:

m_popup_menu i_popup_menu

The above statement declares an instant variable "i_popup_menu" of type "m_popup_menu". This declaration is similar to declaring any traditional datatype variables. For ex: Int I declares a variable "I", of Integer datatype. Close the dialog box by clicking on the OK CommandButton.

Now, if you try to use the variable "i_popup_menu", you will get "Null Point Reference" error. That is because, we haven't created any instance of the popup menu. Using an object variable is little different from using a traditional datatype variable. When we declare an integer variable, we can start using that variable immediately, i.e., you can assign a value to the variable. In case of object variables, you have one more step to complete before you actually start using the variable. That is, creating that variable by using the CREATE statement. Append the following statement to the "Open" event for the "w_product_master" window.

// Object: w_product_master window
// Event: Open

i_popup_menu = CREATE m_popup_menu

CREATE statement creates an instance of a specified object and assigns it to the variable specified on the left hand side. With this step, you have declared and created a menu instance variable. Now, let's display the popup menu. Type the following command:

// Object: dw_product in w_product_master window
// Event: rButtonDown

i_popup_menu.PopMenu( PointerX( Parent ), &
             PointerY( Parent ) )

The above statement displays "i_popup_menu" by calling PopMenu() function. PopMenu() function takes two parameters: X and Y co-ordinates of the screen where the popup menu is to be displayed. PointerX() and PointerY() functions return the mouse's X and Y co-ordinates for the specified object.

Now, run the application, retrieve the data and click with the right mouse button on "dw_product" DataWindow. You will see the popup menu. You may find it different compared to the popup menu you see in the PowerBuilder's development environment. The difference being the display of "DataWindow" menu bar. We don't want to display it in the popup menu. Right? Yes, we can do so with a simple change. Change the above code as follows:

// Object: dw_product in w_product_master window
// Event: rButtonDown

i_popup_menu.m_datawindow.PopMenu( PointerX( Parent ), &
             PointerY( Parent ) )

In the first example, we are calling the PopMenu() function at the menu level, so, PowerBuilder displays the whole menu. To display menu items under a specific menu bar item, you need to invoke the PopMenu() at that level. In the second example, we are calling the PopMenu() function at "m_datawindow", which is the name for "DataWindow" menu bar item. Now, run the application and see how it works.

Now, we need to write the code for the menu options. We already have code for sorting, filtering, etc… So, why not use that code.

ParentWindow.TriggerEvent( "ue_sort" )

Write the above code to the 'Sort' menu option in the "m_popup_menu" menu and as for the rest, find what to write from the table below.

Menu Option

Replace "ue_sort" with the following:

Filter

ue_filter

Save

ue_save

Save As

ue_export

Print

ue_print

Print Preview

ue_print_preview

Print in Black & White Records

No code now. Let's write in the next session.

We don't need to write code for "Cancel this Popup Menu" menu option, because, when user select that option, we do nothing. Remember the popup menu's behavior, once the menu option is selected, the menu disappears and executes the script for the appropriate event. If there is no code, as expected, the menu will disappear and does nothing.

Do you see the difference between SDI ( Single Document Interface ) and  MDI (Multiple Document Interface)? A great interface, isn't it?

We forgot one thing. We didn't write script to exit from the application. Write the following code for the 'Exit' menu option in "m_sheet_menu" and "m_mdi_menu" menus.

Halt Close
HomePrevious Lesson: Communication Between Objects
Next Lesson: Summary

 
Copyright © 1996 - 2013 Prasad Bodepudi. All Rights Reserved.