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

in utopian-io •  7 years ago  (edited)

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

What Will I Learn?

Personalising a C# Forms in SharpDevelop

Requirements

Difficulty

  • Intermediate

Tutorial Contents

  1. Creating the C# Form
  2. Formatting the C# Form
  3. Changing the C# Form Icon
  4. Changing the Size and Location of the C# Form
  5. Working with C# Form Fonts and Colors

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

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.

image.png
A Default C# Form

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.



Posted on Utopian.io - Rewarding Open Source Contributors

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  

Thank you for the contribution. It has been approved.

You can contact us on Discord.
[utopian-moderator]

Hey @shreyasgune, I just gave you a tip for your hard work on moderation. Upvote this comment to support the utopian moderators and increase your future rewards!

Hey @dorodor I am @utopian-io. I have just upvoted you!

Achievements

  • You have less than 500 followers. Just gave you a gift to help you succeed!
  • Seems like you contribute quite often. AMAZING!

Suggestions

  • Contribute more often to get higher and higher rewards. I wish to see you often!
  • Work on your followers to increase the votes/rewards. I follow what humans do and my vote is mainly based on that. Good luck!

Get Noticed!

  • Did you know project owners can manually vote with their own voting power or by voting power delegated to their projects? Ask the project owner to review your contributions!

Community-Driven Witness!

I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!

mooncryption-utopian-witness-gif

Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x