Click here to Skip to main content
15,867,834 members
Articles / Programming Languages / C#

Creating Your First C# Windows Application

Rate me:
Please Sign up or sign in to vote.
4.31/5 (33 votes)
27 Feb 2002CPOL3 min read 412.6K   4.4K   19   29
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.

Image 1

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.

Image 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:

C#
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:

C#
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:

 

C#
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:

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

Image 3

Figure 3 Final Visual C# Application.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
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

 
GeneralRe: Come on man! Pin
6-Mar-02 3:45
suss6-Mar-02 3:45 
Generalback off Pin
Chris Losinger3-Mar-02 19:00
professionalChris Losinger3-Mar-02 19:00 
GeneralRe: back off Pin
6-Mar-02 9:01
suss6-Mar-02 9:01 
GeneralRe: back off Pin
TeamWild6-Jan-04 23:46
TeamWild6-Jan-04 23:46 

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

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