Читать книгу C# 24-Hour Trainer - Stephens Rod - Страница 22
Section I
The Visual Studio IDE and Controls
Lesson 2
Creating Controls
Understanding Controls
ОглавлениеA control is a programming entity that combines a visible appearance on the screen and code to manage it. The code defines the control's appearance and behavior.
For example, a TextBox control displays a blank area on the screen where the user can type information. The code inside the control determines how the control draws itself and provides normal textbox features such as multiline or single-line behavior; scrolling and scrollbars displayed as needed; copy, cut, and paste; a context menu displayed when you right-click the control; the ability to navigate when the user presses the Tab key; and much more.
What's in a Name?
By convention, in C# the names of control types (and other types) use Pascal casing where multiple words are strung together with the first letter of each word capitalized; for example, TextBox, ProgressBar, Button, and PictureBox.
In addition to controls, C# provides components. A component is similar to a control except it has no visible piece on the form. For example, the Timer component acts as a clock to let the program do something at regular intervals. The Timer interacts with the program but doesn't display anything visible to the user. (Some components such as ErrorProvider and ToolTip may display visible effects on the screen, but the components themselves are not visible on the form.)
The features of controls (and components) fall into three categories: properties, methods, and events.
Properties
A property determines the appearance and state of a control. If a Car were a control, its properties would be things like Color, TransmissionType, CurrentSpeed, and NumberOfCupHolders. Your program could set a Car's Color to HotPink (to attract the attention of other drivers) or set its CurrentSpeed to 110 (to attract the attention of the police).
For a programming example, the TextBox control has a Font property that determines the font it uses and a ForeColor property that determines the color of its text.
Methods
A method is a feature of a control that makes the control perform some action. Your code can call a method to make the control do something. For example, the Car control might have methods such as Start, Stop, EjectPassenger, and OilSlick. Your program could call the OilSlick method to make the car spray oil out the back so you can escape from spies.
For a programming example, the TextBox has a Clear method that blanks the control's text and an AppendText method that adds text to the end of whatever the control is currently displaying.
Events
An event occurs when something interesting happens to the control. The control raises or fires the event to tell the program that something happened. For example, a Car might have RanOutOfGas and Crashed events. The Car control would raise the Crashed event to tell the program that the user had driven it into a tree. The program could then take action such as calling an ambulance and a tree surgeon.
For a programming example, the TextBox has a TextChanged event that tells the program that its text has changed. When the event occurs, the program could examine the text to see if the user had entered a valid input. For example, if the TextBox should hold a number and the user entered “One,” the program could beep and change the TextBox's BackColor property to Yellow to indicate an error.
Later lessons discuss events and the code that handles them in greater detail. This lesson focuses on adding controls to a form, arranging them, and setting their properties.