private int x, y; } Using Properties in a Windows Application When you use the Properties window in Microsoft Visual Studio 2005, you are actually generating code that sets and retrieves
Trang 1Declaring Interface Properties
You met interfaces in Chapter 12, “Working with Inheritance.” Interfaces can also
specify properties To do this, you declare 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 struct that implements this interface must implement the accessors 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 allows further derived classes to override the
implementations For example:
class ScreenPosition : IScreenPosition
{
public virtual int X
{
Trang 2get { }
set { }
}
public virtual int Y
{
get { }
set { }
}
}
NOTE
The example shows a class Remember that the virtual keyword is not valid in structs because you can't derive from structs; structs are implicitly sealed
You can also choose to implement a property by using the explicit interface
implementation syntax covered in Chapter 12 An explicit implementation of a property
is non-public and non-virtual (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 use the Properties window in Microsoft Visual Studio 2005, you are actually generating code that sets and retrieves the values of properties of various application components—items such as TextBox controls, Forms, and Button controls Some components have a large number of properties, although some properties are more
Trang 3commonly used than others You can modify many of these properties at runtime 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 Form class to create a simple application that continually displays the size of its main window, even when the window is resized You will also display the current form size in this form's caption
Use properties
1 Start Visual Studio 2005
2 Open the Properties project, located in the \Microsoft Press\Visual CSharp Step by Step\Chapter 14\Properties folder in your My Documents folder
3 On the Debug menu, click Start Without Debugging
The project builds and runs A Windows form displays two empty text boxes labeled Width and Height
In the program, the TextBox controls are called width and height TextBox
controls have many properties In this exercise, you will make use of the Text property, which specifies the text displayed in the text box
4 Close the form and return to the Visual Studio 2005 programming environment
5 Display the Form1.cs source file in the Code and Text Editor window Locate the first resize method containing the comment // to do
This method is called by the Form1 constructor You will use it to display the current size of the form in the width and height text boxes The initial width of the form is 232 pixels, and its height is 96 pixels
6 Add two statements to the resize method to display the size of the form The first statement should assign the string “232” to the Text property of the Width text box The second statement should assign the string “96” to the Text property of the Height text box
The resize method should look exactly like this:
private void resize()
{
width.Text = "232";
height.Text = "96";
}
7 On the Debug menu, click Start Without Debugging to build and run the project
Trang 4The Windows form displays the two text boxes containing the values 232 and 103 The set accessor of the TextBox.Text property causes the specified string to be displayed
8 Resize the form Notice that the text in the text boxes remains unchanged
The next step is to ensure that the text in the TextBox.Text properties always displays the current size of the form
9 Close the form
10 Display Form1 in the Designer View window
11 Click Properties to display the properties of the form, and then locate the Size property
Notice that the Size property actually contains two values—the width and the height of the form At runtime, you can read and write the Size property of a form The type of the Size property is a struct that contains these two values If you look
up the Size property of the Form class in the MSDN Library for Visual Studio
2005, you will see that the type of this property is rather confusingly also called Size:
struct Size
{
public int Height
{
get { }
set { }
}
public int Width
{
get { }
set { }
}
}
}
You can see that the Size struct itself exposes Width and Height as further
properties You read the Size.Width property to retrieve the current width of the form and Size.Height to retrieve the current height of the form
12 Display the source file Form1.cs in the Code and Text Editor window and return
to the resize method you modified earlier
Trang 513 Modify the two statements so that the value Size.Width for the form is assigned to the width.Text property and Size.Height is assigned to the height.Text property You will need to convert from an int to a string The easiest way to do this is to use the ToString method
The resize method should now look like this:
private void resize()
{
int w = this.Size.Width;
width.Text = w.ToString();
int h = this.Size.Height;
height.Text = h.ToString();
}
14 On the Debug menu, click Start Without Debugging to build and run the project
15 Resize the Windows form
As you resize, the text boxes change to display the changing size (The resize method is called whenever the form changes its size by using the form's Resize event Events are explained in Chapter 16, “Delegates and Events.”)
16 Close the form
Your final task is to display the size of the form in the form caption
17 Return to the resize method you edited earlier in the Form1.cs source file
18 Add two more statements to the resize method The first statement should use the string.Format method to create a single string containing the width and height of the form The second statement should write this string to the public Text property
of the form
The Text property represents the title in the caption of the form
NOTE
The string.Format static method is useful for building and formatting strings from other data types, such as numbers It operates in a similar manner to the
Console.WriteLine method, which formats strings for output to the screen
Your resize method should now look exactly like this:
private void resize()
{
int w = this.Size.Width;
Trang 6width.Text = w.ToString();
int h = this.Size.Height;
height.Text = h.ToString();
string s = string.Format("({0}, {1})", w, h);
this.Text = s;
}
19 On the Debug menu, click Start Without Debugging
The project builds and runs The caption now displays the size of the form and also changes as you resize the form
20 Close the form and return to the Visual Studio 2005 programming environment
• If you want to continue to the next chapter
Keep Visual Studio 2005 running and turn to Chapter 15
• If you want to exit Visual Studio.NET now
On the File menu, click Exit If you see a Save dialog box, click Yes