How to Create Windows Forms in C#

Abstract

Learn how to create a basic Windows Forms application using C#.

Most published C# books and tutorials illustrate C# programming concepts with console applications. However, console applications have very limited use in writing practical Windows applications. It is more fun to learn C# with Windows Forms or Windows Presentation Foundation (WPF). I have chosen to use Windows Forms for these tutorials because the development process with WPF is considerably more complex - it involves other .NET technologies not directly related to C# programming (for example, XAML).

I will first demonstrate how to build a very basic Windows Forms application. Then, in Advanced Introduction to C# with Windows Forms - Part 2, I will show you how to build a Windows Forms application that illustrates rudimentary string operations.

Prerequisites

This advanced tutorial is not intended as an introductory programming tutorial. There are many good C# books and on-line resources that you can read in parallel with this tutorial. In the reference section you will find several most popular C# tutorials and free C# books, as well as the development software you will need to work on these projects.

Creating a Basic Windows Forms Application

  1. To create a Windows Forms Application project, on the menu bar, select File, New, Project.

    New Project
  2. Select Visual C# in the Installed Templates list. In the templates list, choose the Windows Forms Application icon. Name the new project BasicApp, and then click the OK button. Visual Studio creates a solution for your project. The following screenshot shows what you should see:

    New App
  3. Change the title of the Windows Form by clicking on the form to select it. In the Properties window, select the text "Form1," and type Basic App, press Enter. You observe that the text at the top of your Windows Form has changed to Basic App.

    Properties
  4. Add a button. From the Toolbox, in the Common Controls category, drag a Button control to the Windows Form. Change the Text property to GO instead of button1.
  5. Add a label. From the Toolbox, in the Common Controls category, drag a Label control to the Windows Form. Change the Text property to What is your name? instead of label1.
  6. Add a text box. From the Toolbox, in the Common Controls category, drag a TextBox control to the Windows Form.
  7. Add an event handler for the Button control. Double-click the button, and you will observe the code for your project:

    Blank Code
  8. This is the event handler for the button click event when the user clicks the button:

     private void button1_Click(object sender, EventArgs e) { } 
  9. Add the following code to the event handler method:

    private void button1_Click(object sender, EventArgs e) 
    {
    label1.Text = "I see, your name is " + textBox1.Text + ".";
    }
  10. Build and run the program. Press F5 (or Control-F5) to build and run the program. The Windows Form is displayed on the screen. Click the GO button. It then displays the following:

    Run App
  11. You can download, build and run this application from: Source code

References

Development Tools

On your development system, you need to install Visual Studio Express 2013 for Windows Desktop from this link: http://www.visualstudio.com/en-US/products/visual-studio-express-vs If you have access to any other Visual Studio edition, such as Visual Studio Professional, you can certainly use it as well.

Popular Tutorials

Free C# Books

Download



Comments

  • There are no comments yet. Be the first to comment!

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Employees must exchange sensitive emails with customers and partners. These emails might contain protected health information, protected financial information, or corporate information that should not be made public. Globalscape® Mail Express® allows you to encrypt the emails that it manages so that no one but the sender and recipient--not even the administrator--can view the contents of the email. "Secure the Transfer of Sensitive Emails" is the property of GlobalSCAPE

  • Live Event Date: May 11, 2015 @ 1:00 p.m. ET / 10:00 a.m. PT One of the languages that have always been supported with the Intel® RealSense™ SDK (Software Developer Kit) is JavaScript, specifically so that web-enabled apps could be created. Come hear from Intel Expert Bob Duffy as he reviews his own little "space shooting" game where the orientation of your face controls the aiming reticle to help teach developers how to write apps and games in JavaScript that can use facial and gesture …

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date