Click here to Skip to main content
Click here to Skip to main content

Creating Your First C# Windows Application

, 27 Feb 2002
Rate this:
Please Sign up or sign in to vote.
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();
}

Other C# Sites

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

Share

About the Author

Nick Parker
Software Developer (Senior)
United States United States
Nick graduated from Iowa State University with a B.S. in Management Information System and a minor in Computer Science. Nick works for Zetetic.
 
Nick has also been involved with the Iowa .NET User Group since it's inception, in particular giving presentations over various .NET topics. Nick was awarded the Visual C# MVP award from Microsoft for four years in a row.
 
In his mystical spare time he is working on a development project called "DeveloperNotes" which integrates into Visual Studio .NET allowing developers easy access to common code pieces. He is also a fan of using dynamically typed languages to perform unit testing, not to mention how he loves to talk about himself in the third person.

Comments and Discussions

 
Generalupdate Pinmemberdominic kMalisa22-Oct-10 3:23 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.1411028.1 | Last Updated 28 Feb 2002
Article Copyright 2002 by Nick Parker
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid