|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Services
Chapters
Feature Zones
|
IntroductionThis article is a very simple introduction on writing a Windows Form application for the Microsoft.NET framework using C#. The sample application demonstrates how to create and layout controls on a simple form and the handling of mouse click events. The application displays a form showing attributes of a file. This form is similar to the properties dialog box of a file (Right click on a file and Click on Properties menu item). Since attributes of a file will be shown, the sample will show how to use File IO operations in .NET framework. What is the starting point?
Like every Win32 application source, we will start with the inclusion of some header
files. C# does not make use of header files; it uses namespaces to accomplish
this purpose. Most of the C# core functionality is implemented in the using System;
using System.WinForms;
We will need some more namespace definitions, but I will explain them as we go
along with this sample application. Like every C# application, Windows Forms
application will be defined as a class. Since we will be making use of the public class WinFileInfo : Form
The next thing that needs to be identified is the entry point for the application. Unlike
Win32 applications, the method public static void Main ()
{
Application.Run (new WinFileInfo ());
}
The How do I layout controls on the form?
Since we are writing a GUI application, we need some controls on the form. And because this
application has been written using the .Net SDK, no wizard or tools have been used.
So how can we put controls on the form and define their location sizes?
This is very much like writing a resource file for Win32 application. The First lets see what the output of this Form application looks like.
The Application does this layout of controls in the method public WinFileInfo ()
{
InitForm ();
}
Setting the this.Text = "File Information Application";
The client size of the form is controlled by the this.ClientSize = new Size(400, 280);
The controls in the Form application follow a hierarchical pattern i.e. each control
acts as a container for other controls. And for complex GUI design, controls
can be layered on top of each other and then pushed back or brought forward as
needed. In this particular application, Form acts as a container for a wndAttribBox.Controls.Add (wndArchiveCheck);
wndAttribBox.Controls.Add (wndReadOnlyCheck);
wndAttribBox.Controls.Add (wndHiddenCheck);
The other method of adding controls to the controls collection is by directly creating
an array of control objects and setting the this.Controls.All = new Control [] {
wndPanel,
wndAttribBox,
wndFileExistCheck,
wndLocationLabel,
wndLocation,
wndCreateTimeLabel,
wndLastAccessLabel,
wndLastWriteLabel,
wndCreateTime,
wndLastAccessTime,
wndLastWriteTime,
wndFindButton,
wndCloseButton
};
How do I create controls and set their properties?Every Control in the .NET Framework is an Object that implements methods, properties and events. An object is created using the new operator in C#. The same concept applies to controls too. I will describe the creation of a Button control, setting its properties like size and location and then event handlers for the click on the button.
First we need to define the control variable for the ButtonwndFindButton= new Button ();
ButtonwndCloseButton= new Button ();
CheckBoxwndFileExistCheck= new CheckBox ();
CheckBoxwndArchiveCheck= new CheckBox ();
CheckBoxwndReadOnlyCheck= new CheckBox ();
CheckBoxwndHiddenCheck= new CheckBox ();
The best thing about the .NET Framework is that we do not have to worry about releasing the variables allocated on the heap using the new operator. Garbage collection will do its magic (trust MS on this) when the variable is not in use anymore. The following code shows how the various properties for the Find button have been set. The name of the button's properties are very self-explanatory but I will try to describe them one by one. // Set some properties for Find Button.
wndFindButton.Text = "Find";
wndFindButton.TabIndex = 0;
wndFindButton.Anchor = AnchorStyles.BottomRight;
wndFindButton.Size = new Size (72, 24);
wndFindButton.Location = new Point (110, 250);
wndFindButton.Click += new EventHandler (this.buttonFind_click);
The
The
The
The
The
The most important setting is how to handle the event when the user clicks on this control. This is
done through adding an event handler to the
This way we can create controls dynamically and set their properties and event handling methods.
After creating all the controls do not forget to add them to the parent
container like How do I tweak some properties of the Form?
Like every Win32 control, we can customize the appearance and actions of all the
controls. For example, what button should handle the message if the user clicks
ENTER? Setting the this.AcceptButton= wndFindButton;
The following code from the // We don't need a maximize box for this form.
this.MaximizeBox = false;
// Set the Find button to ACCEPT button for this form.
this.AcceptButton= wndFindButton;
// Set the close button to Cancel operation button.
this.CancelButton = wndCloseButton;
// Set the start position of the form to be center of screen.
this.StartPosition = FormStartPosition.CenterScreen;
//And then activate the Form object.
this.Activated += new EventHandler (this.WinFileInfo_activate);
Can I use Win32 API functions in C#?
Yes, you can do it. For this you need to import Win32 DLLs that implement the
functions you need. E.g. for //-------------------------------------------------
// We need to import the Win32 API calls used to deal with
// image loading.
//-------------------------------------------------
[DllImport("user32.dll")]
public static extern int LoadIcon(int hinst, String name);
[DllImport("user32.dll")]
public static extern int DestroyIcon(int hIcon);
[DllImport("user32.dll")]
public static extern int LoadImage(int hinst, String lpszName,uint uType,
int cxDesired,int cyDesired, uint fuLoad);
[DllImport("gdi32.dll")]
public static extern int DeleteObject(int hObject);
How do I perform File IO operations?This application shows the attribute for files. This means we need to do some File IO operations. The .NET Framework provides a File Object for IO operations. We can create a File object by providing a file name as the parameter to constructor. File myFile = new File(wndFileName.Text);
Then we can make use of the FileSystemAttributes attribs = myFile.Attributes;
After that, we can do logical operations on the Attributes value to check if a particular
file attribute is set or not. For example, to see if a file is of if ((attribs & FileSystemAttributes.Archive) == FileSystemAttributes.Archive)
{
wndArchiveCheck.Checked = true;
}
One thing that needs to be noticed is that the result of a logical ‘
To get file's creation date, use the DateTime timeFile = myFile.CreationTime;
wndCreateTime.Text = timeFile.Format ("F", DateTimeFormatInfo.InvariantInfo);
Can I include my own resources in the application?
Yes, you can. Use Visual Studio to generate a resource script file ( Can I associate a specific icon to the application?
Yes, it too can be done. There are two ways to accomplish this. One is to use the How do I compile the application?
I have used a !include master.mak
_IMPORTS=$(_IMPORTS) /r:System.WinForms.DLL/r:System.DLL
/r:Microsoft.Win32.Interop.DLL
/r:System.Drawing.DLL /r:System.Net.DLL
_WIN32RES=$(_WIN32RES) /win32res:WinFileInfo.res
#_WIN32ICON=$(_WIN32ICON) /win32icon:FormIcon.ico
_XMLDOC=$(_XMLDOC) /doc:WinFileInfo.xml
all: WinFileInfo.exe
WinFileInfo.exe: WinFileInfo.cs
You must be wondering what that
///<summary>
///ButtonClose_Click Method:
///<para>
///This method is invoked when Close button on the dialog box is clicked.
///This method closes the application.
///</para>
///</summary>
Last ThoughtThis is just a very simple Windows Form application. This should give you a starting point to build upon. I am not an expert on C# or Forms application. I am learning too so I thought I will share the experience with you guys. Please do send your comments and suggestions to me. I will try to improve this application and add some more advanced features. Updating to the final release of .NETThe latest version is now RTM compliant. Here is a brief discussion of the changes. Some changes in referencing system assemblies during compilation
The application needs to refer to some system assemblies like
If you want compiler to ignore the inclusion of default Other changesThere are namespace changes, attribute name and value changes and some method name changes in the new code from the previous version that was written for Beta1. Other than that I did not have to make a whole lot of changes. What Next?Next we will try to write an article that will show how an ASP.Net application can be written without the help of IDE. and it will show how ASP.Net references the assemblies at compile time and run time.
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||