Click here to Skip to main content
15,880,972 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.9K   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

 
QuestionDeveloping a basic windows application using C#.NET Pin
Member 1006923621-May-13 23:18
Member 1006923621-May-13 23:18 
Questiondatabase in c# Pin
fatimahero12-Feb-12 6:59
fatimahero12-Feb-12 6:59 
AnswerRe: database in c# Pin
jitendra y17-May-15 18:43
jitendra y17-May-15 18:43 
Generalupdate Pin
dominic kMalisa22-Oct-10 2:23
dominic kMalisa22-Oct-10 2:23 
GeneralMy vote of 4 Pin
ritu_99914-Oct-10 0:05
ritu_99914-Oct-10 0:05 
GeneralMy vote of 2 Pin
naageswar1-Oct-10 3:24
naageswar1-Oct-10 3:24 
QuestionMULTIPLY FUNCTION IN DATA GRID VIEW, NEED HELP Pin
amirjaved2-Jun-10 21:30
amirjaved2-Jun-10 21:30 
GeneralChecklistBox help.... Pin
MahenB4-Dec-09 21:13
MahenB4-Dec-09 21:13 
GeneralArray in textbox Pin
Desi Bravo14-Nov-09 14:00
Desi Bravo14-Nov-09 14:00 
GeneralAbout my Combobox Items... Pin
Valercik20-Oct-08 2:56
Valercik20-Oct-08 2:56 
GeneralItems don't display in drop down Pin
booby12-Oct-04 8:54
booby12-Oct-04 8:54 
GeneralI'm a newbe, and this is what i need Pin
Stokholm27-Aug-04 5:04
Stokholm27-Aug-04 5:04 
Thank you for the example.
I'm new to C# Win-Apps, so please keep it simple.
I'm a wannabi Hardcore C#-programmer,
but that can wait till i have learned the basics


Jacob Stokholm
Copenhagen, Denmark
GeneralMustafa is a Tool Pin
Anonymous11-Jul-03 21:22
Anonymous11-Jul-03 21:22 
QuestionWhich would you use? Pin
Stan Shannon28-Feb-02 2:08
Stan Shannon28-Feb-02 2:08 
AnswerRe: Which would you use? Pin
Nick Parker28-Feb-02 5:02
protectorNick Parker28-Feb-02 5:02 
AnswerRe: Which would you use? Pin
Mario M.28-Feb-02 7:26
Mario M.28-Feb-02 7:26 
AnswerRe: Which would you use? Pin
2-Jul-02 10:56
suss2-Jul-02 10:56 
GeneralRe: Which would you use? Pin
tom_dx3-Nov-04 12:34
tom_dx3-Nov-04 12:34 
GeneralRe: Which would you use? Pin
tom_dx3-Nov-04 12:34
tom_dx3-Nov-04 12:34 
GeneralRe: Which would you use? Pin
Nick Parker3-Nov-04 18:19
protectorNick Parker3-Nov-04 18:19 
GeneralCome on man! Pin
Mustafa Demirhan27-Feb-02 20:42
Mustafa Demirhan27-Feb-02 20:42 
GeneralRe: Come on man! Pin
Nick Parker28-Feb-02 1:10
protectorNick Parker28-Feb-02 1:10 
GeneralRe: Come on man! Pin
Mustafa Demirhan28-Feb-02 9:46
Mustafa Demirhan28-Feb-02 9:46 
GeneralRe: Come on man! Pin
Nick Parker28-Feb-02 11:03
protectorNick Parker28-Feb-02 11:03 
GeneralRe: Come on man! Pin
BLaZiNiX1-Mar-02 12:15
BLaZiNiX1-Mar-02 12:15 

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.