by March 4, 2003 0 comments

We continue our journey with WinForms this month and look at some other controls and features of the same. Let’s start off with another new control introduced in WinForms, the

As the name suggests, the CheckedListBox is the version of ListBox with direct support for check boxes that can be used to signify multiple selections.

Items are added to the list using the Add method of the Items property of the CheckedListBox. The Add method allows the addition of an Object, with an overload of the same specifying whether the item should be checked at the time of insertion or not. The code that produced the shown output is as follows.

Check boxes can be used for multiple selections


Once the items are selected, there are two ways to go about determining the selection. One is to use the CheckedItems property of the CheckedListBox that returns a collection of items (returned as Object) selected. This approach allows us to directly get to the selected Object question.

Another way is to use the CheckedIndices property that returns a collection of the indexes of the selected items. These indexes, then, can be used to index into the Item property to get the relevant selected objects. Here’s a sample code that uses the CheckedItems property to determine the selection:

// display the selected items…
StringBuilder sb = new StringBuilder();

// determine the selected objects…
for(int i=0;i<this.chkLB.CheckedItems.Count;i++)
MessageBox.Show(sb.ToString(),”Selected Items”); 

The important point to be understood here is that the selected items are returned as an Object, and needs to be processed appropriately. In the example above, it was a simple string, so a ToString works out just fine, but that may not be always the case.

TreeView control
We now focus on another important WinForm control that requires decent attention is the TreeView control. This control has changed since VB6. While then addition of a node required the insertion to take place using a reference of the node that was to be the parent, under WinForms the addition process has changed completely.

You can see the
TreeView control here

To commence a TreeView population, we start of by creating a TreeNode object that will be the child of a given node:

TreeNode tnFChild = new TreeNode(“First Child”);

Next, we create the node under which this child node will be added:

TreeNode tnRoot = new TreeNode(“Root”,new TreeNode[]{tnFChild});

While there are other overloaded versions of TreeNode constructor, we are using the one that helps understand the concept, by simply taking the text to be displayed, and the array of child TreeNode objects that will be inserted under this node that shall become their parent. 

Finally, the addition takes place by using the Add method of the Nodes property of the TreeView control:


tvwMain is the TreeView control object. To expand all the nodes by default, we invoke the ExpandAll method of the control:


If you wish to have checkboxes displayed next to the items, for multiple selections, set the CheckBoxes property of the control to true.

To know if a given node is selected or not, use the Checked property of the TreeNode object, which shall return a Boolean to indicate the same. 

Another important thing is to determine the currently selected node, the reference to which, i.e. a TreeNode reference, is given by the SelectedNode property of the TreeView control. 

Now, here’s a typical problem scenario faced when working with TreeView: one would script the MouseUp event of the TreeView to do some processing on the right-click of a node, as shown below: 

// select the node that was right-clicked…
if (e.Button==MouseButtons.Right)
MessageBox.Show(tvwMain.SelectedNode.Text,”Selected Node Text Is”); 

Left-click the Root node, and then right-click the First Child node.

A typical problem scenario
faced when working with TreeView

The text displayed is that of the Root node and not the node that was right-clicked, namely the First Child node. To overcome this problem, we use the GetNodeAt method of the TreeView control to determine the TreeNode object that is present at the coordinates where the mouse was right-clicked, and set it as the new selected node, and then do the processing on it:

// select the node that was right-clicked…
if (e.Button==MouseButtons.Right)
// get the TreNode at the location mouse
// was right-clicked
TreeNode tnSel = tvwMain.GetNodeAt(e.X,e.Y);
if (tnSel!=null)
// select the node…
tvwMain.SelectedNode = tnSel;

Now, the output is as expected. 
With the important facets of TreeView control in place, here’s a brief look at another neat control, the Panel control. This control has the simple reason of life and that is to act as a container control for other controls. So, how different is this than the GroupBox control? Well, for one simple reason, and that is its capability to provide a scroll view to the contained controls, unlike the GroupBox where the controls need to be contained right within the exact borders of the control. 
Setting the AutoScroll property to true enables the scroll bar that you see, which is allows you to scroll through the Panel in the design mode to add controls, and at runtime, allows you to go access the contained controls. In the screen shot above, we have a checkbox, combobox, and a multi-line textbox. Thus, Panel allows you to easily contain more controls than what the Form size might allow.

Invoking the ShowDialog method of the control displays the folder selection dialog

.NET 1.1 and VS.NET 2003 brings another neat control, the FolderBrowserDialog control. This control allows you to display the dialog that lets you select a folder. Prior to .NET 1.1, this wasn’t not directly possible, so third-party components came into play. You can check one such component that I wrote, called BrowseDialog, that is available for free download from
Invoking the ShowDialog method of the control displays the folder selection dialog:

The control’s Description property can be set to display a custom text, which in this case has been set to PCQ Folder Selection Example Dialog. Once the folder has been selected, the path to the same is available via the SelectedPath property:

Here’s a sample code that results in the above dialog coming up (of course, the path can be different!):

// set the description..
this.folderBrowserDialog1.Description = “PCQ Folder Selection Example Dialog”; 
if (this.folderBrowserDialog1.ShowDialog()==DialogResult.OK)
// display the selected folder path..
MessageBox.Show(this.folderBrowserDialog1.SelectedPath,”Folder Selected Is”); 
More productivity

We have had a decent insight into what WinForms development is all about, and had a look at some of the major programming controls, both old and new. All in all, the idea is that once you have WinForms development up and running, your learning curve across languages, for GUI development, is drastically reduced since WinForms is part of the .NET FCL (Framework Class Libraries), and thus, you as a developer become more productive. This series was aimed at giving you an introduction to WinForms development, and covering some of the key issues required for the development. 

Next month, we’ll commence another series on WebForms development where we shall look at some advanced
WebForm-development techniques.

Kumar Gaurav Khanna, Microsoft .NET MVP, 

No Comments so far

Jump into a conversation

No Comments Yet!

You can be the one to start a conversation.