HomeC# C# Simplified, part 6: Working with Window...
C# Simplified, part 6: Working with Windows Applications
In this article, you will learn about Windows-based applications by creating and modifying one. You will also learn two ways to create controls for these applications, and why TextBoxes are particularly important.
If you have worked with Visual Basic 6.0 or Visual C++ 6.0, you may be aware of the various Graphical User Interface (GUI) elements with which you worked. These elements include buttons, text boxes, check boxes, radio buttons and many more. In fact there are dozens of other controls which are used, along with various Windows-based applications. Windows-based applications are called WinForms for short. With WinForms, you can develop standalone applications. This means that you can run those applications only under a Windows platform and with the .NET Framework. The end user should either install .NET Framework or .NET redistributable framework in order to work with Windows-based applications. If you want to develop ASP.NET applications, you should use WebForm controls.
The System.Windows.Forms namespace provides all necessary classes and methods for developing powerful GUI applications. It is this namespace which provides access to WinForm controls and their associated methods and properties. If you are using a plain text editor instead of Visual Studio .NET, you have to know about the various properties and their corresponding values. But if you have Visual Studio .NET, the properties for each of the controls can be modified from the properties window, and the appropriate coding will be automatically done in the background. The compilation and execution stages are same as any other C# application. The only difference is that the output can be viewed through a Form. The source code will become lengthy as you develop complex application with WinForms.
The code samples in this article assume that you are using a plain text editor such as Notepad for coding purposes.
The .NET Framework supplies more than 60 controls for developing Windows applications. Even though all these controls are not used in a single project, they can be suitably used for various purposes. For example, if you are developing a data entry project, you can make use of TextBoxes and Validation Controls. If you are working on a billing project, you can use Buttons, DataGrid and CheckBoxes. The usage of these controls depends upon each project's requirements.
If you have Visual Studio .NET, you can view the names of all these controls from the ToolBox. Just place your mouse pointer over the ToolBox section on the left hand side and the Visual Studio .NET automatically displays all the controls. As you can see, these controls are divided into tabs. For instance, the controls used for developing database applications are located in a tab titled "Data." In the same way, reusable and user created controls are placed in a tab titled "Components."
The different types of controls used for building a WinForm application is given in Figure 6.1.
I will explain some of the important controls shown in the above figure in this article and in the upcoming articles. You can learn about rest of the controls which are not discussed in this series with the help of the MSDN library or the documentation which came with the .NET Framework SDK.
Visual Studio .NET supports a feature called Dynamic Help. In order to make use of this feature you have to install the MSDN library which came with Visual Studio .NET. To understand its functionality, add a control like Timer to the form and press F1.
Lines 3 and 4 refer to the required namespaces to be used in this program. Note that a new namespace, as discussed in one of the previous sections, have been called. This namespace exposes the Form class and its functionalities. Line 6 specifies the class declaration that extends the Form class.
Line 9 calls the constructor, which will be of course the same as the class name. You can perform whatever task you want, such as creation of controls like Buttons, TextBoxes and so forth inside the constructor.
Lines 10 and 12 refer to the beginning and ending of class constructors by using opening and closing curly braces. Line 14 specifies the Main method, which is required in all programs. As discussed in one of the previous articles, it is the entry point for every C# application. Finally, in line 16 an instance of the class HelloWorldWin1 is created as an argument to the run() method of the Application class.
You can modify the above listing by adding additional statements inside the constructor. The relevant explanations are shown as comments
/*Changes the forms location
Location is the property of the Form class
The keyword "this" refers to the current instance of the Form
*/
this.Location = new System.Drawing.Point(300,300);
If you are calling the System.Drawing namespace (Using System.Drawing) declaration on the top of the program, then the code given below is equivalent to the above listing:
this.Location = new Point(300,300);
The keyword "this" can be applied only to Forms instance and not to other controls
/* Changes the Forms Title
Text is the property of the class Form
*/
this.Text = "Welcome to Windows Forms"
/* Changes the mouse cursor
when you move your mouse inside the Form
*/
this.Cursor = Cursors.Hand
/* Changes the background color of the Form
Backcolor is the property of the Form class inside the
The first parameter denotes width and the second denotes height
*/
this.size = new Size(400,200)
Listing 6.3 shows the modified version of the previous listing after applying certain properties mentioned in this section:
Listing 6.3
001: // HelloWorldWin2.cs
002: // -----------------
003: using System;
004: using System.Windows.Forms;
005: using System.Drawing;
006:
007: public class HelloWorldWin2:Form
008: {
009:
010: public HelloWorldWin2()
011: {
012: //Forms title
013: this.Text = "Welcome to Windows Forms";
014: this.Size = new Size(400,200);
015: this.Cursor = Cursors.Hand;
016: }
017:
018: public static void Main()
019: {
020: Application.Run(new HelloWorldWin2());
021: }
022: }
The output of the above code upon execution will look like Figure 6.2.
Figure 6.2
As explained in the beginning, the .NET Framework provides a lot of controls for developing GUI based applications. Before proceeding further, you should know how to apply these controls in your WinForm application. You can easily add controls to the Form by using the Control class of System.Windows.Forms namespace. The steps required for adding a WinForm control are described below.
First, you must create an instance of the control. For example, if you are adding a TextBox and a Button to the Form, you have to create an object (Instance) of the TextBox and Button class as in listing 6.4:
Listing 6.4
TextBox txt1 = new TextBox();
Button btn1 = new Button();
As the next step, you must apply the necessary properties of the TextBox and the Button control using the above objects txt1 and btn1. The properties can be that of Location, Width, Height, BackColor, and so on. It is recommended that you give a meaningful name for the object so that you can easily debug the code later.
Finally, you must add the object to the Forms control collection. For this purpose, you should make use of the Add() method of the Control class as shown in listing 6.5:
Listing 6.5
this.Controls.Add(txt1);
this.Controls.Add(btn1);
In the above code, the keyword this signifies the current instance of the Form.
That's all there is to it. You have learned how to add controls to the Form. In the forthcoming sections, you will learn more about each of these controls in detail.
It is very easy to add controls using Visual Studio .NET. You don't have to write any code as explained in the above walkthroughs.
Start a new Visual C# .NET project (File | New | Project -> Visual C# Projects -> Windows Application) and add a TextBox and a Button control using ToolBox. You should be able to see the Properties Window on the right side. Otherwise, press F4 to make it visible. You can modify the various properties of a WinForm control using this window as shown in Figure 6.3.
Figure 6.3 Properties Window
You can also select the required control from the top drop-down list and read a short description of each of the properties from the bottom section of this window. Double click anywhere on the Form to view the code generated by Visual Studio .NET.
Working with TextBoxes
With the help of Textboxes, you can enter information. That is why this control plays an important role in all applications. The entered information can be saved to a file, XML file or a database, according to your requirements. Textboxes are of two types, SingleLine and MultiLine. The difference between these two types is that multi-line boxes contain scroll bars and users can enter more that one line of text into them. It is similar to TextArea in HTML.
The super class of this control is TextBoxBase. This class provides functionalities for advanced controls like RichTextBoxes. Listing 6.6 illustrates the working of a multi-line textbox control:
Listing 6.6
001: // MyText.cs
002: // ---------
003: using System;
004: using System.Drawing;
005: using System.Windows.Forms;
006:
007: public class MyText: Form
008: {
009: // Object of TextBox created
010: TextBox t1 = new TextBox();
011:
012: MyText()
013: {
014: this.Controls.Add(t1);
015: t1.Location = new Point(50,50);
016: t1.Size = new Size(200,70);
017: t1.Multiline = true;
018: t1.ScrollBars = ScrollBars.Vertical;
019: t1.TextAlign = HorizontalAlignment.Center;
020: t1.CharacterCasing = CharacterCasing.Upper;
021: this.Text = "TextBox Control Demo";
022: }
023:
024: public static void Main()
025: {
026: Application.Run(new MyText());
027: }
028: }
Compilation
csc MyText.cs
Execution
MyText
Output
In the Textbox, whatever you enter will appear in uppercase, because of line 20. Also, the text will be aligned to center due to the statement in line 19. In Visual Studio .NET, you can change the above properties using the properties window after selecting the TextBox.