Creating a form with taskbar button problem

Guardian

Active member
Joined
Jan 19, 2005
Messages
44
Location
England
Programming Experience
5-10
Hi, I am trying to create a windows form application that has a notify icon. this part however is not the problem. The problem I am having, is that I don't want the form to be shown until a menu item on the notify icon is pressed.

When I try and do this, in the forms Load event, I set the form to hidden/non visible. This however creates the form, then hides it, which causes the user to see it on screen briefly.

another method I tried involved using a Sub Main to start the program (initialise my notify icon, not load the form) but this causes the application to load and then quit instantly.

does anyone have any ideas how I can achieve this? i have tried searching, the forum and google and haven't turned anything up.

thanks
 
in sub main:
VB.NET:
Expand Collapse Copy
dim frm As New Form1
frm.Visible=false
Application.Run(frm)

now the form should run with no flicker
 
thanks for the reply, but i have just tried your code, and it seems to ignore the visible = false part. the second the application hits Application.Run(frm) it shows the form, i have tried replacing .visible = false with .Hide() but that does not work either.

here is the code i have in my project:
VB.NET:
Expand Collapse Copy
Public Class Form1

    Shared Sub main()

        Dim frm As New Form1
        'frm.Visible = False
        frm.Hide()
        Application.Run(frm)

    End Sub
End Class
the form contains a notify control with an icon specified an nothing else. the startup is set to Sub Main.

any other ideas? thanks
 
could always use sub main, running in a loop to see if it should exit, and do everything from there, you'd have to build your own menu and everything as well as handle the notify icon too

i could make a quick demo sometime tomorrow to help
 
Hi, i played around with the code you suggested, and seem to have managed to create what im after. Im sure there is a better way, but as it works i shall stick with it :)

VB.NET:
Expand Collapse Copy
Public Class Form1

    Public bRunning As Boolean = True

    Shared Sub main()

        Dim frm As New Form1

        Dim n As New NotifyIcon
        n.Visible = True
        n.Icon = My.Resources.External_Firewire

        Dim mnu As New ContextMenu
        mnu.MenuItems.Add("Show Form", AddressOf frm.mnuShow_Click)
        mnu.MenuItems.Add("Exit Application", AddressOf frm.MnuExit_Click)

        n.ContextMenu = mnu

        Do
            Application.DoEvents()
        Loop While frm.bRunning = True

        n.Dispose()

    End Sub

    Public Sub mnuShow_Click(ByVal sender As Object, ByVal e As EventArgs)
        Me.Show()
    End Sub

    Public Sub mnuExit_Click(ByVal sender As Object, ByVal e As EventArgs)
        bRunning = False
    End Sub

End Class


thanks for the help.
 
actually that's close to what i was thinking, except you still used a form, i was thinking of doing it without a form and make my own class to house the code instead

you got the main idea of what i was talking about
 
actually, i have just noticed a problem with using this method, the CPU usage is very high when it is running (50%, but i am on dual core) compared to around 5% CPU when idling. I have also tried running the application's release mode exe rather than through the VS IDE.

back to the drawing board :(
 
Back
Top