How do I call a function in another form?

msellery

Member
Joined
Mar 19, 2003
Messages
17
I have two forms: Form1, and frmMain. Form1 is the sample code from VB .Net to get an icon to appear in the systray for your application. The following code is in Form1 and is used to unload the icon:

Public Sub ExitSelect(ByVal sender As Object, ByVal e As System.EventArgs)
called when the user selects the Exit context menu

hide the tray icon
notifyicon.Visible = False

close up
Me.Close()
End Sub

My main application is frmMain. When I close main, the systray icon remains. I need to be able to call ExitSelect from frmMain. Ive tried Form1.ExitSelect() but it complains about their not being an object reference.
 
Declare an instance of your Form1 and then you can use: Form1.ExitSelect()

Code:
Dim Form1 as new Form1
do whatever you have to...
Form1.ExitSelect()

Or did you already declare it?
 
I tried that, actually. Since Form1 is the systray icon itself kind of (its the form with the code to load the icon and the icons right-click menu) any time you do Dim Blah as new Form1, its going to create new instance of it and put a second icon in the tray.

Form1 is also the start-up form. Form1s OnLoad method contains code to load frmMain, my main application screen.
 
Oh I thought frmMain was the one that starts the app :D
Make a custom constructor in the frmMain, after Form1 loads the icon, declare the frmMain and pass in the instance of Form1 to frmMain and you can then call ExitSelect.
 
thanks for your help.. but I am new to VB (I have a better backround in Java) and Im not sure how to do any of that in VB. If its not difficult, could you paste code to make a custom constructor? Im using nearly all sample code at this point, this is a learning project that Im working on. Thanks a lot!
 
Try this:
Code:
dim somevariablename as Form1
Public Sub New(ByVal theform as Form1)
MyBase.New()
InitializeComponent()
now if you want to just call ExitSelect then just type
theform.ExitSelect()
or if you wish to use this instance somewhere else, make some 
variable that will be public to the class and assign this form to it.
somevariablename = theform
End Sub
 
Back
Top