February 28, 2021
Hot Topics:

Creating a App using JScript .NET and Windows Forms

  • By Essam Ahmed
  • Send Email »
  • More Articles »

Working with Windows Forms Controls

The sample application includes a number of controls, all of which are easy to work with. When you start the sample application (you can download the sample's source code using the link at the end of this article), the tab strip control starts with the tree view control visible, as shown in Figure 1. The tree view control organizes information using a hierarchal structure - you can access lower-level members by clicking on the plus symbol next to the root nodes.

When you select a node and click the "Show Current Node" button, a message box pops (see Figure 2) describing the currently selected node and its full path. Here's the code that handles the button's click event:

// The following line appears elsewhere in the class...
private var treeView1 : System.Windows.Forms.TreeView;

// This function handles the "Show Current Node"
// button's click event
function btnCurrentNode_OnClick( source:Object, e: EventArgs )
    MessageBox.Show("No currently selected node\n\nPlease 
      select a node and try again",
    MessageBox.Show("Selected \"" + treeView1.SelectedNode.Text + 
       "\"\nLocated at: \"" + treeView1.SelectedNode.FullPath+"\"",
      "Selected Node",

Figure 2 - Displaying the currently selected node

If the user does not select a node, the tree view's SelectedNode is null, otherwise, it refers to TreeNode object that describes the currently selected node. The current node's Text property has the display text shown in the tree view control, while the FullPath property contains the complete path (starting form the root node) to the currently selected node. The FullPath property is useful when you want to, for example, perform an operation on a particular file in a directory listing. You can add nodes to the tree view control using its Add or AddRange methods - see the source code for details.

The tab strip control contains a Month Calendar control under the Month Calendar tab. The control allows users to select a date based on a calendar metaphor that's easy to use and understand. The control offers a number of customization features that you can access using the control's properties. For example, the month calendar control has a FirstDayOfWeek property that allows you to specify the first day of the week (the day of the week that appears on the left side of each week in the month) - the sample application sets the FirstDayOfWeek property to Monday.

The last control the tab strip control hosts is the Checked List Box control, which is similar to a regular List Box control except that each item has a check box next to it. When the user clicks the "Checked Items" button, the following event handler executes, which also causes a message box to pop up (see Figure 3):

function btnCheckedItems_OnClick( source:Object, e: EventArgs )
  var i:int;
  var msg:StringBuilder = 
    new StringBuilder("Checked items:\n");
    for(i=0;i< checkedListBox1.CheckedItems.Count;i++)
      msg.Append("* ");
    "Checked Items",

Figure 3 - Displaying checked items

The function uses a StringBuilder object to compose a string that contains a list of the currently selected items. A StringBuilder (from the System.Text namespace) is a dynamic string that's well suited for building strings, as this function does, at runtime and is recommended instead of the System.String object, which is considered to be an immutable object. The only thing you need to watch out for is that you call the StringBuilder object's ToString method when you want to refer to its String representation, otherwise you'll get a compile-time error about a type mismatch whenever a method (like the MessageBox's Show method) requires a String object as part of its list of parameters that it accepts.

The checked list box control represents checked items as a collection, which exposes a Count property making it easy to iterate over the items the user selects as shown in the listing.

Page 2 of 4

This article was originally published on February 22, 2002

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date