Add Button

Designing an APF application the developer has two button controls at hand, cPushButton and cCommandButton. The cPushButton is the simple button class that is used for a standard button implementation, i.e. add a button to the form and hook up the click event. This page focuses on describing how to use a cCommandButton.

A cCommandButton is a commander control. It is a control  used to perform an action in a window when the user interacts with it. A commander is a control that is connected to a command where the command holds the logic, while the commander holds the user interface. The Command Button is used wherever the enabled state of the button needs to be controlled. Normally you add/create the cCommandButton from the Command Manager Designer directly, but it is possible to add a cCommandButton from the Visual Studio toolbox as well.  

For more detailed information on how to create and handle commands using the Command Manager Designer, see the commands page.

Contents

Create a command for a command button

  1. Use Visual Studio designer view on the form where you want to add a command button.
  2. Open the context menu for the commandManager component in the designer surface of Visual Studio.
  3. Select the menu item Designer....
  4. In the designer dialog, on the left hand side, under the Available Commands label, click the Add new command button .
  5. Select the created command in the list of commands.
  6. In the command property sheet below the list of commands, enter values for the command's properties.

    At least enter values for Name and Caption to have a command that is useful for a command button.

Note: Give the command a useful name. The name of the command will be reflected in event handlers as well as generated controls. A name like command<Caption> is a good name to start out with.

Figure 1. Add a new command with the Command Manager Designer.

Add a command button from Visual Studio Toolbox

  1. Open the Visual Studio toolbox.
  2. Add a cCommandButton control using drag and drop from the Visual Studio toolbox to the form.
  3. Select the created button on the designer surface.
  4. Assign the buttons Command property, by choosing a command from the dropdown menu.

Add button from Command Manager Designer

  1. Use Visual Studio designer view on the form where you want to add a command button.
  2. Open the context menu for the commandManager component in the designer surface of Visual Studio.
  3. Select the menu item Designer....
  4. In the Action Tree, select the Buttons node.
  5. Click the Add new item button .
  6. Select the created pushbutton in the tree view.
  7. Assign the buttons Command property, by choosing a command from the dropdown menu.

Tip: You can quickly create a new command button for a command by dragging the command with the mouse, and dropping it directly on the Buttons node.

Subscribe to command events

The form having the button that uses a command, should subscribe to the command's Inquire and Execute events.

  1. Use Visual Studio designer view on the form where you want to add a command button.
  2. Open the context menu for the commandManager component in the designer surface of Visual Studio.
  3. Select the menu item Designer....
  4. Select the created command in the list of commands.
  5. Click the button Events in the property sheet to view the command's events.
  6. Double click on both events and the Designer will generate the callback methods <command name>_Execute and <command name>_Inquire.
  7. Press the Close button.

The command's Execute event is fired when the command button is clicked. When the event is fired, the callback method will be called. The callback method should perform the actions appropriate for the button. The Inquire event callback methods are used by the application to set the state of commands to reflect the state of the application. Use the Inquire event callback method to enable or disable a command button. This is done by changing the Enabled property on the Command. That property change on the command will be reflected on the command button. Another property that is reflected on the cCommandButton is the Caption property on the Command, which will set the Text of the button.