Windows Forms: Creating an SDI ListView and Control Panel UI, Page 2
Adding and Docking a ListView ControlOne of the major benefits that Visual Studio .NET has over previous versions of the editor is it enables you to dock controls when creating forms. Using this feature, you can place a control on a form, define to which side it docks (top, bottom, left, right, or fill), and the control will automatically be moved or sized appropriately when the form itself is resized by the user. This is especially useful when you wish to create what in MFC would have been a CCtrlView-derived view (such as a CListView) where the list control encompasses its parent view's entire client area. To create this effect, you drag a ListView control onto the form and set its Dock property. (When you set the Dock property, you'll see a graphical representation of the options with the middle option representing the "Fill" option.) Once you've done that, the control will take up the entire client area of the form and be dynamically resized with the form.
Of the many available properties, the following are ones you'll find yourself setting most often, as well as the new ones that are most helpful:
- Full row selectThis boolean propertymissing in previous versions of Visual Studiospecifies that if the user selects any column in a listview that the entire row will be selected and highlighted.
- Hide selectionThis boolean property specifies whether or not the highlighted (selected) row(s) should be highlighted even if the listview control doesn't have focus.
- MultiSelectThis property allows you to specify whether the user can select more than one item from the listview.
- SortingBy default, the listview is not sorted. This property allows you to specify whether it should be sorted and in which direction (ascending or descending).
- ViewThis one allows you to specify the type default view: Large Icon, Small Icon, Details, or List.
- ColumnsA very welcome addition to Visual Studio, this property enables you to define at design time the columns that will make up the listview. In previous versions of Visual Studio, you had to define these in your code even if they never changed.
- ItemsFor situations where you know at design time the items that will be in the listview, this is a nice addition to the listview properties.
Adding a PanelThe Panel control is a little like the MFC CDialogBar in that it can be defined to dock on one or more sides and be the parent of one or more controls. As an example of that, you might have a listview control docked to fill the client area with a panel docked to the top or bottom that contains controls with buttons that perform actions on selected items. Another example is a header/detail-like form such as invoicing where you have a panel docked to the top containing controls for header information such as invoice number, customer, and so on, and a listview or data grid below that for the invoice detail lines. The possibilities are endless and allow you a great deal of flexibility in crafting your user interface.
To add the Panel control to your form, simply drag it from the toolbox onto a form and specify the Dock property. Any control dropped onto that panel becomes its child window and will move with the parent panel. You can also set the child control Dock properties as needed.Figure 1 lists all Windows Services in a list view with a dock value of fill. The tree view control is a child of a panel with a dock value of left and the buttons are on yet another panel control with a dock value of bottom.
Looking ForwardI intentionally kept this first article simple as the majority of my readers are C++ programmers whowhile using Managed C++haven't necessarily used it in connection with creating user interfaces with Windows Forms. Based on the feedback I receive, I'll decide whether to continue writing more UI tips like this. So drop me a line if there's something in specific that you'd like me to cover in next week's article!
About the Author
Tom Archer owns his own training company, Archer Consulting Group, which specializes in educating and mentoring .NET programmers and providing project management consulting. If you would like to find out how the Archer Consulting Group can help you reduce development costs, get your software to market faster, and increase product revenue, contact Tom through his Web site.