
Coding Example
This example is intended to accompany the section
Using the SAP Toolbar. It contains the necessary ABAP coding extracts for creating an instance of the toolbar. However, it contains no other processing logic, and is therefore not a working program.The example produces the following toolbar:

The Expand and Collapse buttons each have a statically-assigned dropdown menu (implemented using a context menu). The Details button is a normal pushbutton, and Active/Inactive is a toggle button.
1. Data Declarations
* Object references
* Data structures for passing data to toolbar
* Data structures for events
2. Event Handler Class
CLASS lcl_toolbar_events DEFINITION.
* Here, you define instance methods or
* static methods to handle the events
* of the toolbar.
CLASS-METHODS: on_function_selected
FOR EVENT function_selected OF cl_gui_toolbar
IMPORTING fcode = fcode.
ENDCLASS.
CLASS lcl_toolbar_events IMPLEMENTATION.
METHOD on_function_selected.
* Implementation of the method. It has the parameter
* FCODE that you can use to work out which function was
* chosen
ENDMETHOD.
ENDCLASS.
3. Creating and Filling the Toolbar
CREATE OBJECT toolbar EXPORTING parent = parent.
* Information for first button
* Repeat for other buttons
* Add buttons to toolbar
4. Creating and Filling the Dropdown Menus
CREATE OBJECT ct_expand.
APPEND wa_ctxmenu TO table_ctxmenu
* Repeat for other context menu
* Add dropdown context menus to toolbar
CALL METHOD toolbar->assign_static_ctxmenu_table
EXPORTING table_ctxmenu = table_ctxmenu.
5. Registering the Events
event-eventid = cl_gui_toolbar=>m_id_function_selected.
event-appl_event = ' '.
append event to events.
CALL METHOD toolbar->set_registered_events
EXPORTING events = events.
SET HANDLER lcl_toolbar_handler=>on_function_selected for toolbar.