Responding To Dynamically Created Controls And Menuitems

Date: 12 September 1997
Author: Mark Caroli, University of Mannheim, Germany

Overview

This articles issue is the treatment of messages sent by dynamically created controls or menuitems. The first section only focusses on controls . In the second one you get to know how to insert menuitems at runtime . Finally, the response to dynamically created menuitems is explained. In addition to that there is a sample for download purposes.

Responding To Dynamically Created Controls

The standard procedure for responding to a message sent by an arbitary control looks like this:

For the most cases that is suitable, but the controls you want to work with must be known at design time. If the controls are created at runtime this technique does not provide any possibility to respond to messages coming from the new controls. The key to the solution of this problem is the TWindow::EvCommand() function which is called, when something happens in your control. You receive three values:

These are sufficient to identify the sending control and the message. A specific function call can be invoked. The code following code snippet shows how to respond to the BN_CLICKED message of three dynamically created TButton objects. They have the IDs IDBASE, IDBASE+1 and IDBASE+2. As you cann see from the code you have to keep track of the ID's of the new controls.

Adding New Items To A Menu

This section describes in brief how to add new menuitems to an existing menu. First, you need to retrieve the handle of the window holding the menu. Normally this is the FrameWindow. With this handle you can retrieve the menu. The application of the TMenu::GetSubMenu() method delivers the submenu where you want to add the menuitems. Use the TMenu::InsertMenu(...) function to add the items.

In the sample above the items are inserted by position. The first parameter specifies the position, the second one is self explainatory, the third one is the ID of the new item and finally we have the item string.

Responding To Dynamically Created Menuitems

Responding to dynamically created menuitems is nearly the same as responding to controls created in the same manner. You just have to overload a function called EvCommandEnabler(...) in order to avoid that the new items are enabled when you click on the popup menu. The implementation of this function does nothing else then expilicitly enabling the new menuitems.

Further Resources