Export (0) Print
Expand All

Rect.Offset Method (Vector)

Updated: April 2011

Moves the rectangle by the specified vector.

Namespace:  System.Windows
Assembly:  WindowsBase (in WindowsBase.dll)

public void Offset(
	Vector offsetVector
)

Parameters

offsetVector
Type: System.Windows.Vector
A vector that specifies the horizontal and vertical amounts to move the rectangle.

ExceptionCondition
InvalidOperationException

This method is called on the Rect.Empty rectangle.

Calling this method on an empty rectangle (Rect.Empty) is not allowed.

Note that calling the Offset method will only have an effect if you can change the X and Y properties directly. Because Rect is a value type, if you reference a Rect object by using a property or indexer, you get a copy of the object, not a reference to the object. If you attempt to change X or Y on a property or indexer reference, a compiler error occurs. Similarly, calling Offset on the property or indexer will not change the underlying object.  If you want to change the value of a Rect that is referenced as a property or indexer, create a new Rect, modify its fields, and then assign the Rect back to the property or indexer. 

The following example shows how to use the Offset(Vector) method to change the position of a rectangle.


private Point offsetExample1()
{
    // Initialize new rectangle.
    Rect myRectangle = new Rect();

    // The Location property specifies the coordinates of the upper left-hand 
    // corner of the rectangle. 
    myRectangle.Location = new Point(10, 5);

    // Set the Size property of the rectangle with a width of 200
    // and a height of 50.
    myRectangle.Size = new Size(200, 50);

    // Create a vector to use to offset the position of the rectangle.
    Vector vector1 = new Vector(20, 30);

    // The Offset method translates this rectangle by the specified vector.
    // myRectangle location changed from 10,5 to 30,35.
    myRectangle.Offset(vector1);

    // This rectangle's location changed from 10,5 to 30,35.
    return myRectangle.Location;

}


.NET Framework

Supported in: 4, 3.5, 3.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Date

History

Reason

April 2011

Added remark about using Offset with properties and indexers.

Information enhancement.

Community Additions

ADD
Show:
© 2014 Microsoft