logo
CSharp_Prog_Guide

Добавление элементов управления

Элементы управления, являющиеся компонентами с визуальным представлением (например, кнопки и текстовые поля) можно перетащить мышью в поверхность разработки одного из конструкторов. На рисунке ниже показано поле со списком, которое при помощи перетаскивания из Панели элементов было добавлено в форму в Конструкторе Windows Forms.

As you work visually, the Windows Forms Designer translates your actions into C# source code and writes them into a project file that is named name.designer.cs where name is the name that you gave to the form. Similarly, the WPF designer translates actions on the design surface into Extensible Application Markup Language (XAML) code and writes it into a project file that is named Window.xaml. When your application runs, that source code (Windows Form) or XAML (WPF) will position and size your UI elements so that they appear just as they do on the design surface.

При работе в визуальном режиме конструктор Windows Forms переводит выполняемые действия в исходный код C# и записывает их в файл проекта с именем имя.designer.cs, где имя – имя, назначенное форме. Подобным образом, конструктор WPF переводит действия на поверхности разработки в код языка XAML и записывает его в файл проекта с именем Window.xaml. Когда приложение будет выполняться, исходный код (Windows Form) или XAML (WPF) разместит элементы пользовательского интерфейса и скорректирует их размер так, как они отображаются на поверхности построения.

Setting Properties

After you add a control to the design surface, you can use the Properties window to set its properties, such as background color and default text.

In the Windows Form designer, the values that you specify in the Properties window are the initial values that will be assigned to that property when the control is created at run time. In the WPF designer, the values that you specify in the Properties window are stored as attributes in the window's XAML file.

In many cases, those values can be accessed or changed programmatically at run time by getting or setting the property on the instance of the control class in your application. The Properties window is useful at design time because it enables you to browse all the properties, events, and methods supported on a control.

Handling Events

Programs with graphical user interfaces are primarily event-driven. They wait until a user does something such as typing text into a text box, clicking a button, or changing a selection in a list box. When that occurs, the control, which is just an instance of a .NET Framework class, sends an event to your application. You have the option of handling an event by writing a special method in your application that will be called when the event is received.

You can use the Properties window to specify which events you want to handle in your code. Select a control in the designer and click the Events button, with the lightning bolt icon, on the Properties window toolbar to see its events. The following icon shows the events button.

When you add an event handler through the Properties window, the designer automatically writes the empty method body. You must write the code to make the method do something useful. Most controls generate many events, but frequently an application will only have to handle some of them, or even only one. For example, you probably have to handle a button's Click event, but you do not have to handle its SizeChanged event unless you want to do something when the size of the button changes.