Click here to Skip to main content
15,884,177 members
Articles / Programming Languages / C#
Article

Writing a Customizable AboutBox Component

Rate me:
Please Sign up or sign in to vote.
1.38/5 (10 votes)
28 Nov 2000 118.1K   448   30   6
Shows how to write a component using C#.

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.

C#
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.

C#
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.

C#
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.

C#
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.

C#
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.

C#
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.

C#
using NKAboutBox;

private void MenuAbout_Click (Object source, EventArgs e)
{
   // Create the AboutBox defines in NKAboutBox namespace.
   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.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 1 Pin
Ali BaderEddin17-Oct-11 12:39
Ali BaderEddin17-Oct-11 12:39 
GeneralMy vote of 1 Pin
Jeevan Agarwal22-Sep-10 23:20
Jeevan Agarwal22-Sep-10 23:20 
GeneralClose Pin
System.Object13-Sep-06 3:10
System.Object13-Sep-06 3:10 
GeneralDemo and source files not accessible Pin
29-Nov-00 22:55
suss29-Nov-00 22:55 
GeneralRe: Demo and source files not accessible Pin
30-Nov-00 4:27
suss30-Nov-00 4:27 
Hi Nicolas,
It maybe due to the fact that this article is still in unedited area. I will send email to managers of this site. In the mean time, i will send you the source files directly to your email account.
GeneralRe: Demo and source files not accessible Pin
4-Dec-00 23:34
suss4-Dec-00 23:34 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.