Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
CSharp_for_Beginners.doc
Скачиваний:
35
Добавлен:
13.02.2015
Размер:
2.39 Mб
Скачать

Setting Properties

After you add a control to your form, you can use the Properties window to set its properties, such as background color and default text. 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 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. For more information, see Properties Window.

Handling Events

Programs with graphical user interfaces are primarily event-driven. They wait until a user does something such as entering 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 diagram shows the events button.

Задание свойств

После добавления элемента управления в форму в окне Свойства можно задать его свойства, такие как цвет фона и текст по умолчанию. Значения, задаваемые в окне Свойства, являются начальными значениями, которые будут назначены соответствующему свойству при создании элемента управления во время выполнения. Во многих случаях доступ к значениям и их изменение возможно программными средствами во время выполнения путем получения или установки свойств в экземпляре класса элемента управления в приложении. Окно Свойство может оказаться полезным во время выполнения, так как с его помощью можно просматривать все свойства, события и методы, поддерживаемые элементом управления.

Обработка событий

Программы с графическим интерфейсом пользователя главным образом основаны на событиях. Такие программы ожидают действий пользователя, например ввода текста в текстовое поле, нажатия кнопки или изменения выбора в поле со списком. При выполнении действия элемент управления, который всего лишь представляет собой экземпляр класса .NET Framework, отправляет событие в приложение. Для обработки события можно написать специальный метод в приложении, который будет вызван при получении события.

В окне Свойства можно указать события, которые должны обрабатываться в коде. Для просмотра событий элемента управления следует выбрать его в конструкторе и нажать кнопку События с изображением молнии в окне Свойства. Следующая схема отображает кнопку событий.

When you add an event handler through the Properties window, the designer will automatically write the empty method body for you, and you must write the code to make the method do something useful. Most controls generate many events, but most of the time an application will only have to handle several 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 Paint event unless you want to customize its appearance in some advanced way. Each control has a default event handler. You can create the default event handler by double-clicking the control in the designer. This creates the event handler and opens the Code Editor so that you can write code to handle the event.

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]