Interacting with .NET WinForms, Part 2

Tuesday Feb 5th 2002 by Anand Narayanaswamy

We conclude our tutorial on working with Windows Forms in Microsoft.NET. Here, we'll examine some of the important GUI Controls and their application in C#.

We use a lot of controls like Buttons, Label, Checkbox, Radio Button, Text boxes, etc. while developing a GUI application. .NET provides a wide range of class, properties, and events for developing a complete GUI program by using the Control class of System.Windows.Forms namespace. In this part of our tutorial, I'll examine some of the important GUI Controls and their application in C#.

General Method for Adding Controls to Forms

  1. Create an instance of the control.
  2. Apply necessary properties like location, size, width, height, etc.
  3. Finally, add the Control object to the Form's Controls Collection.

Using TextBox

Generally, Text boxes are of two classes, viz., Single line and multiple line boxes. Multiple line boxes have scroll bars. Also, users can enter more lines of text in them. The superclass of TextBox Control is TextBoxBase. It also provides functionalities for advanced controls like RichTextBoxes.

The following listing shows the usage of TextBox by examining important properties. Keep in mind that some of the properties derive from the TextBoxBase class. TextBox class provides only a limited set of properties.

Listing 1.

using System;
using System.Windows.Forms;
using System.Drawing;
        public class Text: Form  {  
        // Text box object created
        TextBox t1 = new TextBox();	
        Text()    {
        //  Text box added
        t1.Location = new Point(50,50);
        t1.Size = new Size(200,50);
        t1.Text = "Class something to test";
        t1.Multiline = true;
        t1.ScrollBars = ScrollBars.Vertical;
        t1.TextAlign = HorizontalAlignment.Center;
        t1.CharacterCasing = CharacterCasing.Upper;
        public static void Main() {
        Application.Run(new Text());

I recommend you test the above listing and change the values of location, size, etc. to learn more. All other properties are self-explanatory.

Buttons, Radio Buttons, and Check Boxes

Buttons are the most important control in any GUI environment. We use Buttons to perform actions such as feeding the data to the database, generating reports, etc. The base class of the Button Control is called ButtonBase, which provides us with three classes for CheckBox, RadioButton, and Button. The ButtonBase class defines a lot of properties, which we'll use in our programs. The listing below examines each of these classes in detail.

Using Buttons

Listing 2.

using System;
using System.Windows.Forms;
using System.Drawing;
        public class But:Form   {
        Button b1 = new Button();
        Button b2 = new Button();
        But()  {
        b1.Text = "OK";
        b2.Text = "CANCEL";
        b1.BackColor = Color.Pink;
        b2.BackColor = Color.Yellow;
        b1.Size = new Size(40,30);
        b2.Size = new Size(60,30);
        b1.Location = new Point(50,40);
        b2.Location = new Point(50,80);
        b1.FlatStyle = FlatStyle.Popup;
        this.Text = "Using Buttons...";
         public static void Main()  {
        But b = new But();

Type in the above code using any text editor and save the file as But.cs. Execute and observe the result. Try making changes to the values on your own.

Using RadioButtons

To the above Listing, add the following piece of code for creating Radio Buttons.

Listing 3.

public class Rad: Form  {
        // First Radio Button created
        RadioButton r1 = new RadioButton();
        // Second Radio Button created
        RadioButton r2 = new RadioButton();
        // GroupBox created
        GroupBox g1 = new GroupBox();
        Rad()      {
        r1.Text = "Java";
        r1.Location = new Point(20,20);
        r2.Text = "C++";
        r2.Location = new Point(20,40);
        g1.Location = new Point(20,20);
        g1.Text = "Your Options";
        g1.BackColor = Color.Pink;
        // Radio Buttons added to group box
        // Group Box added to the form

Here, RadioButtons are added to a GroupBox, which is similar to Frames in VB 6.0.

Using CheckBox

Checkboxes enables us to select more than one item at a time as compared to a RadioButton, where only one item can be selected. To create Checkboxes, simply modify the above listing by creating objects of Checkbox. Try changing the above code and observe the result.

Listing 4.

 public class Chk: Form  {
        // First checkbox created
        CheckBox c1 = new CheckBox();
        // Second checkbox created
        CheckBox c2 = new CheckBox ();
       // Remaining code goes here

Using ComboBoxes

With Combo boxes, you can select a list of items -- but only one item at a time. Copy the listing given below and execute as usual.

Listing 5.

public class Cbo: Form  {
          // Combo Box created
        ComboBox cb = new ComboBox();
        Cbo()      {
        cb.Location = new Point(40,40); 
         // Items added to the ComboBox
        cb.Items.AddRange(new object[5] {"Microsoft","Sun","Oracle","Sybase","Cisco"});
        // Main method goes here

Here, items are added to the combo box by using the AddRange() method of the ComboBox class. If you need to add more items, then you should update the array index value.

About TabIndex and TabStop Properties

You can set the Tab order of the controls by using the TabIndex property as shown below:

Text1.TabIndex = 2

Moreover, you can stop the Tab key from entering a control by setting the TabStop property to False.

Using the MonthCalendar Control

You can easily add a calendar control to your form by using the MonthCalendar class of the System.WinForms namespace. The listing shown below offers sample code for using this interesting control.

Listing 6.

        MonthCalendar mc = new MonthCalendar();
        // Constructor goes here
        mc.Location = new Point(40,40);

Adding Shortcuts

You can easily add shortcuts to buttons by using the '&' symbol in front of the relevant alphabet as shown below:

        b1.Text = "&OK"     // Can be invoked by using ALT + O Key

The button can be activated by using Alt + underlined alphabet combination.

About the Author

Anand Narayanaswamy is a graduate of the University of Kerala. He currently works as an instructor teaching Java, Visual Basic, and technologies such as ASP and XML and a regular contributor to gamelan and sites. He enjoys learning new programming languages like C#. Currently, Anand lives in Thiruvananthapuram, Kerala State, India. He can be contacted via his Website or via email at

Mobile Site | Full Site
Copyright 2017 © QuinStreet Inc. All Rights Reserved