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()
EndSubEndModule
'HERE IS THE FORM
Public
Class Form123SharedSub staticmethod()MessageBox.Show("hello")
EndSubPrivateSub Form123_Load(ByVal senderAs System.Object,ByVal eAs System.EventArgs)HandlesMyBase.Load
EndSub
EndClass
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