ТОП просматриваемых книг сайта:
C# 24-Hour Trainer. Stephens Rod
Читать онлайн.Название C# 24-Hour Trainer
Год выпуска 0
isbn 9781119065692
Автор произведения Stephens Rod
Жанр Зарубежная образовательная литература
Издательство John Wiley & Sons Limited
5. [WPF] Create a new WPF application. Run it side by side with a Windows Forms application. What are the differences? (Hint: There shouldn't be many and they should be cosmetic. You learn about more important but less obvious differences in later lessons.)
NOTE
Please select the videos for Lesson 1 online at www.wrox.com/go/csharp24hourtrainer2evideos.
Lesson 2
Creating Controls
Way back in the computer stone ages, when programmers worked by candlelight on treadle-powered computers and hand-carved wooden monitors, input and output were very simple. The computer wrote text in toxic green on the bottom of a monitor and the text scrolled up as the monitor became full. The user typed on a keyboard to enter text at a single input prompt, and that was about it. Multiple windows performing useful work simultaneously, mice, and forms displaying many labels and textboxes, buttons, scrollbars, and full-color images existed only in the fevered dreams of science-fiction writers.
Today these things are so commonplace that we take them completely for granted. They appear in desktop software, web pages, laptops, handheld computers, and even cell phones.
Building these sorts of objects in the old days would have been extremely difficult, but today it's practically trivial to add them to your application.
You already saw in Lesson 1 how easy it is to make an application (albeit a trivial one) that displays a form that runs independently of the others on the computer. It's almost as easy to use labels, textboxes, buttons, scrollbars, images, menus, popups, and everything else that makes up a modern application.
C# makes all of these objects and more available as controls.
In this lesson, you learn how to add controls to a form. You learn how to size, position, and arrange controls. You also learn how to use a control's properties to change its appearance and behavior at design time and at run time. When you're done with this lesson, you'll be able to build a professional-looking form.
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.
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.
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.
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.
Creating Controls
Adding controls to a form is easy. In fact, it's so easy and there are so many different ways to add controls to a form that it takes a while to describe them all.
Start by creating a new project as described in Lesson 1. Open the form in the Form Designer. (If the form isn't already open, double-click it in Solution Explorer.)
The following list describes some of the ways you can put controls on the form:
● Click a tool in the Toolbox to select it. Then click and drag on the form. When you release the mouse, Visual Studio creates the control in the area you selected and then selects the pointer in the Toolbox.
● Click a tool in the Toolbox to select it. Then hold down the Ctrl key while you click and drag on the form to place a copy of the control on the form. When you release the mouse, Visual Studio creates the control in the area you selected and keeps the control's tool selected in the Toolbox so you can make another control of that type.
● Double-click a tool in the Toolbox to create an instance of the control on the form at a default size and position. (You'll then probably want to resize and reposition it.)
● Select one or more controls that are already on the form, press Ctrl+C to copy them, and then press Ctrl+V to paste them onto the form. You can even copy and paste from one instance of Visual Studio to another.
● Select one or more controls on the form. While holding down the Ctrl key, drag the controls to a new location. Visual Studio makes a copy of the controls, leaving the originals where they started.
NOTE
You have several ways to select controls on the Form Designer. Click a control to select only it. Click and drag to select multiple controls.
Hold down the Shift or Ctrl key while clicking or clicking and dragging to toggle whether controls are in the current selection.
And, if you want to deselect all controls, simply click an empty part of the form or press Esc.
The