Me

From Xojo Documentation

Method

Me 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).

Usage

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) 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 Me method.

The distinction is important.

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

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:

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:

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 Me method:

Me.Left = 100

Because Me 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.

fa-info-circle-32.png
When Me is used outside of a control event handler, it works the same as Self. But for clarity, you should limit your use of Me to only within event handlers.

Sample Code

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

Event Open()
Me.Text = "Hello"
End Event

Me can be use in place of a control's name inside any of its event handlers. For example, this example in the Open event of a ListBox sets the column widths and populates the header row:

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

The following example in the DropObject event handler of a ListBox adds a row for each item being dropped.

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 event handler of a Canvas changes the mouse pointer to an open hand.

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

The following code in the MouseDown event handler of an ImageWell creates a DragItem and enables dragging. It uses the DragItem constructor.

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

See Also

Self method; UserGuide:Me vs. Self topic