|
||||||
|
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:
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# FormThe 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# FormThe 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 IconThe 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# FormHaving 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 ColorsVery 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. SummaryThe C# programmer is not limited to producing standard Windows forms. Instead they can modify the form’s properties so that they can change:
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.
|
||||||
|
|
||||||
|
|
||||||