I.T. Discussion Community!
-Collapse +Expand
C#
Search C# Group:

Advanced
-Collapse +Expand C# To/From
To/FromCODEGuides
-Collapse +Expand C# Study Test
PRESTWOODCERTIFIED
-Collapse +Expand C# Store
PRESTWOODSTORE

Prestwood eMagazine

March Edition
Subscribe now! It's Free!
Enter your email:

   ► KBProgrammingC#WinForms   Print This     
  From the March 2009 Issue of Prestwood eMag
 
A 10 Minute C# Winforms Quick Start
 
Posted 11 years ago on 6/19/2008
Take Away:

The ButtonsCS project. Create a classic "Hello, World" application using Visual Studio .Net with C# syntax. Requires either the full version or Visual C# Express Edition.

KB101212



Got 10 minutes? Want to get started with C# Winforms?

This article is part of our series of 10 Minute Quick Starts. Each quick start is step by step, assumes you know very little about the subject, and takes about 10 minutes. You can use them to scratch the service of areas you want to learn and as a quick review when returning to something after a long absence.

C# Winforms

In this tutorial, you will create a classic "Hello, World" windows application called ButtonsCS. Specifically, a form with 3 buttons on it. Each button will display a "Hello, World" dialog but each will demonstrate a different introductory development concept. The goal is to get your feet wet with C# syntax and introduce the Visual Studio development environment. When completed,  you can use this ButtonsCS project to test and learn the syntax of C# by adding your own buttons to it.

Setup

To get started, you have to have Visual Studio.Net installed. This article is based on VS.Net 2008 but you can use any version you wish (there may be slight variations in menu options but we try to keep our 10 Minute Quick Starts as generic as possible).
 

And here, we, go...

Part One: ButtonsCS

  1. Start a new project. Open Visual Studio and click File | New Project. Select the Visual C# > Windows > Windows Forms Application template. In the Name field type ButtonsCS for the project name. Later, we can rename a project but it's easiest to name it now. Then click OK.
     
    Note - Solutions and projects. All your coding is organized into solutions and projects. A project can consist of a single application (an EXE), website, DLL, class, etc. A solution just brings together a collecton of projects. In this case, we have a project called ButtonsCS in a solution of the same name. 
       

     

     
  2. Pin the Toolbox. If the Toolbox is not pinned, select it and pin it. If the Toolbox is not pinned, click on the Toolbox tab to display the Toolbox and then click the Tack in the upper right.
     

     
  3. Place 3 buttons on the form. Click the Button control then click anywhere on your form (do this two more times). You can also double clock the Button control in the Toolbox to place it on a form. You can quickly align the buttons by clicking and dragging the buttons. You'll see guidelines similar to the blue guidelines in the following screen clip.
     

     
  4. Add code to button 1. Double click Button 1 and add the following code:

    MessageBox.Show("Hello, World!");


    Your code window should now look similar to the following (I added a few comments to explain the code a bit).
    //Namespace aliases.
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
     
    namespace ButtonsCS //Our project's namespace.
    {
      //Our form class.
      public partial class Form1 : Form
      {
        public Form1()
        {
          InitializeComponent();
        }

        //Code for our button1 button.
        private void button1_Click(object sender, EventArgs e)
        {
          MessageBox.Show("Hello, World!");
        }
      }
    }

  5. Switching between Code and the Form. The tab with [Design] is the form. In our example, there may be other tabs but the Form1.cs tab is the code tab and the Form1.cs [Design] is the form.
     

     
  6. Compile your code. You are now ready to compile your code into a Windows application. Click Build | Build ButtonsCS. If you have any syntax errors or compiler warnings, this is when Visual Studio will let you know and you can fix them.
  7. Run-it! Now let's test your very first program. You can run it within Visual Studio (called debugging) or you can run the EXE outside of Visual Studio. To runt it you start a debugging session. To start a debugging session, either click the toolbar debug icon, select Debug | Start Debugging, or press F5. Click Button 1 to show the Hello, World dialog. Click OK then close the form to abort the debugging session.
     

     
     
    You can also run the application outside of Visual Studio. The compiled EXE is located in your debug folder. Here is the location of mine:
     
    D:\MP\Documents\Visual Studio 2008\Projects\ButtonsCS\ButtonsCS\bin\Debug\ButtonsCS.exe
     
    You can even distribute your EXE to any Windows computer with the appropriate DotNet Framework installed. The DotNet Framework is required because a DotNet application is not a true native code application like the ones created with tools such as C++ and Delphi.
     
  8. Add code to Button 2. So far we've been using literals for the text in our dialog box. This time let's declare two variables, concatenate them together and use the variable with the MessageBox member. For good measure, we'll make use of both a single line comment and a multi-line comment.
     
    private void button2_Click(object sender, EventArgs e)
    {
    string FirstName;
    string LastName;
    string FullName;

    //Assign variables.
    
    
    FirstName = "Felicia";
    LastName = "Prestwood";
    FullName = FirstName + " " + LastName;

    /*
    * Display 
    * dialog.
    */
    
    
    MessageBox.Show("My daughter's name is " + FullName);
    }


  9. Add code to Button 3. The final example shows that C# has features from C++ like incrementing unary operators. The "++" increments the Age variable.
    private void button3_Click(object sender, EventArgs e)
    {
    int Age = 17;
    MessageBox.Show("Felicia is " + Age);
    Age = ++Age;
    MessageBox.Show("Next year she will be " + Age);
    }
  10. Test-It!

The Program.cs File

The Program.cs file contains the Main method which is the entry point for the executable. All .NET projects except for DLLs needs one. Here is the Program.cs file for a typical CSharp program.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace ButtonsCS
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }
}

What now?

Now that you've gotten your feet wet with C#, you can start learning how to do specific coding tasks, learn the intricies of the language, and the ins and outs of the development environment. A good next step is to browse the varioius menu options and see what they do. Select various menu options, when in the feature, press F1 for a quick overview.
 

That's it! I hope this was helpful.

 


Comments

0 Comments.
Share a thought or comment...
 
Write a Comment...
...
Sign in...

If you are a member, Sign In. Or, you can Create a Free account now.


Anonymous Post (text-only, no HTML):

Enter your name and security key.

Your Name:
Security key = P1204A1
Enter key:
Article Contributed By Mike Prestwood:

Mike Prestwood is a drummer, an author, and creator of the PrestwoodBoards online community. He is the President & CEO of Prestwood IT Solutions. Prestwood IT provides Coding, Website, and Computer Tech services. Mike has authored 6 computer books and over 1,200 articles. As a drummer, he maintains play-drums.com and has authored 3 drum books. If you have a project you wish to discuss with Mike, you can send him a private message through his PrestwoodBoards home page or call him 9AM to 4PM PST at 916-726-5675 x205.

Visit Profile

 KB Article #101212 Counter
17653
Since 6/19/2008
-
   Contact Us!
 
PrestwoodBoards.com was developed and is maintainted by me. Do you have a question or suggestion? Do you see a problem? Contact me now. My goal is to build an ad-free and spam-free source of I.T. information with many contributers (ok to promote your website/company in your bio). Yes, my company Prestwood IT Solutions is mentioned in my bio which shows with every post, but you can contribute and promote your pet project too!

2,310 People Online Now!!  
Sign In to see who's online now!  Not a member? Join now. It's free!
Show more stats...


©1995-2019 PrestwoodBoards  [Security & Privacy]
Professional IT Services: Coding | Websites | Computer Tech