Rect.Offset Method (Vector)


Moves the rectangle by the specified vector.

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

member Offset : 
        offsetVector:Vector -> unit


Type: System.Windows.Vector

A vector that specifies the horizontal and vertical amounts to move the rectangle.

Exception Condition

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.

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


.NET Framework
Available since 3.0
Return to top