Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
This lesson shows how to add a command to a screen and write code that executes when the command is clicked at run time. The command displays a message box that contains the results of a calculation.
Adding a Command
Other lessons show how to enable, disable, and remove the standard Add, Edit, and Delete commands on a screen.
To add a command to a screen
In Solution Explorer, double-click the CurrentProductList screen node.
In the Screen Designer, expand the Command Bar node under the DataGrid | ProductsByCategory node.
In the Add list, select New Button.
The Add Button dialog box appears.
In the Add Button dialog box, select New Method and type ProductCount in the Name box, and then click OK.
In the left pane, select ProductCount.
In the Properties window, click Edit Execute Code.
The Code Editor appears and displays the ProductCount_Execute() method.
In the ProductCount_Execute() method, add the following code.
ShowMessageBox("There are " & ProductsByCategory.Count.ToString & " products in this category.", "Category Count", MessageBoxOption.Ok)String count = this.ProductsByCategory.Count.ToString(); this.ShowMessageBox("There are " + count + " products in this category.", "Category Count", MessageBoxOption.Ok);This code displays a message box when a user clicks the button that you just created.
Press F5 to run the application.
Verify the new command by opening the Current Product List screen and selecting a product in the Current Products list, and then clicking the Count button in the Products By Category list. The Category Count message box should appear and should display the number of items in the matching category.
Closer Look
This lesson showed how to add a command to the CurrentProductList screen, give it a Display Name and Description, and add the code that runs every time that the command button is clicked. The code executes the ShowMessageBox method, which displays a message box.
The code enclosed in parentheses contains the parameters to the method, in this case the message to display, the caption for the title bar, and the button to display in the message box. The message parameter contains both text and the Count property of the ProductsByCategory; the Count property returns an Integer and the ToString function converts it to a String.
Next Steps
In the next lesson, you will learn how to create a shell command that appears on the application toolbar.
Next lesson: Adding a Shell Command to the Application Toolbar
See Also
Tasks
How to: Add a Custom Command to a Screen