ABAP---How to use controls

本文介绍ABAP UI控件的同步机制及事件处理流程,包括使用Flush方法进行手动同步、事件类型的区分及注册事件的方法。通过具体实例展示了如何定义事件处理器类、注册事件及设置事件处理。

How to use controls
 

How to:

General
Synchronization/Flush
Set up event handling for controls
Declare a reference variable before the class has been defined
Set focus to a control


 Control Framework and enheritance hierarchy
The control framework consists of 2 parts:

CL_GUI_CFW contains methods that provide services for communication with the frontend, and can be used both by control wrapper calsses and by control progtrammers
The CL_GUI_OBJECT encapsulates ActiveX or JavaBeans methods, while CL_GUI_CONTROL is responsible for displaying the control on the screen.
CL_GUI_OBJECT -> CL_GUI_CONTROL -> CL_GUI_* (Wrapper class)

These classes contains methods that are enherited by subsequent classes in the enheritance tree. 

RFC calls is used to synchronize the front and backend. This synchronization takes palce at some predifined points in the program flow. However you should not rely entirely on automatic synchronization, but force synchronization with the Flush method when necessary.

Bear in mind that the synchronization/RFC calls represenmts a bottleneck, so you should keep the not use the Flush method to a minimum.

Syntax:

CALL METHOD cl_gui_cfw=>FLUSH 

There are two types of events:

Application events. T

The flow logic of the screen is processed after the event (The PAI module is processed).

In the events table the event must be registred as an application event by setting then field appl_event to 'X':

wa_events-eventid = cl_gui_textedit=>event_double_click.
wa_events-appl_event = 'X'.
append wa_events to i_events.

Important: The dispatch method of cl_gui_cfw must be called in the PAI module:

CALL METHOD cl_gui_cfw=>dispatch.

System events.

For system events the flow-logic of the screen is not executed. That means that the PAI and PBO modules are not processed.

In the events table the the field appl_event must be set to SAPCE:

wa_events-eventid = cl_gui_textedit=>event_double_click.
wa_events-appl_event = space.
append wa_events to i_events.

The dispatch method of cl_gui_cfw must NOT be called.

Example

It is presumed that a SAP toolbar control named go_toolbar of class cl_gui_toolbar has been defined. To see a complete example of how to handle events, refer to The SAP toolbar control.

Data:
* 1.  Define and instance of the eventhandler class.
*      If the event handler class is defined after the data decalaration
*      the class must be declared as DEFERRED in the top of the program:  CLASS cls_event_handler DEFINITION DEFERRED.
  go_event_handler TYPE REF TO cls_event_handler,
* 2. Define table for registration of events.
*     Note that a TYPE REF   to cls_event_handler must be created before you can
*     reference types cntl_simple_events and cntl_simple_event
  gi_events TYPE cntl_simple_events,
* Workspace for table gi_events
  g_event TYPE cntl_simple_event.
* 3. Define and implement eventhandler class
CLASS cls_event_handler DEFINITION.
  PUBLIC SECTION.
    METHODS:
*     Syntax:
*     FOR EVENT
*         OF
      on_function_selected
        FOR EVENT function_selected OF cl_gui_toolbar
          IMPORTING fcode,
ENDCLASS.
CLASS cls_event_handler IMPLEMENTATION.
   METHOD on_function_selected.
*     Do something when the event is raised
  ENDMETHOD.
ENDCLASS.
* 4. Append events to the events table
*     The Event Id  can be found in the control documentation. Note that The event below is registred as an application event
 g_event-eventid         = go_toolbar->m_id_function_selected.
 g_event-appl_event   = 'X'.    "This is an application event
APPEND g_event TO gi_events.
.... append more events i necessary...
* 5. Use the events table to register events for the control
    CALL METHOD go_toolbar->set_registered_events
        EXPORTING
           events = gi_events.
* 6. Create event handler
CREATE OBJECT   go_event_handler.
* Syntax:
* SET HANDLER ->
*   FOR
SET HANDLER go_event_handler->on_function_selected
      FOR go_toolbar.
Scenario: DATA: o_my_class TYPE REF TO lcl_myclass.

CLASS lcl_myclass.....
ENDCLASS.

This will cause an error because the definition of class lcl_myclass is after the declaration.

Solution:

Define the class in the beginning of the program with DEFINITION DEFERRED:

CLASS lcl_myclass DEFINITION DEFERRED.

DATA: o_my_class TYPE REF TO lcl_myclass.

CLASS lcl_myclass.....
ENDCLASS. 

Set the focus to a control named go_grid:

CALL METHOD cl_gui_control=>set_focus
  EXPORTING control = go_grid.
 



Trackback: http://tb.blog.youkuaiyun.com/TrackBack.aspx?PostId=1659001


<think> Hmm, the user is asking about the meaning of "answer_code = E" in ABAP development. Let me analyze this carefully. First, I recall from previous interactions that the user is working with SAP ABAP dialog programming, specifically popup confirmation functions. The references provided mention POPUP_TO_CONFIRM_WITH_VALUE function modules and custom popup implementations. Ah yes, in ABAP confirmation popups, answer_code typically stores the user's response. The 'E' value is clearly explained in SAP documentation as representing the "Cancel" button action. This makes perfect sense in GUI contexts where users might abort an operation. The user seems to be debugging or implementing a confirmation dialog. They probably encountered 'E' in their code and need clarification. Since they referenced popup examples, they're likely working with similar function modules. I should explain that answer_code is returned by standard popup functions after user interaction. The key values are: - '1' for Yes - 'A' for No - 'E' for Cancel Important to note that in ABAP, 'E' stands for "Abbruch" (German for cancel) since SAP originated in Germany. The system variable SY-SUBRC would typically be set to 0 after popups regardless of user choice, so checking answer_code is crucial. The user might benefit from knowing how to handle this in code. After CALL FUNCTION, they should check the ANSWER parameter immediately. For 'E', common practices include rolling back transactions or returning to previous screen. I wonder if they're getting unexpected cancel responses? Maybe they need troubleshooting tips too. Should mention that CANCEL_DISPLAY parameter controls whether cancel button appears at all - if set to space, 'E' might never occur. Also worth noting that some legacy systems use different codes, but 'E' for cancel is standard since SAPGUI 7.0 onward. If they see different behavior, it might be custom popup implementation like in reference [2]. The explanation should cover both the meaning and practical handling, with clear ABAP code examples showing conditional logic based on answer_code. Since they referenced MESSAGE statements in their example, I'll include similar error handling. </think> 在ABAP开发中,`answer_code = 'E'` 通常表示用户在弹出确认对话框(如 `POPUP_TO_CONFIRM` 系列函数)中**选择了"取消"操作**。以下是关键说明: ### 含义解析 1. **标准返回值**: ABAP标准弹窗函数(如 `POPUP_TO_CONFIRM`)返回的 `answer_code` 代码中: - `'1'` → 用户点击"是"(Yes) - `'A'` → 用户点击"否"(No) - `'E'` → 用户点击"取消"(Cancel)或关闭窗口 2. **业务逻辑处理**: 开发者需在代码中检查返回值: ```abap IF answer_code = 'E'. MESSAGE s000(zmsg) WITH '操作已取消' DISPLAY LIKE 'E'. "提示取消 RETURN. "通常终止后续处理 ENDIF. ``` 3. **技术关联**: - 当弹窗参数 `CANCEL_DISPLAY = 'X'` 时(如引用[1]),"取消"按钮才会显示 - `SY-SUBRC` 仅反映函数调用是否成功(0=成功),**不体现用户选择** ### 典型应用场景 ```abap CALL FUNCTION 'POPUP_TO_CONFIRM' EXPORTING text_question = '确定删除订单?' cancel_display = 'X' "启用取消按钮 IMPORTING answer = answer_code. "接收用户选择 CASE answer_code. WHEN '1'. "执行删除逻辑 WHEN 'A'. "忽略操作 WHEN 'E'. "记录取消日志 ENDCASE. ``` ### 注意事项 - **弹窗类型差异**:自定义弹窗(如引用[2])可能使用不同返回值约定,需检查具体实现 - **用户中断场景**:`'E'` 通常对应系统标准工具栏的"取消"图标(🚫)或按 `ESC` 键 - **错误处理**:若未显式处理 `'E'`,可能导致用户意外中断后流程异常 > 引用[1]中的 `POPUP_TO_CONFIRM_WITH_VALUE` 函数返回值规则与此一致,您的代码需处理可能的 `'E'` 返回[^1]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值