Rect.Offset Method (Double, Double)

Moves the rectangle by the specified horizontal and vertical amounts.

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

public void Offset(
	double offsetX,
	double offsetY
)

Parameters

offsetX
Type: System.Double

The amount to move the rectangle horizontally.

offsetY
Type: System.Double

The amount to move the rectangle vertically.

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(Double, Double) method to change the position of a rectangle.

private Point offsetExample2()
{
    // 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);

    // The Offset method translates this rectangle by the specified horizontal and  
    // vertical amounts.  
    // myRectangle location changed from 10,5 to 30,35.
    myRectangle.Offset(20,30);

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

}

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

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

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft