Method

# Me

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

## Description

<span class="title-ref">Me</span> refers to the control that owns (or contains) the current event handler on a Window. Outside an event handler on a Window (such as within a method), it refers to the current object (`Self</api/language/self_keyword>`).

## Usage

``` xojo
Me.MemberName
```

## Notes

When you refer to a property or method in your code without any sort of prefix, the one on the class itself (`Self</api/language/self_keyword>`) is used by default.

For example, for any code contained on a window or web page, Self refers to the window or web page.

However, within an event handler of a control, you may need to access a method or property of the control. For these cases you use the <span class="title-ref">Me</span> method.

The distinction is important.

For example, say you want to move a button by changing its Left property. In the Opening event handler for the button, the most obvious thing to write is this code:

``` xojo
Left = 100
```

But that does not do what you expect. Because there is no prefix, Self is implied. And Self refers to the window or web page. So the above code actually means this:

``` xojo
Self.Left = 100
```

Which sets the Left property of the window or web page and *not* the property of the button.

One way to get Left property of the button is to use the button name as the prefix like this:

``` xojo
Button1.Left = 100
```

This works, but it forces you to use the name of the Button to refer to the property. If you rename the button, then you will get a compile error. It also makes it more difficult to reuse the code on another button that might have a different name.

Instead, you should use the <span class="title-ref">Me</span> method:

``` xojo
Me.Left = 100
```

Because <span class="title-ref">Me</span> is used within an event handler for a control (the button in this case), it refers to the control. It is not affected should the name of the button changes and you can move this code to another button and it will work as is.

<div class="note">

<div class="title">

Note

</div>

When <span class="title-ref">Me</span> is used outside of a control event handler, it works the same as `Self</api/language/self_keyword>`. But for clarity, you should limit your use of <span class="title-ref">Me</span> to only within event handlers.

</div>

## Sample code

Code in the Opening event handler of a Label that sets its Text property:

``` xojo
Event Opening()
  Me.Text = "Hello"
End Event
```

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

<span class="title-ref">Me</span> can be use in place of a control's name inside any of its event handlers. For example, this example in the `Opening<desktopcontrol.opening>` event of a `DesktopListBox</api/user_interface/desktop/desktoplistbox>` sets the column widths and populates the header row:

``` xojo
Sub Opening()
  Me.ColumnCount = 5
  Me.ColumnWidths = "0,25%,25%,25%,25%"
  Me.HeadingAt(0) = "ID"
  Me.HeadingAt(1) = "FirstName"
  Me.HeadingAt(2) = "LastName"
  Me.HeadingAt(3) = "Phone"
  Me.HeadingAt(4) = "Zip"
End Sub
```

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

The following example in the `DropObject<desktopuicontrol.dropobject>` event handler of a `DesktopListBox</api/user_interface/desktop/desktoplistbox>` adds a row for each item being dropped.

``` xojo
Sub DropObject(obj As DragItem)
  Do
    If Obj.TextAvailable Then
      Me.AddRow(Obj.Text)
    End if
  Loop Until Not obj.NextItem
End Sub
```

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

The following example in the `MouseEnter<desktopuicontrol.mouseenter>` event handler of a `DesktopCanvas</api/user_interface/desktop/desktopcanvas>` changes the mouse pointer to an open hand.

``` xojo
Sub MouseEnter()
  Me.MouseCursor = System.Cursors.HandOpen
End Sub
```

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

The following code in the `MouseDown<desktopuicontrol.mousedown>` event handler of an `DesktopImageViewer</api/user_interface/desktop/desktopimageviewer>` creates a `DragItem</api/user_interface/desktop/dragitem>` and enables dragging. It uses the `DragItem constructor<dragitem.constructor0>`.

``` xojo
Function MouseDown(X As Integer, Y As Integer) As Boolean
  ' self refers to the containing window; me refers to the ImageWell.
  Var d As New DragItem(Self, Me.Left, Me.Top, Me.Width, Me.Height)
  d.Picture = Me.Image
  ' Allow the drag
  d.Drag
  Return True ' this overrides the default MouseDown behavior of the control.
End Function
```

## Compatibility

|                       |     |
|-----------------------|-----|
| **Project Types**     | All |
| **Operating Systems** | All |

<div class="seealso">

`Self</api/language/self_keyword>` method; `Me vs. Self</getting_started/object-oriented_programming/me_vs._self>` topic

</div>
