Click to See Complete Forum and Search --> : Using Form Methods from Shared Function!?!


WarMacre
02-21-2009, 05:42 AM
Hey all. This my first query on here (and everywhere else) as I'm fed-up of hitting brick walls with VB .NET.

Here's the issue. I have a Shared function. From within this function I want to get the 'desktopLocation' of the primary form (Form1). I thought I had done it but I keep getting '0' returned regardless of where the form resides.

The Code in question:

(This is within Form1)

Private Sub TimeBarSlot_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles lblTimeBarSlot.MouseDown
timeBar.Width = MouseLoc.getMouseX - timeBarSlot.Left
End Sub

And this is the function (within a class) that it calls:

Friend Class MouseLoc
Public frm As Form1 = New Form1

Shared Function getMouseLoc() As Point
Dim _me As MouseLoc = New MouseLoc
getMouseLoc = _me.frm.MousePosition
getMouseLoc.X -= _me.frm.DesktopLocation.X
getMouseLoc.Y -= _me.frm.DesktopLocation.Y
Return getMouseLoc
End Function

Shared Function getMouseX() As Integer
Dim _me As MouseLoc = New MouseLoc
getMouseX = _me.frm.MousePosition.X
getMouseX -= _me.frm.DesktopLocation.X
Return getMouseX
End Function

Shared Function getMouseY() As Integer
Dim _me As MouseLoc = New MouseLoc
getMouseY = _me.frm.MousePosition.Y
getMouseY -= _me.frm.DesktopLocation.Y
Return getMouseY
End Function
End Class

Essentially, what this is doing is getting the mouse position when I click on a label called "timeBarSlot" and then subtracts the desktop location of the Form (or Application) to give a location co-ordinate that is relative to the Form dimensions and not the desktop dimensions.

Any ideas of where I am going completely and splutterly wayward?

WarMacre
02-21-2009, 05:55 AM
Just to note, the above code DOES work when placed (and altered accordingly) within the confines of Form1. However, I wish to reuse a lot of functions and therefore need them to be independant, i.e. stand-alone, so it's the call-back to the Form that is failing.

RoughGuy
02-21-2009, 06:14 AM
Friend Class MouseLoc
Public frm As Form1 = New Form1


I guess this is the mistake. Since you are creating a new object to the form this will reset all the properties.... I guess im not wrong.

WarMacre
02-21-2009, 07:14 AM
You're right. It's so obvious it's embarressing. I've been at it too long.

Question is, how do I reference an already running Form?

This should be simple, I know. However, I've come not to expect this from
Microsoft. :P

joewmaki
02-23-2009, 02:48 PM
You're right. It's so obvious it's embarressing. I've been at it too long.

Question is, how do I reference an already running Form?

This should be simple, I know. However, I've come not to expect this from
Microsoft. :P


try passing the form as a parameter in your shared functions:

Friend Class MouseLoc

Shared Function getMouseLoc(frm as Form) As Point
Dim _me As MouseLoc = New MouseLoc
getMouseLoc = frm.MousePosition
getMouseLoc.X -= frm.DesktopLocation.X
getMouseLoc.Y -= frm.DesktopLocation.Y
Return getMouseLoc
End Function

Shared Function getMouseX(frm as Form) As Integer
Dim _me As MouseLoc = New MouseLoc
getMouseX = frm.MousePosition.X
getMouseX -= frm.DesktopLocation.X
Return getMouseX
End Function

Shared Function getMouseY(frm as Form) As Integer
Dim _me As MouseLoc = New MouseLoc
getMouseY = frm.MousePosition.Y
getMouseY -= frm.DesktopLocation.Y
Return getMouseY
End Function
End Class

Private Sub TimeBarSlot_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles lblTimeBarSlot.MouseDown
timeBar.Width = MouseLoc.getMouseX(Me) - timeBarSlot.Left
End Sub

WarMacre
02-23-2009, 10:03 PM
timeBar.Width = MouseLoc.getMouseX(Me) - timeBarSlot.Left

Thanks for the suggestion joewmaki, but I'm not overly happy with passing Form1 to the function, simply because I believe that this can be ascertained within the function, and thereby reducing the complexity of the function call to:

MouseLoc.getMouseLoc

I like this method, which is used with all the math functions, e.g. math.sqrt, etc.

So, how can the active form be referenced within a shared function without passing a variable of the form instance to it?

Is reflection the way to go here?????