CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Sep 2002
    Location
    Glos, UK
    Posts
    13

    How can I hide a form without closing it??

    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???

    Thanks in advance.

  2. #2
    Join Date
    Jun 2001
    Location
    MO, USA
    Posts
    2,868
    FormVarName.Hide()

  3. #3
    Join Date
    Sep 2002
    Location
    Glos, UK
    Posts
    13
    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!!

  4. #4
    Join Date
    Jun 2001
    Location
    MO, USA
    Posts
    2,868
    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

  5. #5
    Join Date
    Sep 2002
    Location
    Glos, UK
    Posts
    13
    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?

  6. #6
    Join Date
    Jun 2001
    Location
    MO, USA
    Posts
    2,868

  7. #7
    Join Date
    Sep 2002
    Location
    All around the World
    Posts
    99
    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:[email protected]

  8. #8
    Join Date
    Sep 2002
    Location
    Glos, UK
    Posts
    13
    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!

  9. #9
    Join Date
    Sep 2002
    Location
    Glos, UK
    Posts
    13
    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!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured