IT SOLUTIONS
Your full service technology partner! 
-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

October 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 16 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

1 Comments.
Share a thought or comment...
First Comment
Comment 1 of 4

Everything is very open with a clear description of the issues. It was truly informative. Your website is very helpful. Many thanks for sharing!

Posted 18 months ago

Comment 2 of 4

Hy theer i can see your post and i must say 

  1. UI Components: WinForms provides a set of pre-built UI controls like buttons, labels, textboxes, checkboxes, and more. These controls can be easily dragged and dropped onto the application‘s design surface, making it user-friendly for developers.

  2. Event-Driven Programming: C# WinForms follows an event-driven programming model, where actions or events (e.g., button clicks, mouse movements) trigger specific functions or event handlers. This allows developers to respond to user interactions effectively.

  3. Properties and Events: Each UI control in WinForms has various properties that can be set to customize its appearance and behavior. Additionally, they expose events that can be handled to respond to user actions.

  4. Visual Designer: Visual Studio, the popular Integrated Development Environment (IDE) for C#, provides a visual designer that simplifies the process of designing WinForms applications. Developers can use the designer to visually create the layout of the application and set properties for UI controls.

Thanks and regards

Dessie D. Thomson

Posted 15 months ago

Comment 3 of 4

Hy there i must say i have some suggestion and might helps for you

  1. GUI Design: WinForms allows you to design the user interface visually using drag-and-drop controls from the Toolbox onto the Form (window). You can also arrange and customize the controls using the Properties window.

  2. Event-Driven Programming: WinForms follows an event-driven programming model. You write code to respond to events generated by the user‘s interaction with the application, such as button clicks, mouse movements, and keyboard input.

  3. Controls: WinForms provides a wide range of controls such as buttons, labels, text boxes, combo boxes, list boxes, data grids, and more. You can customize the appearance and behavior of these controls to suit your application‘s needs.

  4. Layout Management: WinForms includes layout managers like FlowLayoutPanel, TableLayoutPanel, and Anchor/Dock properties to help you organize and resize controls dynamically as the window size changes.

  5. Data Binding: You can easily bind controls to data sources like databases or collections, allowing for automatic updating of UI elements when data changes. TellHappyStar Survey

Thanks and regards

Nakita

Posted 15 months ago

Latest Comment
Comment 4 of 4

Use your breaks for something that energizes you, like getting a snack or going for a short walk.

mapquest directions

Posted 14 months ago
 
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 = P1116A1
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
19587
Since 6/19/2008
Go ahead!   Use Us! Call: 916-726-5675  Or visit our new sales site: 
www.prestwood.com


©1995-2024 Prestwood IT Solutions.   [Security & Privacy]