A
Alex
In the code below, clicking on the button ButtonChangeCursor changes the form's
cursor to a WaitCursor. Clicking the button ButtonRestoreCursor changes the
form's cursor back to its original cursor. For the reasons I explain below I
wouldn't expect this code to behave as it does, so why does it work?
Public Class Form1
Dim OldCursor As Cursor
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
OldCursor = Me.Cursor
End Sub
Private Sub ButtonChangeCursor_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles ButtonChangeCursor.Click
Me.Cursor = Cursors.WaitCursor
End Sub
Private Sub ButtonRestoreCursor_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles ButtonRestoreCursor.Click
Me.Cursor = OldCursor
End Sub
End Class
My confusion derives from the statement "OldCursor = Me.Cursor" in the Form's
Load event. After this statement executes, isn't the OldCursor variable
associated with the same object instance as the one that Me.Cursor is
associated with? That's what I would expect as the Dim statement for OldCursor
simply declares an object variable, it doesn't create a Cursor object. So if
OldCursor and Me.Cursor are associated with the same object, wouldn't any
changes to Me.Cursor affect OldCursor too? That's what I would expect, but
that's not what happens.
cursor to a WaitCursor. Clicking the button ButtonRestoreCursor changes the
form's cursor back to its original cursor. For the reasons I explain below I
wouldn't expect this code to behave as it does, so why does it work?
Public Class Form1
Dim OldCursor As Cursor
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
OldCursor = Me.Cursor
End Sub
Private Sub ButtonChangeCursor_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles ButtonChangeCursor.Click
Me.Cursor = Cursors.WaitCursor
End Sub
Private Sub ButtonRestoreCursor_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles ButtonRestoreCursor.Click
Me.Cursor = OldCursor
End Sub
End Class
My confusion derives from the statement "OldCursor = Me.Cursor" in the Form's
Load event. After this statement executes, isn't the OldCursor variable
associated with the same object instance as the one that Me.Cursor is
associated with? That's what I would expect as the Dim statement for OldCursor
simply declares an object variable, it doesn't create a Cursor object. So if
OldCursor and Me.Cursor are associated with the same object, wouldn't any
changes to Me.Cursor affect OldCursor too? That's what I would expect, but
that's not what happens.