Unable to access Cursor.Current.Position (Access of shared member....through an instance; qualifying expression will not be evaluated

356 Views Asked by At

I am trying to determine the current mouse position in a MouseHover event for a ToolStripMenuItem. I am using the Cursor.Current.Position property to accomplish this.

Here is the prospective code:

    Private Sub pbNotesFlag_MouseHover(sender As Object, e As EventArgs)
        pbNotesFlagToolTip.Location = New Point(Cursor.Position.X, Cursor.Current.Position.Y)   'New Point(pbNotesFlag.Location.X - 20, pbNotesFlag.Location.Y + 15)
        pbNotesFlagToolTip.Visible = True
        pbNotesFlagToolTip.BringToFront()
    End Sub

The problem is that I am getting the following error:

Access of shared member, constant member, enum member or nested type through an instance; qualifying types will not be evaluated.

After researching this online, I am finding that the prevailing view is that the Cursor reference should be fully qualified, so I changed the code as follows:

    Private Sub pbNotesFlag_MouseHover(sender As Object, e As EventArgs)
        pbNotesFlagToolTip.Location = New Point(System.Windows.Forms.Cursor.Current.Position.X, System.Windows.Forms.Cursor.Current.Position.Y)   'New Point(pbNotesFlag.Location.X - 20, pbNotesFlag.Location.Y + 15)
        pbNotesFlagToolTip.Visible = True
        pbNotesFlagToolTip.BringToFront()
    End Sub

Unfortunately, I'm still getting the same error.

I've tried changing the reference context; e.g., putting the reference in a module instead of a class. So far, I have been unable to resolve this error.

I'm not sure why this reference doesn't work. On the surface, this appears to be a textbook example of the correct usage (according to MSDN).

What am I missing here?

Any insights and/or suggestions will, of course, be greatly appreciated.

1

There are 1 best solutions below

0
jmcilhinney On

The first code didn't work because the form has a Cursor property and your use of that name was interpreted as that property - an instance - rather than the type. The second code didn't work because you used the type to get an instance and then used that instead of using the type directly. This:

System.Windows.Forms.Cursor.Current.Position

should have been this:

System.Windows.Forms.Cursor.Position

You can also omit the System because that namespace is already imported by default. Given my comment too, your code should be this:

 pbNotesFlagToolTip.Location = PointToClient(Windows.Forms.Cursor.Position)