How to Customize the Look and Feel of a C# Form

Personalising a Windows Forms with C#

© Mark Alexander Bain

Apr 7, 2009
Customize a Windows Form with C#, Mark Alexander Bain
A C# Windows form is always plain and grey. However, a C# programmer can customize the form to give it exactly the look and feel that they require.

Any C# programmer who has developed a Windows application will have produced something than looks very much like every other Windows application. It will have a grey background, grey buttons and text boxes and the border will be resizable.

That is all quite acceptable, but the developer may decide that they require a more distinctive look to their application. If that’s the case then they will be pleased to know that there are many aspects of the form that may be customized. For example they may wish to:

  • Use their own icon for the form
  • Define the size and location of the form
  • Change the border style
  • Change the font type and color being used
  • Change the background color of the form and any components (such as the text boxes)

Fortunately C# makes this all very easy for the programmer and so, of course, the first step is to create the form itself.

Creating the C# Form

The first step is for the programmer to create the form as normal, starting by including any required libraries:

using System;
using System.Windows.Forms;
using System.Drawing;

The application then needs a form class with variables for any of its components (in this case just a single label):

public class MainForm : Form {
private Label lblText;

Next the main subroutine is required as well as the class constructor:

public static void Main() {
Application.Run(new MainForm());
}
public MainForm()
{
formatForm();
}

Obviously the next stage is to carry out the customization of the form’s look and feel.

Formatting the C# Form

The C# form is accessed from the code by using the “this” object and it is formatting by altering the values of some of its properties. Any components in the form are changed in exactly the same way.

Changing the C# Form Icon

The programmer can change the form’s icon by making use of the icon property:

public void formatForm() {
this.Text = "Format the Form";
this.Icon = new Icon("c:\\icons\\c_sharp.ico");

This example also changes the title of the form using its text property.

Changing the Size and Location of the C# Form

Having seen how to set the icon it not surprising to find that updating the height and width properties alters the size of the form:

this.Width = 200;
this.Height = 300;

However, the form size can also be made dynamic and dependant on the size of the screen:

this.Width = Screen.GetWorkingArea(this).Width/2;
this.Height = Screen.GetWorkingArea(this).Height/2;

And the same can be done for the location of the form on the screen:

int x = Screen.GetWorkingArea(this).Width*25/100;
int y = Screen.GetWorkingArea(this).Height*25/100;
this.DesktopLocation = new Point(x,y);

Then programmer can prevent the user from altering the size of the form:

this.FormBorderStyle = FormBorderStyle.FixedSingle;
this.MaximizeBox = false;

Here the code has fixed the size of the form and turned off the maximize box.

Working with C# Form Fonts and Colors

Very dramatic effects can be achieved by setting the form fonts and colors, for example:

this.Font = new Font (new FontFamily("Verdana"),10);
this.ForeColor = Color.Yellow;
this.BackColor = Color.Blue;

And the same can be done with the form components:

this.lblText = new Label();
lblText.Location = new Point(0,0);
lblText.Text = "Hello World!";
this.Controls.Add(lblText);
lblText.ForeColor = Color.White;
lblText.BackColor = Color.Black;

Another interesting effect is the ability to make a form (or portions of a form) transparent:

this.TransparencyKey = Color.Blue;

Here the form will be transparent instead of blue.

Summary

The C# programmer is not limited to producing standard Windows forms. Instead they can modify the form’s properties so that they can change:

  • The title and icon
  • The width, height and location
  • Font color and family as well as the form’s background color

Producing a form with a look and feel all of its own.


The copyright of the article How to Customize the Look and Feel of a C# Form in C Programming is owned by Mark Alexander Bain. Permission to republish How to Customize the Look and Feel of a C# Form in print or online must be granted by the author in writing.


A Default C# Form, Mark Alexander Bain
The Customized C# Form, Mark Alexander Bain
Customize a Windows Form with C#, Mark Alexander Bain
   


Post this Article to facebook Add this Article to del.icio.us! Digg this Article furl this Article Add this Article to Reddit Add this Article to Technorati Add this Article to Newsvine Add this Article to Windows Live Add this Article to Yahoo Add this Article to StumbleUpon Add this Article to BlinkLists Add this Article to Spurl Add this Article to Google Add this Article to Ask Add this Article to Squidoo