Click here to Skip to main content
Click here to Skip to main content

Writing a Customizable AboutBox Component

, 28 Nov 2000
Rate this:
Please Sign up or sign in to vote.
Shows how to write a component using C#.
<!-- Link to source file download --> <!-- Add the rest of your HTML here -->

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)
{
   // 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

About the Author

Naveen K Kohli

United States United States
No Biography provided

Comments and Discussions

 
GeneralMy vote of 1 PinmemberAli BaderEddin17-Oct-11 12:39 
GeneralMy vote of 1 Pinmemberjeevanagarwal22-Sep-10 23:20 
GeneralClose PinmemberSystem.Object13-Sep-06 3:10 
GeneralDemo and source files not accessible PinmemberNicolas Loiselet29-Nov-00 22:55 
GeneralRe: Demo and source files not accessible PinmemberAnonymous30-Nov-00 4:27 
GeneralRe: Demo and source files not accessible Pinmemberanonymous4-Dec-00 23:34 

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

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

| Advertise | Privacy | Mobile
Web04 | 2.8.140721.1 | Last Updated 29 Nov 2000
Article Copyright 2000 by Naveen K Kohli
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid