Problems accessing form from module

Can anyone help me? I am trying to access a form sub from a module. I have the code below but my messagebox does not seem to be triggered. Another question I would have is: if your function is not shared in a form, how else can you access it from the module?

Thanks!

HERE IS THE MODULE

Module themod

'try to access the form

Form123.staticmethod()

EndSub

EndModule

'HERE IS THE FORM

PublicClass Form123

SharedSub staticmethod()

MessageBox.Show("hello")

EndSub

PrivateSub Form123_Load(ByVal senderAs System.Object,ByVal eAs System.EventArgs)HandlesMyBase.Load

EndSub

EndClass

[2201 byte] By [project2n5e0o1] at [2007-12-24]
# 1
You're missing the Sub statement in the module, I assume it is a typo. I also don't see you calling it anywhere. Be sure to copy and paste the actual code you tried to use. Other than that, it should work.

To call a non-shared method in a form, you need a reference to the form instance. If you use VS2005, you can use My.Forms.Form123.YourMethodName().

nobugz at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 2

nobugz thanks for the reply. I am using VS 2002. I have put the code down below. I have a module that creates a systray application. Ideally I would like to hit the Form123_load on the class from the module so it will execute a listener for a hotkey.

I have a sub called staticmethod that does execute my messagebox but only if the login button is pressed (so the module must require an action). How can I have it access the form123_load sub from the module automitically when the program runs without an action placed upon the module?

Thank you!


Module Imports systray1.vbAccelerator.Components.HotKey


Public Class Form123
Inherits vbAccelerator.Components.HotKey.HotKeyForm

Shared Sub staticmethod()
MessageBox.Show("hello")

End Sub
Public Sub Form123_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

' add an event handler for hot key pressed (or could just use Handles)
AddHandler Me.HotKeyPressed, AddressOf hotKey_Pressed

' set the hotkey:
Dim htk As HotKey = New HotKey("My HotKey", Keys.Up, HotKey.HotKeyModifiers.MOD_CONTROL Or HotKey.HotKeyModifiers.MOD_SHIFT)
Me.HotKeys.Add(htk)

'this is a test
'show a messagebox:

End Sub

Private Sub Form123_Closed(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Closed
' remove the dynamic event handler
RemoveHandler Me.HotKeyPressed, AddressOf hotKey_Pressed
End Sub

Private Sub hotKey_Pressed(ByVal sender As System.Object, ByVal e As HotKeyPressedEventArgs)
' ensure form is shown:
Me.RestoreAndActivate()

'show a messagebox:
MessageBox.Show(Me, _
String.Format("HotKey Pressed:" + vbCrLf + "Name: {0} " + vbCrLf + "KeyCode: {1}" + vbCrLf + "Modifiers: {2}", _
e.HotKey.Name, _
e.HotKey.KeyCode.ToString(), _
e.HotKey.Modifiers.ToString()), _
".NET Hot Key Demonstration", _
MessageBoxButtons.OK, _
MessageBoxIcon.Information)
End Sub

End Class

Module systray
Private systray As New NotifyIcon()

Public Sub Main()

Dim mnu As New ContextMenu()
Dim mnuitem As New MenuItem()
Dim mnuitem2 As New MenuItem()

'First
mnuitem.Text = "Exit"
AddHandler mnuitem.Click, AddressOf EndApp
mnu.MenuItems.Add(mnuitem)
'second
mnuitem2.Text = "Login"
AddHandler mnuitem2.Click, AddressOf LoginApp
mnu.MenuItems.Add(mnuitem2)

systray.ContextMenu = mnu
systray.Icon = New Icon(Application.StartupPath & "\systray.ico")
systray.Visible = True

Application.Run()

'try to access the form
Form123.staticmethod()

End Sub


Private Sub LoginApp(ByVal sender As Object, ByVal e As EventArgs)
systray.Visible = True
Dim frmchklogininstance As New frmLogin()

frmchklogininstance.Show()
'try to access the form
Form123.staticmethod()
End Sub
End Module

Public Sub Main()

'try to access the form
Form123.staticmethod()

End Sub


End Module

Public Class Form123
Inherits vbAccelerator.Components.HotKey.HotKeyForm


Shared Sub staticmethod()
MessageBox.Show("hello")

End Sub
Public Sub Form123_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'ideally i would like to hit this sub
' MessageBox.Show("hello")
End Sub


End Class

project2n5e0o1 at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 3
That doesn't look good, you are trying to convert a console mode program into a Windows form program. You started this thread because that isn't working out well. I'd recommend you start from scratch but now pick the Windows Application template instead of the Console Application template. Put the code you now have in Sub Main in the form's Load event instead. Keep all the code and declarations in the form's code and you'll find out there's no need for Shared methods.

nobugz at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 4

Ah yes! Sorry. I am sure that looks like an extremely stupid mistake (which it is), I am an ASP.NET developer trying to create another desktop app (very hard for me to understand). You are right thank you so very much.

How do I make the form start without a form window (so my applicaiton just runs at systray?

Thanks so much for your help!

project2n5e0o1 at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 5

I figured it out via trial and error. My solution to hide the initail window is

Me.showintaskbar = false

Thanks for your help!

project2n5e0o1 at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic General...