Creating and Using Controls
In this exercise, you will begin by opening an existing Windows Forms application. You will add a ToolBar control to the main form of the application, add the appropriate number of buttons to the ToolBar control, and set values for each of the buttons. After the design for the toolbar is complete, you will develop the code that handles the ButtonClick event of the toolbar and responds appropriately for each of the different buttons. You will then develop the code statements required to create an instance of a composite control, associate a context menu with the control, and then add the control to the control collection of a Panel control that already exists on the form. You will finish this exercise by creating the event handler for a ContextMenu control, developing the code that determines which control raised the event, and removing that control from the controls collection of the Panel control to which it belongs. This exercise assesses your knowledge of events and event handlers as well as your ability to use Windows Forms controls and ContextMenus at run time.
There are starter and solution files associated with this exercise. Browse to installfolder\Labfiles\ Lab02_1\Ex01\Starter to find the starter files, and browse to install_folder\Labfiles\Lab02_1\ Ex01\Solution to find the solution files. If you performed a default installation of the course files, installfolder corresponds to C:\Program Files\Msdntrain\2565.
Scenario
You have been given the task of adding a toolbar to the Purchase Order application and completing some code sections that are used to display purchase item data. You have been given the following table that contains design specification data for the toolbar. You will use this design information to construct the toolbar and the buttons that it contains.
|
Item |
Item property |
Property value |
|
ToolBar |
Name |
POToolBar |
|
POToolBar |
ImageList |
POImageList |
|
POToolBar Member 0 |
Name |
RefreshToolBarButton |
|
POToolBar Member 1 |
Name |
AddOrderItemToolBarButton |
|
POToolBar Member 2 |
Name |
SaveOrderToolBarButton |
|
POToolBar Member 3 |
Name |
PrintPreviewToolBarButton |
|
POToolBar Member 4 |
Name |
SubmitToolBarButton |
|
POToolBar Member 5 |
Name |
ViewUnsubmittedToolBarButton |
|
POToolBar Member 6 |
Name |
ViewSubmittedToolBarButton |
|
RefreshToolBarButton |
Tag |
Refresh |
|
RefreshToolBarButton |
ImageIndex |
0 |
|
RefreshToolBarButton |
ToolTipText |
Refresh product and customer data |
|
AddOrderItemToolBarButton |
Tag |
Add |
|
AddOrderItemToolBarButton |
ImageIndex |
1 |
|
AddOrderItemToolBarButton |
ToolTipText |
Add a new order item to the purchase order |
|
SaveOrderToolBarButton |
Tag |
Save |
|
SaveOrderToolBarButton |
ImageIndex |
2 |
|
SaveOrderToolBarButton |
ToolTipText |
Save the current purchase order |
(continued) Item
Item property
Property value
PrintPreviewToolBarButton Tag
PrintPreviewToolBarButton ImageIndex
PrintPreviewToolBarButton ToolTipText
SubmitToolBarButton Tag
SubmitToolBarButton ImageIndex
SubmitToolBarButton ToolTipText
ViewUnsubmittedToolBarButton Tag
ViewUnsubmittedToolBarButton ImageIndex
ViewUnsubmittedToolBarButton ToolTipText
ViewSubmittedToolBarButton Tag
ViewSubmittedToolBarButton ImageIndex
ViewSubmittedToolBarButton ToolTipText
PrintPreview
Print preview Submit
Submit purchase order data to the NWTraders database
Unsubmitted
View the unsubmitted orders Submitted
View report of submitted orders
After you have created the toolbar, you will develop code for the ToolBar.ButtonClick event. The following table identifies the actions that should be taken when each button is clicked.
Toolbar button name
Action taken when this button is clicked
RefreshToolBarButton
AddOrderItemToolBarButton
SaveOrderToolBarButton
PrintPreviewToolBarButton
SubmitToolBarButton
ViewUnsubmittedToolBarButton
ViewSubmittedToolBarButton
DataRefreshMenuItem.PerformClick() NewOrderItemButton.PerformClick() SaveOrderButton.PerformClick() PrintPreview()
DataSubmitMenuItem.PerformClick()
ViewUnsubmittedOrdersMenuItem.PerformClick()
ViewSubmittedOrdersMenuItem.PerformClick()
After the toolbar is complete, you will develop the code to add and remove an instance of a composite control from the controls collection of a container control (the Panel control, ProductOrderPanel, has already been added to the main form of the Purchase Order application). Each composite control represents a single purchase item; together, these purchase items make up a purchase order. Although users can add a new purchase item by using either NewOrderItemButton or AddOrderItemToolBarButton, you will add your code to the event handler for the NewOrderItemButton.Click event. You will add code to this event handler that creates an instance of the composite control (PurchaseOrder.OrderItemControl), sets the ContextMenu property of the new control (the context menu is used by the application user to remove a purchase item from the Panel control), and adds the control to the controls collection. To finish up this task, you will create an event handler that responds to a context menu click event, and you will develop the code that removes a purchase item (the purchase item that raised the context menu click event) from the controls collection of ProductOrderPanel.
|
Tasks |
Additional information |
|
1. Open the Lab02Application. sln file in Visual Studio .NET. The solution file is located in install_folder\Labfiles\ Lab02_1\Ex01 \Starter\ Lab02Application. |
a. For more information about opening a project file and starting an application, see the following resource: • The Visual Studio .NET Help documentation. For additional information about opening a project file, in Search, select the Search in titles only check box, then search by using the phrase Open Project Dialog Box. For additional information about starting an application from in Designer, search the Index by using the phrase Debugging Windows Applications. |
|
2. In the Design view, add a ToolBar control to MainForm.vb. Configure the Toolbar as specified in the scenario. |
a. For more information about the ToolBar control, see the following resources: • Lesson, Using Windows Forms Controls, in Module 2, "Working with Controls," in Course 2565A, Developing Microsoft .NET Applications for Windows (Visual Basic .NET). • Practice, Creating and Using a ToolBar Control, in Module 2, "Working with Controls," in Course 2565A, Developing Microsoft .NET Applications for Windows (Visual Basic .NET). • The Visual Studio .NET Help documentation. Search by using the phrase Introduction to the Windows Forms ToolBar Control. |
|
3. Use the Task List to locate the code section 'TODO: create an event handler for the POToolBar.ButtonClick event', and then create an event handler for the POToolBar.ButtonClick event. Develop the code that will invoke the appropriate action when a ToolBar button is clicked. |
a. For more information about the ToolBar.ButtonClick event and determining which button on a toolbar caused the ButtonClick event to be raised, see the following resources: • Lesson, Using Windows Forms Controls, in Module 2, "Working with Controls," in Course 2565A, Developing Microsoft .NET Applications for Windows (Visual Basic .NET). • Practice, Creating and Using a ToolBar Control, in Module 2, "Working with Controls," in Course 2565A, Developing Microsoft .NET Applications for Windows (Visual Basic .NET). • The Visual Studio .NET Help documentation. For additional information about handling the ButtonClick event of a ToolBar control and to see an example demonstrating a method for determining which button of a toolbar raised the ButtonClick event, search by using the phrase ToolBar.ButtonClick Event. |
|
4. Run your application to test the toolbar and the ButtonClick event handler. You can position the mouse over the ToolBar buttons to display ToolTips and click the buttons to make sure that your ButtonClick event handler is working correctly. |
a. For more information about the purchase order sample application, see the following resources: • Demonstration, Purchase Order Application, in Module 0, "Introduction," in Course 2565A, Developing Microsoft .NET Applications for Windows (Visual Basic .NET). |
|
Tasks |
Additional information |
|
5. Use the Task List to locate the code section 'TODO: create a purchase item', and then create an instance of the PurchaseOrder. OrderItem Control control named tempProductOrder that uses ProductContextMenu as a context menu. |
a. For more information about using a ContextMenu control at run time, see the following resources: • Lesson, Creating Menus, in Module 2, "Working with Controls," in Course 2565A, Developing Microsoft .NET Applications for Windows (VisualBasic .NET). • Practice, Updating Menus at Run Time, in Module 2, "Working with Controls," in Course 2565A, Developing Microsoft .NET Applications for Windows (Visual Basic .NET). • The Visual Studio .NET Help documentation. Search by using the phrase Adding Context Menus to Windows Forms. |
|
6. Use the Task List to locate the code section 'TODO: add a purchase item to ProductOrderPanel', and then add the new purchase item to the controls collection of the ProductOrderPanel control. |
a. For more information about adding controls to a control collection, see the following resources: • Lesson, Adding Controls at Run Time, in Module 2, "Working with Controls," in Course 2565A, Developing Microsoft .NET Applications for Windows (Visual Basic .NET). • Practice, Adding and Removing Controls at Run Time, in Module 2, "Working with Controls," in Course 2565A, Developing Microsoft .NET Applications for Windows (Visual Basic .NET). • The Visual Studio .NET Help documentation. Search by using the phrase Control.Controls Property. |
|
7. Use the Task List to locate the code section 'TODO: handle the click event for the DeleteOrderItemMenuItem', and then create an event handler for the Click event of the DeleteOrderItemMenuIte m menu item that calls the DeleteOrderItem() procedure. |
a. For more information about creating event handlers, see the following resources: • Lesson, Creating an Event Handler for a Control, in Module 2, "Working With Controls," in Course 2565A, Developing Microsoft .NET Applications for Windows (Visual Basic .NET). • Practice, Creating and Event Handler for a Control, in Module 2, "Working With Controls," in Course 2565A, Developing Microsoft .NET Applications for Windows (Visual Basic .NET). • The Visual Studio .NET Help documentation. Search by using the phrase Consuming Events. |
|
8. Use the Task List to locate the code section 'TODO: determine the index number of the control that will be deleted', and then assign the index number of the control that displayed the context menu to a variable named currentControlIndex. |
a. For more information about determining which control displayed a context menu, see the following resources: • Lesson, Creating Menus, in Module 2, "Working with Controls," in Course 2565A, Developing Microsoft .NET Applications for Windows (VisualBasic .NET). • Practice, Updating Menus at Run Time, in Module 2, "Working with Controls," in Course 2565A, Developing Microsoft .NET Applications for Windows (Visual Basic .NET). • The Visual Studio .NET Help documentation. Search by using the phrase ContextMenu.SourceControl. |
|
Tasks |
Additional information |
|
9. Use the Task List to locate the code sections 'TODO: remove a control from the middle of the ProductOrderPanel control collection' and 'TODO: remove a control from the end of the ProductOrderPanel control collection' and then, in each case, create a code statement that will remove the control that displayed the context menu from the controls collection of ProductOrderPanel. |
a. For more information about removing controls from ControlCollection, see the following resources: • Lesson, Adding Controls at Run Time, in Module 2, "Working with Controls," in Course 2565A, Developing Microsoft .NET Applications for Windows (Visual Basic .NET). • Practice, Adding and Removing Controls at Run Time, in Module 2, "Working with Controls," in Course 2565A, Developing Microsoft .NET Applications for Windows (Visual Basic .NET). • The Visual Studio .NET Help documentation. Search by using the phrase Control.ControlCollection.Remove Method. |
|
10. Run your application to test the code that you just created. You should now be able to add and remove purchase items from the controls collection of ProductOrderPanel. |
Additional information is not necessary for this task. |
|
11. Save your changes, and then close Visual Studio .NET. |
Additional information is not necessary for this task. |
Post a comment