- •Contents at a Glance
- •Table of Contents
- •Acknowledgments
- •Introduction
- •Who This Book Is For
- •Finding Your Best Starting Point in This Book
- •Conventions and Features in This Book
- •Conventions
- •Other Features
- •System Requirements
- •Code Samples
- •Installing the Code Samples
- •Using the Code Samples
- •Support for This Book
- •Questions and Comments
- •Beginning Programming with the Visual Studio 2008 Environment
- •Writing Your First Program
- •Using Namespaces
- •Creating a Graphical Application
- •Chapter 1 Quick Reference
- •Understanding Statements
- •Identifying Keywords
- •Using Variables
- •Naming Variables
- •Declaring Variables
- •Working with Primitive Data Types
- •Displaying Primitive Data Type Values
- •Using Arithmetic Operators
- •Operators and Types
- •Examining Arithmetic Operators
- •Controlling Precedence
- •Using Associativity to Evaluate Expressions
- •Associativity and the Assignment Operator
- •Incrementing and Decrementing Variables
- •Declaring Implicitly Typed Local Variables
- •Chapter 2 Quick Reference
- •Declaring Methods
- •Specifying the Method Declaration Syntax
- •Writing return Statements
- •Calling Methods
- •Specifying the Method Call Syntax
- •Applying Scope
- •Overloading Methods
- •Writing Methods
- •Chapter 3 Quick Reference
- •Declaring Boolean Variables
- •Using Boolean Operators
- •Understanding Equality and Relational Operators
- •Understanding Conditional Logical Operators
- •Summarizing Operator Precedence and Associativity
- •Using if Statements to Make Decisions
- •Understanding if Statement Syntax
- •Using Blocks to Group Statements
- •Cascading if Statements
- •Using switch Statements
- •Understanding switch Statement Syntax
- •Following the switch Statement Rules
- •Chapter 4 Quick Reference
- •Using Compound Assignment Operators
- •Writing while Statements
- •Writing for Statements
- •Understanding for Statement Scope
- •Writing do Statements
- •Chapter 5 Quick Reference
- •Coping with Errors
- •Trying Code and Catching Exceptions
- •Handling an Exception
- •Using Multiple catch Handlers
- •Catching Multiple Exceptions
- •Using Checked and Unchecked Integer Arithmetic
- •Writing Checked Statements
- •Writing Checked Expressions
- •Throwing Exceptions
- •Chapter 6 Quick Reference
- •The Purpose of Encapsulation
- •Controlling Accessibility
- •Working with Constructors
- •Overloading Constructors
- •Understanding static Methods and Data
- •Creating a Shared Field
- •Creating a static Field by Using the const Keyword
- •Chapter 7 Quick Reference
- •Copying Value Type Variables and Classes
- •Understanding Null Values and Nullable Types
- •Using Nullable Types
- •Understanding the Properties of Nullable Types
- •Using ref and out Parameters
- •Creating ref Parameters
- •Creating out Parameters
- •How Computer Memory Is Organized
- •Using the Stack and the Heap
- •The System.Object Class
- •Boxing
- •Unboxing
- •Casting Data Safely
- •The is Operator
- •The as Operator
- •Chapter 8 Quick Reference
- •Working with Enumerations
- •Declaring an Enumeration
- •Using an Enumeration
- •Choosing Enumeration Literal Values
- •Choosing an Enumeration’s Underlying Type
- •Working with Structures
- •Declaring a Structure
- •Understanding Structure and Class Differences
- •Declaring Structure Variables
- •Understanding Structure Initialization
- •Copying Structure Variables
- •Chapter 9 Quick Reference
- •What Is an Array?
- •Declaring Array Variables
- •Creating an Array Instance
- •Initializing Array Variables
- •Creating an Implicitly Typed Array
- •Accessing an Individual Array Element
- •Iterating Through an Array
- •Copying Arrays
- •What Are Collection Classes?
- •The ArrayList Collection Class
- •The Queue Collection Class
- •The Stack Collection Class
- •The Hashtable Collection Class
- •The SortedList Collection Class
- •Using Collection Initializers
- •Comparing Arrays and Collections
- •Using Collection Classes to Play Cards
- •Chapter 10 Quick Reference
- •Using Array Arguments
- •Declaring a params Array
- •Using params object[ ]
- •Using a params Array
- •Chapter 11 Quick Reference
- •What Is Inheritance?
- •Using Inheritance
- •Base Classes and Derived Classes
- •Calling Base Class Constructors
- •Assigning Classes
- •Declaring new Methods
- •Declaring Virtual Methods
- •Declaring override Methods
- •Understanding protected Access
- •Understanding Extension Methods
- •Chapter 12 Quick Reference
- •Understanding Interfaces
- •Interface Syntax
- •Interface Restrictions
- •Implementing an Interface
- •Referencing a Class Through Its Interface
- •Working with Multiple Interfaces
- •Abstract Classes
- •Abstract Methods
- •Sealed Classes
- •Sealed Methods
- •Implementing an Extensible Framework
- •Summarizing Keyword Combinations
- •Chapter 13 Quick Reference
- •The Life and Times of an Object
- •Writing Destructors
- •Why Use the Garbage Collector?
- •How Does the Garbage Collector Work?
- •Recommendations
- •Resource Management
- •Disposal Methods
- •Exception-Safe Disposal
- •The using Statement
- •Calling the Dispose Method from a Destructor
- •Making Code Exception-Safe
- •Chapter 14 Quick Reference
- •Implementing Encapsulation by Using Methods
- •What Are Properties?
- •Using Properties
- •Read-Only Properties
- •Write-Only Properties
- •Property Accessibility
- •Understanding the Property Restrictions
- •Declaring Interface Properties
- •Using Properties in a Windows Application
- •Generating Automatic Properties
- •Initializing Objects by Using Properties
- •Chapter 15 Quick Reference
- •What Is an Indexer?
- •An Example That Doesn’t Use Indexers
- •The Same Example Using Indexers
- •Understanding Indexer Accessors
- •Comparing Indexers and Arrays
- •Indexers in Interfaces
- •Using Indexers in a Windows Application
- •Chapter 16 Quick Reference
- •Declaring and Using Delegates
- •The Automated Factory Scenario
- •Implementing the Factory Without Using Delegates
- •Implementing the Factory by Using a Delegate
- •Using Delegates
- •Lambda Expressions and Delegates
- •Creating a Method Adapter
- •Using a Lambda Expression as an Adapter
- •The Form of Lambda Expressions
- •Declaring an Event
- •Subscribing to an Event
- •Unsubscribing from an Event
- •Raising an Event
- •Understanding WPF User Interface Events
- •Using Events
- •Chapter 17 Quick Reference
- •The Problem with objects
- •The Generics Solution
- •Generics vs. Generalized Classes
- •Generics and Constraints
- •Creating a Generic Class
- •The Theory of Binary Trees
- •Building a Binary Tree Class by Using Generics
- •Creating a Generic Method
- •Chapter 18 Quick Reference
- •Enumerating the Elements in a Collection
- •Manually Implementing an Enumerator
- •Implementing the IEnumerable Interface
- •Implementing an Enumerator by Using an Iterator
- •A Simple Iterator
- •Chapter 19 Quick Reference
- •What Is Language Integrated Query (LINQ)?
- •Using LINQ in a C# Application
- •Selecting Data
- •Filtering Data
- •Ordering, Grouping, and Aggregating Data
- •Joining Data
- •Using Query Operators
- •Querying Data in Tree<TItem> Objects
- •LINQ and Deferred Evaluation
- •Chapter 20 Quick Reference
- •Understanding Operators
- •Operator Constraints
- •Overloaded Operators
- •Creating Symmetric Operators
- •Understanding Compound Assignment
- •Declaring Increment and Decrement Operators
- •Implementing an Operator
- •Understanding Conversion Operators
- •Providing Built-In Conversions
- •Creating Symmetric Operators, Revisited
- •Adding an Implicit Conversion Operator
- •Chapter 21 Quick Reference
- •Creating a WPF Application
- •Creating a Windows Presentation Foundation Application
- •Adding Controls to the Form
- •Using WPF Controls
- •Changing Properties Dynamically
- •Handling Events in a WPF Form
- •Processing Events in Windows Forms
- •Chapter 22 Quick Reference
- •Menu Guidelines and Style
- •Menus and Menu Events
- •Creating a Menu
- •Handling Menu Events
- •Shortcut Menus
- •Creating Shortcut Menus
- •Windows Common Dialog Boxes
- •Using the SaveFileDialog Class
- •Chapter 23 Quick Reference
- •Validating Data
- •Strategies for Validating User Input
- •An Example—Customer Information Maintenance
- •Performing Validation by Using Data Binding
- •Changing the Point at Which Validation Occurs
- •Chapter 24 Quick Reference
- •Querying a Database by Using ADO.NET
- •The Northwind Database
- •Creating the Database
- •Using ADO.NET to Query Order Information
- •Querying a Database by Using DLINQ
- •Creating and Running a DLINQ Query
- •Deferred and Immediate Fetching
- •Joining Tables and Creating Relationships
- •Deferred and Immediate Fetching Revisited
- •Using DLINQ to Query Order Information
- •Chapter 25 Quick Reference
- •Using Data Binding with DLINQ
- •Using DLINQ to Modify Data
- •Updating Existing Data
- •Adding and Deleting Data
- •Chapter 26 Quick Reference
- •Understanding the Internet as an Infrastructure
- •Understanding Web Server Requests and Responses
- •Managing State
- •Understanding ASP.NET
- •Creating Web Applications with ASP.NET
- •Building an ASP.NET Application
- •Understanding Server Controls
- •Creating and Using a Theme
- •Chapter 27 Quick Reference
- •Comparing Server and Client Validations
- •Validating Data at the Web Server
- •Validating Data in the Web Browser
- •Implementing Client Validation
- •Chapter 28 Quick Reference
- •Managing Security
- •Understanding Forms-Based Security
- •Implementing Forms-Based Security
- •Querying and Displaying Data
- •Understanding the Web Forms GridView Control
- •Displaying Customer and Order History Information
- •Paging Data
- •Editing Data
- •Updating Rows Through a GridView Control
- •Navigating Between Forms
- •Chapter 29 Quick Reference
- •What Is a Web Service?
- •The Role of SOAP
- •What Is the Web Services Description Language?
- •Nonfunctional Requirements of Web Services
- •The Role of Windows Communication Foundation
- •Building a Web Service
- •Creating the ProductsService Web Service
- •Web Services, Clients, and Proxies
- •Talking SOAP: The Easy Way
- •Consuming the ProductsService Web Service
- •Chapter 30 Quick Reference
430 |
Part IV Working with Windows Applications |
Adding Controls to the Form
So far, you have created a form, set some properties, added a few controls, and defined a style. To make the form useful, you need to add some more controls and write some code
of your own. The WPF library contains a varied collection of controls. The purposes of some are fairly obvious—for example, TextBox, ListBox, CheckBox, and ComboBox—whereas other,
more powerful, controls might not be so familiar.
Using WPF Controls
In the next exercise, you will add controls to the form that a user can use to input details about members of the bell ringers association. You will use a variety of controls, each suited to a particular type of data entry.
You will use TextBox controls for entering the first name and last name of the member. Each member belongs to a “tower” (where bells hang). The Middleshire district has several towers, but the list is static—new towers are not built very often, and hopefully, old towers do not to
fall down with any great frequency either. The ideal control for handling this type of data is a ComboBox. The form also records whether the member is the tower “captain” (the person in charge of the tower who conducts the other ringers). A CheckBox is the best sort of control for this; it can be either selected (True) or cleared (False).
Tip CheckBox controls can actually have three states if the IsThreeState property is set to True. The three states are true, false, and null. These states are useful if you are displaying information
that has been retrieved from a relational database. Some columns in a table in a database allow null values, indicating that the value held is not defined or is unknown.
The application also gathers statistical information about when members joined the association and how much bell-ringing experience they have (up to 1 year, between 1 and 4 years, between 5 and 9 years, and 10 or more years). You can use a group of options, or radio but-
tons, to indicate the member’s experience—radio buttons provide a mutually exclusive set of values. The older Microsoft Windows Forms library provides the DateTimePicker control
for selecting and displaying dates, and this control is ideal for indicating the date that the member joined the association. There is one small snag, however: The WPF library does not
provide an equivalent control. You can either implement your own custom control to provide this functionality or use Windows Forms interoperability and the WindowsFormsHost control to add the DateTimePicker control to a WPF form. You will adopt the latter approach in this
application.
Finally, the application records the tunes the member can ring—rather confusingly, these tunes are referred to as “methods” by the bell-ringing fraternity. Although a bell ringer rings only one bell at a time, a group of bell ringers under the direction of the tower captain can
Chapter 22 Introducing Windows Presentation Foundation |
431 |
ring their bells in different sequences and play simple music. There are a variety of bellringing methods, and they have rather quaint-sounding names such as Plain Bob, Reverse Canterbury, Grandsire, Stedman, Kent Treble Bob, and Old Oxford Delight. New methods are being written with alarming regularity, so the list of methods can vary over time. In a realworld application, you would store this list in a database. In this application, you will use a small selection of methods that you will hard-wire into the form. (You will see how to access and retrieve data from a database in Part V of this book, “Managing Data.”) A good control
for displaying this information and indicating whether a member can ring a method is a ListBox containing a list of CheckBox controls.
When the user has entered the member’s details, the Add button will validate and store the data. The user can click Clear to reset the controls on the form and cancel any data entered.
Add controls to the form
1.Ensure that Window1.xaml is displayed in the Design View window. Remove the two button controls and the text box control from the form.
2.In the XAML pane, change the Height property of the form to 470 and the Width property to 600, as shown in bold type here:
<Window x:Class=”BellRingers.Window1”
...
Title=”...” Height=”470” Width=”600”>
...
</Window>
3.In the Design View window, click the Window1 form. From the Toolbox, drag a Label control onto the form, and place it near the upper-left corner. Do not worry about positioning and sizing the label precisely because you will do this task for several controls later.
4.In the XAML pane, change the text for the label to First Name, as shown in bold type here:
<Label ...>First Name</Label>
Tip You can also change the text displayed by a label and many other controls by setting the Content property in the Properties window.
5.In the Design View window, click the Window1 form. From the Toolbox, drag a TextBox control onto the form to the right of the label.
Tip You can use the guide lines displayed by the Design View window to help align controls. (The guide lines are displayed after you drop the control on the form.)
432 |
Part IV Working with Windows Applications |
6.In the XAML pane, change the Name property of the text box to firstName, as shown here in bold type:
<TextBox ... Name=”firstName” .../>
7.Add a second Label control to the form. Place it to the right of the firstName text box. In the XAML pane, change the text for the label to Last Name.
8.Add another TextBox control to the form, and position it to the right of the Last Name label. In the XAML pane, change the Name property of this text box to lastName.
9.Add a third Label control to the form, and place it directly under the First Name label. In the XAML pane, change the text for the label to Tower.
10.Add a ComboBox control to the form. Place it under the firstName text box and to the right of the Tower label. In the XAML pane, change the Name property of this combo box to towerNames.
11.Add a CheckBox control to the form. Place it under the lastName text box and to the right of the towerNames combo box. In the XAML pane, change the Name property of the check box to isCaptain, and change the text displayed by this check box to
Captain.
12.Add a fourth Label to the form, and place it under the Tower label. In the XAML pane, change the text for this label to Member Since.
13.In Solution Explorer, right-click the References folder under the BellRingers project, and then click Add Reference. In the Add Reference dialog box, click the .NET tab, hold down the CTRL key while you select the System.Windows.Forms and
WindowsFormsIntegration assemblies, and then click OK.
In the following steps, you will add a WindowsFormsHost control to the form to hold a DateTimePicker control. These controls require the application to reference the System. Windows.Forms and WindowsFormsIntegration assemblies.
14.In the XAML pane, add the following XML namespace declaration shown in bold type to the Window1 form. This declaration brings the types in the Windows Forms library into scope and establishes wf as an alias for this namespace:
<Window x:Class=”BellRingers.Window1” xmlns=”http://schemas.microsoft.com/winfx/2006/xaml/presentation” xmlns:x=”http://schemas.microsoft.com/winfx/2006/xaml” xmlns:wf=”clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms”
Title=...>
...
</Window>
Chapter 22 Introducing Windows Presentation Foundation |
433 |
15.On the Build menu, click Build Solution.
This step is necessary to enable the Visual Studio 2008 window to resolve the references to the System.Windows.Forms namespace correctly before you add Windows Forms controls to the application.
16.From the Toolbox, in the Controls section, add a WindowsFormsHost control to the form, and place it under the towerNames combo box.
Note After you position the WindowsFormsHost control, it becomes invisible. Don’t worry too much about placing this control because you will modify the control’s properties in the
XAML description in the next exercise. Additionally, the XAML pane might display a warning stating that the type WindowsFormsHost was not found. As long as you have added the references to the System.Windows.Forms and WindowsFormsIntegration assemblies to
the project, you can ignore this warning.
17.In the XAML pane, change the Name property of the WindowsFormsHost control to hostMemberSince. Still in the XAML pane, add a Windows Forms DateTimePicker control as a child property to the WindowsFormsHost control, and name it memberSince.
(As with other child properties, you must change the closing tag delimiter (/>) of the WindowsFormsHost control to an ordinary delimiter character (>) and add a closing </WindowsFormsHost> tag for the WindowsFormsHost control.) The completed XAML code for the WindowsFormsHost control should look like this:
<WindowsFormsHost ... Name=”hostMemberSince” ...>
<wf:DateTimePicker Name=”memberSince”/> </WindowsFormsHost>
The DateTimePicker control should appear on the form and display the current date. Depending on how your Visual Studio windows are arranged, you might need to scroll to see the new control.
18.Add a GroupBox control from the Containers section of the Toolbox to the form, and place it under the Member Since label. In the XAML pane, change the Name property of the group box to yearsExperience, and change the Header property to Experience. The Header property changes the label that appears on the form for the group box.
19.Add a StackPanel control to the form, and place it inside the yearsExperience group box. In the XAML pane, verify that the StackPanel control occurs inside the XAML code for the GroupBox control, like this:
<GroupBox Header=”Experience” ... Name=”yearsExperience” ...> <StackPanel ... Name=”stackPanel1” ... />
</GroupBox>
434 |
Part IV Working with Windows Applications |
20.Add a RadioButton control to the form, and place it inside the StackPanel control you just added. Add three more RadioButton controls to the StackPanel control. They should automatically be arranged vertically.
21.In the XAML pane, change the Name property of each radio button and the text it displays, as shown here in bold type:
<GroupBox...> <StackPanel ...>
<RadioButton ... Name=”novice” ...>Up to 1 year</RadioButton> <RadioButton ... Name=”intermediate” ...>1 to 4 years</RadioButton> <RadioButton ... Name=”experienced” ...>5 to 9 years</RadioButton> <RadioButton ... Name=”accomplished” ...>10 or more years</RadioButton>
</StackPanel>
</GroupBox>
22.Add a ListBox control to the form, and place it to the right of the GroupBox control. In the XAML pane, change the Name property of the list box to methods.
23.Add a Button control to the form, and place it near the bottom on the lower-left side of the form, underneath the GroupBox control. In the XAML pane, change the Name property of this button to add, and change the text displayed by this button to Add.
24.Add another Button control to the form, and place it near the bottom to the right of the Add button. In the XAML pane, change the Name property of this button to clear, and change the text displayed by this button to Clear.
You have now added all the required controls to the form. The next step is to tidy up the
layout. The following table lists the layout properties and values you should assign to each of the controls. Using the XAML pane or the Properties window, make these changes. The
margins and alignment of the controls are designed to keep the controls in place if the user
resizes the form. Also notice that the margin values specified for the radio buttons are relative to each preceding item in the StackPanel control containing them; the first radio button is 10 units from the top of the StackPanel control, and the remaining radio buttons have a
gap between them of 20 units vertically.
Control |
Property |
Value |
label1 |
Height |
23 |
|
|
|
|
Margin |
29, 25, 0, 0 |
|
|
|
|
VerticalAlignment |
Top |
|
|
|
|
HorizontalAlignment |
Left |
|
|
|
|
Width |
75 |
|
|
|
firstName |
Height |
21 |
|
|
|
|
Margin |
121, 25, 0, 0 |
|
|
|
|
Chapter 22 Introducing Windows Presentation Foundation |
435 |
||
Control |
Property |
Value |
|
|
|
VerticalAlignment |
Top |
|
|
|
|
|
|
|
|
HorizontalAlignment |
Left |
|
|
|
|
|
|
|
|
Width |
175 |
|
|
|
|
|
|
|
label2 |
Height |
23 |
|
|
|
|
|
|
|
|
Margin |
305, 25, 0, 0 |
|
|
|
|
|
|
|
|
VerticalAlignment |
Top |
|
|
|
|
|
|
|
|
HorizontalAlignment |
Left |
|
|
|
|
|
|
|
|
Width |
75 |
|
|
|
|
|
|
|
lastName |
Height |
21 |
|
|
|
|
|
|
|
|
Margin |
380, 25, 0, 0 |
|
|
|
|
|
|
|
|
VerticalAlignment |
Top |
|
|
|
|
|
|
|
|
HorizontalAlignment |
Left |
|
|
|
|
|
|
|
|
Width |
175 |
|
|
|
|
|
|
|
label3 |
Height |
23 |
|
|
|
|
|
|
|
|
Margin |
29, 72, 0, 0 |
|
|
|
|
|
|
|
|
VerticalAlignment |
Top |
|
|
|
|
|
|
|
|
HorizontalAlignment |
Left |
|
|
|
|
|
|
|
|
Width |
75 |
|
|
|
|
|
|
|
towerNames |
Height |
21 |
|
|
|
|
|
|
|
|
Margin |
121, 72, 0, 0 |
|
|
|
|
|
|
|
|
VerticalAlignment |
Top |
|
|
|
|
|
|
|
|
HorizontalAlignment |
Left |
|
|
|
|
|
|
|
|
Width |
275 |
|
|
|
|
|
|
|
isCaptain |
Height |
21 |
|
|
|
|
|
|
|
|
Margin |
420, 72, 0, 0 |
|
|
|
|
|
|
|
|
VerticalAlignment |
Top |
|
|
|
|
|
|
|
|
HorizontalAlignment |
Left |
|
|
|
|
|
|
|
|
Width |
75 |
|
|
|
|
|
|
|
Label4 |
Height |
23 |
|
|
|
|
|
|
|
|
Margin |
29, 134, 0, 0 |
|
|
|
|
|
|
|
|
VerticalAlignment |
Top |
|
|
|
|
|
|
|
436 |
Part IV Working with Windows Applications |
|
||
|
|
Control |
Property |
Value |
|
|
|
HorizontalAlignment |
Left |
|
|
|
|
|
|
|
|
Width |
90 |
|
|
|
|
|
|
|
hostMemberSince |
Height |
23 |
|
|
|
|
|
|
|
|
Margin |
121, 134, 0, 0 |
|
|
|
|
|
|
|
|
VerticalAlignment |
Top |
|
|
|
|
|
|
|
|
HorizontalAlignment |
Left |
|
|
|
|
|
|
|
|
Width |
275 |
|
|
|
|
|
|
|
yearsExperience |
Height |
200 |
|
|
|
|
|
|
|
|
Margin |
29, 174, 0, 0 |
|
|
|
|
|
|
|
|
VerticalAlignment |
Top |
|
|
|
|
|
|
|
|
HorizontalAlignment |
Left |
|
|
|
|
|
|
|
|
Width |
258 |
|
|
|
|
|
|
|
stackPanel1 |
Height |
151 |
|
|
|
|
|
|
|
|
Width |
224 |
|
|
|
|
|
|
|
Novice |
Height |
16 |
|
|
|
|
|
|
|
|
Margin |
0, 10, 0, 0 |
|
|
|
|
|
|
|
|
Width |
120 |
|
|
|
|
|
|
|
Intermediate |
Height |
16 |
|
|
|
|
|
|
|
|
Margin |
0, 20, 0, 0 |
|
|
|
|
|
|
|
|
Width |
120 |
|
|
|
|
|
|
|
Experienced |
Height |
16 |
|
|
|
|
|
|
|
|
Margin |
0, 20, 0, 0 |
|
|
|
|
|
|
|
|
Width |
120 |
|
|
|
|
|
|
|
Accomplished |
Height |
16 |
|
|
|
|
|
|
|
|
Margin |
0, 20, 0, 0 |
|
|
|
|
|
|
|
|
Width |
120 |
|
|
|
|
|
|
|
Methods |
Height |
200 |
|
|
|
|
|
|
|
|
Margin |
310, 174, 0, 0 |
|
|
|
|
|
|
|
|
VerticalAlignment |
Top |
|
|
|
|
|
|
|
|
HorizontalAlignment |
Left |
Width |
245 |
|
Chapter 22 Introducing Windows Presentation Foundation |
437 |
||
Control |
Property |
Value |
|
|
Add |
Height |
23 |
|
|
|
|
|
|
|
|
Margin |
188, 388, 0, 0 |
|
|
|
|
|
|
|
|
VerticalAlignment |
Top |
|
|
|
|
|
|
|
|
HorizontalAlignment |
Left |
|
|
|
|
|
|
|
|
Width |
75 |
|
|
|
|
|
|
|
Clear |
Height |
23 |
|
|
|
|
|
|
|
|
Margin |
313, 388, 0, 0 |
|
|
|
|
|
|
|
|
VerticalAlignment |
Top |
|
|
|
|
|
|
|
|
HorizontalAlignment |
Left |
|
|
|
|
|
|
|
|
Width |
75 |
|
|
|
|
|
|
|
As a finishing touch, you will next apply a style to the controls. You can use the bellRingersStyle style for controls such as the buttons and text boxes, but the labels, combo box, group
box, and radio buttons should probably not be displayed on a gray background.
Apply styles to the controls, and test the form
1.In the XAML pane, add the bellRingersFontStyle shown in bold type in the following code to the <Windows.Resources> element. Leave the existing bellRingersStyle style in place. Notice that this style changes the font only of controls that reference this style.
<Window.Resources>
<Style x:Key=”bellRingersFontStyle” TargetType=”Control”> <Setter Property=”FontFamily” Value=”Comic Sans MS”/>
</Style>
<Style x:Key=”bellRingersStyle” TargetType=”Control”>
...
</Style>
</Window.Resources>
2.In the XAML pane, apply the bellRingersFontStyle style to the label1 control, as shown in bold type here:
<Label Style=”{StaticResource bellRingersFontStyle}” ...>First Name</Label>
Apply the same style to the following controls:
label2
label3
isCaptain
towerNames
438 |
Part IV Working with Windows Applications |
label4
yearsExperience
methods
Note Applying the style to the yearsExperience group box and the methods list box automatically causes the style to be used by the items displayed in these controls.
3. Apply the bellRingersStyle style to the following controls: firstName
lastName add clear
4.On the Debug menu, click Start Without Debugging.
The form when it runs should look like the following image:
Notice that the Methods list box is currently empty. You will add code to populate it in a later exercise.
5.Click the drop-down arrow in the Tower combo box. The list of towers is currently empty. Again, you will write code to fill this combo box in a later exercise.
6.Close the form, and return to Visual Studio 2008.
