DotNet Tutorials

V4 Dot Net Tutorials

Server Intellect Cloud Hosting

 Using Classes in WinForms using VS.NET and C#

This tutorial shows you how to use a class so that multiple forms can use your methods. C# version.

Using classes in your Windows Applications not only makes it easier to organize our code, but it also makes it easier to maintain our code. With classes, we can store our methods in one place and use them many times on several Windows Forms (or web pages). Furthermore, when it comes to editing our methods, we only need to edit them in one place - the class.

In this tutorial, we will look at how we can create a class, how we can create methods within that class and also how we can make use of the class in a Windows Form by calling its methods.

The first thing we will do is to create a new project in Visual Studio. This example was created with VS .NET 2008, but the principles apply to 2005 as well. Once we have our project open, we will right-click it in Solution Explorer and choose Add > Class (Or Add New Item, Class). You can name it whatever you want, in this example, we left the default - Class1.

We should then be provided with the default code of the class, which should look something like this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace UsingClasses_cs
{
public class Class1
{
}
}

I just signed up at Server Intellect and couldn't be more pleased with my Windows Server! Check it out and see for yourself.

Notice that the namespace is the Project Name, and within that is the class. Now if we goto the code of our Form1, by right-clicking it and choosing View Code, we will see the following:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace UsingClasses_cs
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
}
}

Notice that the Form1 is also wrapped in the namespace. We are going to create three methods in the class that this form will use, so we will add the following controls to our form: Simple Addition - two labels, two textboxes and a button; Fahrenheit to Celsius - one textbox, one button and one label; Celsius to Fahrenheit - one textbox, one button and one label. The labels will be used to display the results of the methods; you can add more labels to give titles if you wish.

Once we have added these controls to our form, we can begin coding. Let's go back to our class and create our methods. They are very simple methods to show how classes work:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace UsingClasses_cs
{
public class Class1
{
public static int AddNumbers(int x, int y)
{
return x + y;
}

public static int ToCelsius(int f)
{
return (f - 30) / 2;
}

public static int ToFahrenheit(int c)
{
return (c * 2) + 30;
}
}
}

In these methods, we are performing simple calculations to provide functionality to the user, which we are doing with a class.
These methods reside in the Class1 we created, which is in a separate file to our Form1 code. To utilize these methods, we will need to reference the Class name in our Form1 code, like so:

Server Intellect offers Windows Hosting Dedicated Servers at affordable prices. I'm very pleased!

private void button1_Click(object sender, EventArgs e)
{
label1.Text = textBox1.Text + " + " + textBox2.Text + " = " +
Class1.AddNumbers(Convert.ToInt16(textBox1.Text),Convert.ToInt16(textBox2.Text)).ToString();
}

Notice that we are simply referencing the Class name first, and then the method. We can do the same for the other methods, and also add a try and catch to each of them to catch any errors:

private void button1_Click(object sender, EventArgs e)
{
try
{
label1.Text = textBox1.Text + " + " + textBox2.Text + " = " +
Class1.AddNumbers(Convert.ToInt16(textBox1.Text),Convert.ToInt16(textBox2.Text)).ToString();
}
catch
{
label1.Text = "Unable to perform calculation, please try again.";
}
}

private void button2_Click(object sender, EventArgs e)
{
try
{
label6.Text = Class1.ToCelsius(Convert.ToInt16(textBox3.Text)) + "°C";
}
catch
{
label6.Text = "Unable to perform calculation,\n please try again.";
}
}

private void button3_Click(object sender, EventArgs e)
{
try
{
label7.Text = Class1.ToFahrenheit(Convert.ToInt16(textBox4.Text)) + "°F";
}
catch
{
label7.Text = "Unable to perform calculation,\n please try again.";
}
}

Yes, it is possible to find a good web host. Sometimes it takes a while. After trying several, we went with Server Intellect and have been very happy. They are the most professional, customer service friendly and technically knowledgeable host we've found so far.

The entire code-behind of Form1 will look something like this:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace UsingClasses_cs
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{

}

private void button1_Click(object sender, EventArgs e)
{
try
{
label1.Text = textBox1.Text + " + " + textBox2.Text + " = " +
Class1.AddNumbers(Convert.ToInt16(textBox1.Text),Convert.ToInt16(textBox2.Text)).ToString();
}
catch
{
label1.Text = "Unable to perform calculation, please try again.";
}
}

private void button2_Click(object sender, EventArgs e)
{
try
{
label6.Text = Class1.ToCelsius(Convert.ToInt16(textBox3.Text)) + "°C";
}
catch
{
label6.Text = "Unable to perform calculation,\n please try again.";
}
}

private void button3_Click(object sender, EventArgs e)
{
try
{
label7.Text = Class1.ToFahrenheit(Convert.ToInt16(textBox4.Text)) + "°F";
}
catch
{
label7.Text = "Unable to perform calculation,\n please try again.";
}
}
}
}

Looking for the VB.NET 2005 Version? Click Here!

Looking for more .NET Tutorials? Click Here!

Download Project Source - Enter your Email to be emailed a link to download the Full Source Project used in this Tutorial!



100% SPAM FREE! We will never sell or rent your email address!