Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
C# 2008 Step by Step.pdf
Скачиваний:
22
Добавлен:
25.03.2016
Размер:
13.96 Mб
Скачать

284

Part III Creating Components

Declaring Interface Properties

You encountered interfaces in Chapter 13, “Creating Interfaces and Defining Abstract Classes.” Interfaces can define properties as well as methods. To do this, you specify the get or set keyword, or both, but replace the body of the get or set accessor with a semicolon. For example:

interface IScreenPosition

{

int X { get; set; } int Y { get; set; }

}

Any class or structure that implements this interface must implement the X and Y properties with get and set accessor methods. For example:

struct ScreenPosition : IScreenPosition

{

...

public int X

{

get { ... } set { ... }

}

public int Y

{

get { ... } set { ... }

}

...

}

If you implement the interface properties in a class, you can declare the property implementations as virtual, which enables derived classes to override the implementations. For example:

class ScreenPosition : IScreenPosition

{

...

public virtual int X

{

get { ... } set { ... }

}

public virtual int Y

{

get { ... } set { ... }

}

...

}

Chapter 15 Implementing Properties to Access Fields

285

Note This example shows a class. Remember that the virtual keyword is not valid when creating a struct because structures are implicitly sealed.

You can also choose to implement a property by using the explicit interface implementation syntax covered in Chapter 13. An explicit implementation of a property is nonpublic and nonvirtual (and cannot be overridden). For example:

struct ScreenPosition : IScreenPosition

{

...

int IScreenPosition.X

{

get { ... } set { ... }

}

int IScreenPosition.Y

{

get { ... } set { ... }

}

...

private int x, y;

}

Using Properties in a Windows Application

When you set property values of objects such as TextBox controls, Windows, and Button controls by using the Properties window in Microsoft Visual Studio 2008, you are actually generating code that sets the values of these properties at run time. Some components have a large number of properties, although some properties are more commonly used than others. You can write your own code to modify many of these properties at run time by using the same syntax you have seen throughout this chapter.

In the following exercise, you will use some predefined properties of the TextBox controls and the Window class to create a simple application that continually displays the size of its main

window, even when the window is resized.

Use properties

1.Start Visual Studio 2008 if it is not already running.

2.Open the WindowProperties project, located in the \Microsoft Press\Visual CSharp Step by Step\Chapter 15\WindowProperties folder in your Documents folder.

3.On the Debug menu, click Start Without Debugging.

286

Part III Creating Components

The project builds and runs. A window (a Windows Presentation Foundation [WPF] form) appears, displaying two empty text boxes labeled Width and Height.

In the program, the text box controls are named width and height. They are currently empty. You will add code to the application that displays the current size of the window.

4.Close the form, and return to the Visual Studio 2008 programming environment.

5.Display the Window1.xaml.cs file in the Code and Text Editor window, and locate the sizeChanged method.

This method is called by the Window1 constructor. You will use it to display the current size of the form in the width and height text boxes. You will make use of the ActualWidth and ActualHeight properties of the Window class. These properties return the current width and height of the form as double values.

6.Add two statements to the sizeChanged method to display the size of the form. The first statement should read the value of the ActualWidth property of the form, convert it to a string, and assign this value to the Text property of the width text box. The second statement should read the value of the ActualHeight property of the form, convert it to a string, and assign this value to the Text property of the height text box.

The sizeChanged method should look exactly like this:

private void sizeChanged()

{

width.Text = this.ActualWidth.ToString(); height.Text = this.ActualHeight.ToString();

}

7.Locate the window1SizeChanged method.

This method runs whenever the size of the window changes when the application is running. Notice that this method calls the sizeChanged method to display the new size of the window in the text boxes.

8.On the Debug menu, click Start Without Debugging to build and run the project.

The form displays the two text boxes containing the values 305 and 155. These are the default dimensions of the form, specified when the form was designed.

9.Resize the form. Notice that the text in the text boxes changes to reflect the new size.

10.Close the form, and return to the Visual Studio 2008 programming environment.

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