This sample is directed towards describing simple steps that a beginner can follow
to add a menu to a Forms application. I will also show how you can write a
component that implements an AboutBox
to be included in any Forms application.
When you look at the implementation you will realize how trivial it is to
create a component that can be used by other applications written in any
language. We do not have to write any IDL
files, and no GUIDs. Nothing.
Everything is being taken care of by the Common Runtime Engine, since AboutBox
is
nothing more than a dialog box that shows some information about an application. In
Windows Forms application, a dialog box is nothing but a Form with its style
defined as FixedDialog
.
this.BorderStyle = FormBorderStyle.FixedDialog;
Therefore to inherit all the functionality of a Form, we define our class to be inherited from
the Form class, which is defined in System.WinForms
.
public class AboutBox : Form {
First, let us see how we start. In the .NET world everything is based on namespaces.
Therefore, we need to define a namespace for our component. I have defined the namespace to be NKAboutBox
.
One thing that you will have to make sure is that source file name for this component
should match the namespace name. So abiding by this restriction, the file is saved as NKAboutBox.cs
.
Why does the filename need the extension cs
?
It has to be cs
because we are using the C#
language for building our
component.
namespace NKAboutBox {
public class AboutBox : Form {
Like in every COM component, we can add properties and methods to the class. In C# all the
public member variables get treated as properties of the component. And all the
public functions get treated as methods of the component. A very simple
AboutBox
will display the Author Name, Company Name, Product Name and Version. We can define
these as the component's properties that a user can get and set directly.
public String Author;
public String AppName;
public String Version;
To display the Form, I have added a method ShowAboutBox
to the component.
The client of this component will call this method after s/he has set the
properties that need to be displayed in the dialog box.
public void ShowAboutBox ()
{
InitDialog ();
}
InitDialog
is a private function in the component class.
This function lays out the Form and its control. Since I am not using Visual Studio
for resources, I have used the hard way of using the .NET SDK to accomplish this task.
For more information on laying out the controls on a Form, please refer to my other
article “Writing Windows Form Application Using C#”.
To close the dialog box, I have added a button to the Form. To handle the click
event, I have added the About_OK
event handler to class.
private void About_OK (Object source, EventArgs e)
{
Control wndCtrl = ((Button)source).Parent;
((Form)wndCtrl).Close ();
}
You can see from the implementation of this eventhandler
how the source argument
is casted down to the Form class. And then the Close method is called
on the object to close the Form. The next step is to compile this component and generate the DLL that can
be used other clients. You can type the following instruction on the command prompt to compile
NKAboutBox.cs
file to NKAboutBox.dll.
csc /out:NKAboutBox.dll /target:library /r:System.WinForms.DLL /r:System.DLL
/r:Microsoft.Win32.Interop.DLL /r:System.Drawing.DLL /r:System.Net.DLL
NKAboutBox.cs
Clients that use this component have to follow a couple very simple steps. First, the namespace of this
component needs to be imported into the client code. And then during compilation of
the client code this component DLL needs to be referred to using the /r
option.
using NKAboutBox;
private void MenuAbout_Click (Object source, EventArgs e)
{
AboutBox myBox = new AboutBox ();
myBox.Author = "Naveen K Kohli";
myBox.AppName = "Windows File Attributes";
myBox.Version = "1";
myBox.ShowAboutBox ();
}
You will need to have the .NET SDK installed on your machine to compile and run the sample project.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.