Click to See Complete Forum and Search --> : How can I hide a form without closing it??
ibeevbee
September 10th, 2002, 08:26 AM
I want to be able to minimize my VB.Net program to a task tray icon (clicking on the icon will then show it again) however in .Net VB as soon as you hide or set visible = false (or even take away the borders, make it size 0,0 and showintaskbar to false), it closes and the program ends.
I run the program from Sub Main, which initalises the program and then just does a form.showdialog.
The form has a NotifyIcon which I can get to display before I show the form, however as soon as I attempt to 'hide' the form by any means it closes it (and the notifyicon) and returns to Sub Main which then closes the application.
This was not a problem in VB 6 as you could manually load and unload forms.
Does anyone know how I can get around this problem in VB .Net??? :confused:
Thanks in advance. :)
DSJ
September 10th, 2002, 08:33 AM
FormVarName.Hide()
ibeevbee
September 10th, 2002, 08:56 AM
That would work in VB 6 (I presume), but in .Net that also closes the form down and returns control to the statement following the Form.ShowDialog, which in my case there is nothing and so it closes the program.
What I want it to do is hide the form but not return control to the statement following the .ShowDialog.
The form has a NotifyIcon on it, of which I want to use the Click event to re-show the hidden form. However, as soon as the form is hidden, the nofity icon goes with and kinda stuffs up that plan! :( .
Thinking about it, is there anyway I can get a NotifyIcon displayed without using a form, and then is there anyway to get VB to just sit there and wait instead of just closing the application when there is nothing to do / no form displayed???
I'm getting a headache!! :confused: :( :confused:
DSJ
September 10th, 2002, 11:08 AM
Sounds like your form variable is going out of scope. Try declaring it a the class level for example, instead of:
Class SomeClass
.
.
.
Private Sub SomeSub()
dim form2 as new form2()
form2.showdialg()
end sub
Try:
Class SomeClass
private form2 as form2()
.
.
.
Private Sub SomeSub()
form2 = new form2()
form2.showdialog()
end sub
ibeevbee
September 11th, 2002, 04:22 AM
Thanks for for you help, unfortunately I need to be able to reference the main form globally.
I have now worked out a way around my main problem, however this has left me with another problem!!
I've changed the program to startup from my main Form (rather than Sub Main in a module) and now call the initalisation code from the main form, this works well and I can now make the form invisible (leaving the task tray icon) without it unloading and ending the progam.
However, I still need to be able to reference this form globally - this is where I now come a cropper.
Since the form is created at startup (and not by me in the program) I have no idea how to reference it - does anyone know how to reference the startup form? :confused:
DSJ
September 11th, 2002, 08:06 AM
Read this article: http://msdn.microsoft.com/library/en-us/dv_vstechart/html/vbtchWorkingWithMultipleFormsInVisualBasicNETUpgradingToNET.asp
e_har
October 10th, 2002, 02:51 AM
ibeevbee,
i'm having a same problem as you. Can you please kindly send me your sample or reply through here how do i solve this problem.
My e-mail is as follows:e_har@excite.com
ibeevbee
October 10th, 2002, 05:31 AM
Hi e_har, posted it here as it may helps others too...
I did eventually solve the problem... :)
1st thing, do run start from Sub Main, use the form (call Sub Main from the Load event if need be).
Doing it this way will not end the application when you hide it.
The problem of referencing this startup form is overcome like this...
Create the following region after the Windows designer generated code on the startup form...
(This is copied from my code, so you will need to change the form name if yours is not called frmMain)
#Region "Default Instance"
'PURPOSE: Create a global reference to the startup form (this form)
Private Shared frmMainDefInstance As frmMain
Private Shared InitializingDefInstance As Boolean
Public Shared Property DefInstance() As frmMain
'Return reference to this form...
Get
If frmMainDefInstance Is Nothing OrElse frmMainDefInstance.IsDisposed Then 'form not created, create...
InitializingDefInstance = True
frmMainDefInstance = New frmMain()
InitializingDefInstance = False
End If
DefInstance = frmMainDefInstance
End Get
'Set default instance form ref...
Set(ByVal Value As frmMain)
frmMainDefInstance = Value
End Set
End Property
#End Region
Then, modify the designer generated code Sub New to set the default reference, eg, the bit in the *'s...
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'*****
'Create the default reference to this form (see region)
If frmMainDefInstance Is Nothing Then
frmMainDefInstance = Me
End If
'*****
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
You can now reference the default form using DefInstance, e.g. frmMain.DefInstance
Hopes this sorts out your problem!
ibeevbee
October 11th, 2002, 06:27 AM
Just noticed, at the beginning of the post above...
'do run start' should have been 'do NOT run start'!!!!
Sorry if it's confused anyone!
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.