Vous êtes sur la page 1sur 5

Using Visual Studio.

Net to build the


"Hello WinForm" Application

Creating a new Project


First of all, we need to create a new C# Windows Application Project. For this, start Visual
Studio.Net and click FileNewProject. It will show the following screen:

From the above screen, select Visual C# Projects in Project types and Windows
Application in Templates. Write the name of the new project (LearningWinForm in the
above figure) in the text box labeled Name. Select the location where you wish to store the
project using the Browse... Button and click OK. It will show you an empty form in the
designer view similar to the figure below:
You can see the toolbox window at the left hand side and the properties window at the
right hand side of the above snapshot. The toolbox allows you to add different controls to
your form. Once the control is placed on the form, you can change its various properties
from the Properties window. You can also change the location and size of the controls using
the mouse. Event properties can be changed by switching to the Event Poperties pane ( )
in the Properties Window.
Setting various properties of the form
You can change the default properties of the form using the Properties window. For this,
select (click) the form and select the properties window (If the properties window is not
visible in the right hand pane, select ViewProperties Window). Now change the title of the
form and the name of the forms class in the code by changing the Text and Name
properties, as shown in the following figure.

Adding Controls to the Form


Now select the Label control from the toolbox, place it on the form and resize it as
appropriate. Select (click) the label on the form and from the properties window, set its Text
property to "Hello WinForm" and the Name property to lblGreeting. The name of a control is
the name of its corresponding instance in the source code. Now select the Button control
from the toolbox, place it on the form and resize it appropriately. Select (click) the button and
set its Name property to btnExit and its Text property to Exit using the properties window.
The form should
now look like this:
Adding Event Handling
Now we need to add the event handling code for the Exit button. For this, simply double click
the Exit button in the designer. This will create a new event handler for the Exit buttons Click
event and take you to the source code as shown in the following figure:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Laborator1
{
public partial class MyForm : Form
{
public MyForm()
{
InitializeComponent();
}
private void btnExit_Click(object sender, EventArgs e)
{

}
}
}

Write the code to close the application (Application.Exit()) in the event handler. The IDE has
not only created the event handler but also has registered it with the Exit buttons Click
event.

Executing the application


That is it! The Hello WinForm application is complete. To compile and execute the
application, select BuildBuild Solution (or press Ctrl+Shift+B) and then select DebugStart
Without Debugging (or press Ctrl+F5). This will compile and start the Hello WinForm
application in a new window as shown below:

To close the application, click the Exit button or the close button on the title bar of the
window.

Vous aimerez peut-être aussi