Cse8 ch02
Cse8 ch02
murach’s
C# 8th Edition
(Chapter 2)
Thanks for downloading this chapter from Murach’s C# (8th Edition). We hope it
will show you how easy it is to learn from any Murach book, with its paired-pages
presentation, its “how-to” headings, its practical coding examples, and its clear, concise
style.
To view the full table of contents for this book, you can go to our website. From there,
you can read more about this book, you can find out about any additional downloads that
are available, and you can review our other books on related topics.
Thanks for your interest in our books!
“I am actually flying through the C# book! And a lot of the topics I had
problems with in the past are now making perfect sense in this book.”
Jim Bonner, Test Lab Engineer, Washington
“In 20+ years of software development, I have never read another book so
well organized. Topics flow in a well-thought-out, logical order and are easy to
follow and understand. When I need new technical books in the future, I will
always search for a Murach book first.”
Jeff Rumage, Lower Alabama .NET User Group
“Best C# book ever, precise and to the point, with lots and lots of examples.
Highly recommended for beginners.”
Posted at an online bookseller
“Murach has yet again hit a home run! This book gets you from zero to Jr Dev
knowledge in no time.”
Brian Knight, Founder, Pragmatic Works, Florida
“I have to tell you that your C# book is far and away the best resource I have
seen to date. It is simple, straightforward, presents logical examples, and the
two-page format is the best.”
Timothy Layton, Developer, Missouri
“I have learned more in the 800+ pages of this book than I have reading half a
dozen other books and hours of forum posts online.”
Posted at an online bookseller
2
How to design a Windows
Forms app
In the last chapter, you learned the basic skills for working with Visual Studio,
you toured a Windows Forms app, and you tested an app with three Windows
forms. Now, in this chapter, you’ll learn how to use Visual Studio to design the
user interface for a Windows Forms app.
Description
•• The project template that you select determines the initial files, dependencies, code,
and property settings that are added to the project.
Description
•• Visual Studio remembers the location you specify when you create a new project
and uses it as the default location the next time you create a project.
•• If the “Place solution and project in the same directory” box is checked, Visual Studio
stores the solution and project files in the same folder. Otherwise, it creates a folder for
the solution and a subfolder for the project.
Text box
Labels
Read-only
text boxes
Buttons
Description
•• A text box is used to get the subtotal from the user. Read-only text boxes are used
to display the discount percent, discount amount, and total. And label controls are
used to identify the values that are in the text boxes on the form.
•• After entering a subtotal, the user can click the Calculate button to calculate the
discount percent, discount amount, and total. Alternatively, the user can press the
Enter key to perform the calculation.
•• To calculate another invoice total, the user can enter another subtotal and then click
the Calculate button or press the Enter key again.
•• To close the form and end the application, the user can click the Close button in the
upper right corner of the form or click the Exit button. Alternatively, the user can
press the Esc key to exit from the form.
•• The user can press Alt+C to activate the Calculate button or Alt+X to activate the
Exit button. On most systems, the letters that activate these buttons aren’t under-
lined until the user presses the Alt key.
Control that’s
selected in
the Toolbox
Description
•• The Properties window displays the properties for the object that’s currently
selected in the Form Designer window. To display the properties for another object,
click on that object or select the object from the drop-down list at the top of the
Properties window.
•• To change a property, enter a value into the text box or select a value from its drop-
down list if it has one. If a button with an ellipsis (…) appears at the right side of a
property’s text box, you can click on the ellipsis to display a dialog that lets you set
options for the property.
•• To change the properties for two or more controls at the same time, select the
controls. Then, the common properties of the controls are displayed in the
Properties window.
•• When you click on a property in the Properties window, a brief explanation of the
property appears in a pane at the bottom of the window. For more information,
press F1 to display the help information for the property.
•• You can use the first two buttons at the top of the Properties window to sort the
properties by category or alphabetically.
•• You can use the plus (+) and minus (-) signs displayed to the left of some of the
properties and categories in the Properties window to expand and collapse the list
of properties.
Note
•• If a description isn’t displayed when you click on a property in the Properties
window, right-click on the window and select Description from the shortcut menu.
Note
•• To provide an access key for the Subtotal text box, you can set the TabIndex and
Text properties for the Subtotal label as shown above.
Figure 2-8 The property settings for the Invoice Total form
50 Section 1 An introduction to Visual Studio
Perspective
If you can design the Invoice Total form that’s presented in this chapter,
you’ve taken a critical first step toward learning how to develop Windows
Forms apps with Visual Studio. The next step is to add the code that makes the
form work the way you want it to, and that’s what you’ll learn to do in the next
chapter.
Terms
template
label
text box
button
primary control
property
tab order
focus
access key
7. Use the Properties window to set the properties for the form and its controls
so it looks like the form in figure 2-3. These properties are summarized in
figure 2-8.
Test the user interface
8. Press F5 to build and run the project. That should display the form in the
center of the screen, and it should look like the one in figure 2-3.
9. Experiment with the form to see what it can do. When you press the Tab key,
notice how the focus moves from one control to another. When you click
a button, notice how it indents and then pops back out just like any other
Windows button control. Nothing else happens in response to these button
clicks, though, because you haven’t written the code for them yet.
Notice that the Calculate button has a dark outline around it to indicate that its
function will be executed if you press the Enter key. (If it doesn’t have a dark
outline, you haven’t set the AcceptButton property of the form to the button.)
When you press the Alt key, notice that an underline appears under the s in
Subtotal, the first c in Calculate, and the x in Exit to indicate that you can use
an access key to work with these controls. (If the underlines don’t show, you
haven’t entered the Text properties correctly.)
10. If you notice that some of the properties are set incorrectly, click the Close
button in the upper right corner of the form to close the form. Then, make the
necessary changes and run the project again. When you’re satisfied that the
form is working right, close the form to return to the Form Designer.
Experiment with the properties for the form and its controls
11. In the Form Designer, click on the form so it is selected. Then, if necessary,
adjust the Properties window so you can see the description for each property.
To do that, drag the bottom boundary of the window up.
12. Click on the Categorized button at the top of the Properties window to display
the properties by category. Then, review the properties in the Appearance,
Behavior, Layout, and Window Style categories. Although you won’t under-
stand all of the descriptions, you should understand some of them.
13. In the Window Style category, change the settings for the MaximizeBox and
MinimizeBox to false to see how that changes the form. Then, to undo those
changes, click twice on the Undo button in the Standard toolbar or press
Ctrl+Z twice.
14. Click on the first text box and review the Appearance, Behavior, and Layout
properties for that control. Then, repeat this process for one of the labels and
one of the buttons. Here again, you won’t understand all of the descriptions,
but you should understand some of them.
15. Select all four of the labels, click on the plus sign before the Font property in
the Appearance group, and change the Bold setting to True to see how that
changes the form. Then, undo that change.
54 Section 1 An introduction to Visual Studio
100% Guarantee
When you buy directly from us, you must be
satisfied. Try our books for 30 days or our eBooks
for 14 days. They must outperform any competing
book or course you’ve ever tried, or return your
purchase for a prompt refund.…no questions asked.