Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Creating Your First C# Windows Application

0.00/5 (No votes)
27 Feb 2002 1  
Learn how to create your first Visual C# Windows application.

Introduction

C# appears to be the new language of choice around the development community, or at least the most talked about new language. Microsoft has introduced a language that will (hopefully) bring together both Visual Basic developers and Visual C++ developers alike. It is important to remember that, this application is only to show you some of the most basic components in C#. Here we will design a form that will allow a user to populate a listbox with customer names they type above.

This article is based on the ideas that Dr.Asad Altimeemy posed in the article he wrote titled: A Beginners Guide To Dialog Based Applications written in Visual C++. This article covers essentially the same methods only converted to run in .NET and written in C#. This code is unfortunately based on Visual Studio .NET Beta 2; please make any comments regarding the changes to the final release version.

Creating A New Project

To create a new project you need to first load Visual Studio .NET and select under the Visual C# Projects, “Windows Application” as in Figure 1. Type the name of the project below along with selecting the desired location to store your files.

Figure 1. Creating a C# Project.

Designing The Interface

You will need to add the following items onto your form.

Item Quantity
GroupBox 1
Label 3
ComboBox 1
Textbox 2
Button 3
ListBox 1

Arrange your items onto the form so that they look something like this. I would suggest that you place the GroupBox on the form first, otherwise you will need to re-drag all item inside the box once they are on the form. Arrange the items as you see below in Figure 2.

Figure 2 Design Layouts For Application.

Labels, Labels, Labels…..

You will want to add the value ‘Title’ to the Text property of label1. The Text property for label2 should be set to ‘First Name’ and the Text property of label3 should be…….’Last Name’, big surprise huh? Text properties of button1 should be ‘OK’, button2 should be ‘Clear List’ and button3 will be ‘Close’. I have set the Text Property of groupBox1 to ‘Customers’ and Form1 to ‘Customers’ as well. You may also wish to set the Text value of the listBox and comboBox to blank also.

 

Adding The Code

To begin, when this form loads we need to populate the ComboBox with the appropriate values. Add the following code by clicking on the form on the outside of the groupBox. You should see something like this:

private void Form1_Load(object sender, System.EventArgs e)
 {
     //Add the following code
         //to fill the comboBox when 
     //form loads.

     comboBox1.Items.Add("Dr.");
     comboBox1.Items.Add("Mr.");
     comboBox1.Items.Add("Mrs.");
     comboBox1.Items.Add("Ms.");
     comboBox1.Focus();
}

We now need to handle what happens when the user hits the OK button after populating the text boxes. To do this simply click on the Form1.cs[Design]* tab at the top left to switch from code-view to the form object designer. Double-click on the OK button and add the following code:

private void button1_Click(object sender, System.EventArgs e)
{
    listBox1.Items.Add(comboBox1.Text + " " +
        textBox1.Text + " " + textBox2.Text);
    textBox1.Text = "";
    textBox2.Text = "";
    comboBox1.Text = "";
    comboBox1.Focus();
}

When we want to allow the user to clear all fields entered into the listBox, we will need to go back like we did above to the visual designer and double-click on the Clear List button, this should again switch to a code view and allow you to add the following code:

 

private void button2_Click(object sender, System.EventArgs e)
{
    listBox1.Items.Clear();
    comboBox1.Focus();
}

And finally we want to allow the user to be able to close the application when they want. To show you another way to allow the user to close the program aside from that catchy X in the upper right-hand corner, I have provided a button entitled……Close. I know, you were looking for some complex name, weren’t you? Anyway, before I get off on a tangent, double-click the Close button and add the following code below:

private void button3_Click(object sender, System.EventArgs e)
{
    this.Dispose();
}

Conclusion

Again this small sample application was made to show you how some of the basic components of a Windows application are controlled in C#. Hopefully, you have a better understanding of how the new C# language works within a Windows application. Here is a view of what you might see when running the application.

Figure 3 Final Visual C# Application.

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