Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Лаба №1 / books / csharp_ebook.pdf
Скачиваний:
77
Добавлен:
03.03.2016
Размер:
3.69 Mб
Скачать

Programmers Heaven: C# School

Format Menu: Provides access to a set of useful operations for formatting the controls and their layout in the Form Designer view.

Tools Menu: Provides the access to various useful Visual Studio.Net tools.

Using Visual Studio.Net to build the "Hello WinForm" Application

Now we've had a quick tour of Visual Studio.Net, let's use the Visual Studio.Net IDE to build the "Hello WinForm" application which we created earlier in the lesson.

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 File'New'Project. 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:

200

Programmers Heaven: C# School

The layout of the form designer screen may be somewhat different from the one shown above. Your toolbox and properties window may be visible and some other windows may not be visible. You can change these settings using the View menu as described earlier in the lesson.

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 View'Properties Window). Now change the title of the form and the name of the form's class in the code by changing the Text and Name properties, as shown in the following figure.

201

Programmers Heaven: C# School

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:

202

Programmers Heaven: C# School

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 button's Click event and take you to the source code as shown in the following figure.

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 button's Click event.

Executing the application

That is it! The 'Hello WinForm' application is complete. To compile and execute the application, select Build'Build Solution (or press Ctrl+Shift+B) and then select Debug'Start Without Debugging (or press Ctrl+F5).

This will compile and start the 'Hello WinForm' application in a new window as shown below:

203

Соседние файлы в папке books