Link to home
Start Free TrialLog in
Avatar of rbichon
rbichon

asked on

Open tray form when user double-clicks shortcut

I have an application that minimizes to the system tray. Sometimes my users forget the app is open if it is minimized and they try to double-click the shortcut to run the app. Is there a way to get the form to popup from the tray rather than starting a new instance of the app?
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Is the application yours?

If Yes, what version?  VB.Net 2003 or VB.Net 2005?

Avatar of rbichon
rbichon

ASKER

It is mine and is VB.Net 2005.

I was thinking of trying to use a HotKey. The problem is that I cannot get the app to sendkeys to the already opened app.
I'm glad you said VB.Net 2005...

.Net 2.0 has a new feature which allows you to communicate with previous instances:

Take a look at the StartUpNextInstance() event:
http://msdn2.microsoft.com/en-us/library/b9z4eyh8.aspx

Here is an overview:
http://msdn2.microsoft.com/en-us/library/w3xx6ewx.aspx

I'll post some sample code.
ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rbichon

ASKER

I can't check "Make single instance application". To do that, I have to have "Enable application framework" checked which forces me to use a form as my startup object. I am using a Sub Main in a module as my startup object. Is there another way?
Why are you using Sub Main()?...

Use the StartUp() event:
http://msdn2.microsoft.com/en-us/library/t4zch4d2(VS.80).aspx

It's accessed the same way as the StartupNextInstance() event.  The StartUp() event is called before any forms get loaded.

There are other ways but this is so simple with the Application Model!

What are you doing in Sub Main()?
Avatar of rbichon

ASKER

Nevermind. You were right on. I made a few adjustments that allow me to load the form instead of a module. I changed the code a little because I don't want the notify icon to disappear and I want the form to fade in. Here is the code:

Imports System.Threading

Namespace My

    Partial Friend Class MyApplication
        Private Sub MyApplication_StartupNextInstance(ByVal sender As Object, ByVal e As Microsoft.VisualBasic.ApplicationServices.StartupNextInstanceEventArgs) Handles Me.StartupNextInstance
            e.BringToForeground = True
            If (Form1.WindowState = FormWindowState.Minimized) Then
                'Display Form1 dialog if it is not already displayed.
                Form1.Opacity = 0
                If Form1.Visible = False Then
                    Form1.Visible = True
                End If
                Form1.Activate()
                Form1.WindowState = FormWindowState.Normal

                Application.DoEvents()

                Dim op As Integer
                For i As Integer = 1 To 10
                    Form1.Opacity = 0.1 * i
                    Thread.Sleep(10)
                Next
                op = Nothing
            Else
                Form1.Activate()
            End If
        End Sub
    End Class

End Namespace


Works like a charm!
=)

Glad you were able to implement it.

I have found some instances where Sub Main() has to be used...for instance, I haven't figured out how to start with my app with own implementation of the ApplicationContext Class without using Sub Main().  Sometimes the Application model is a love/hate thing.  ;)
Avatar of rbichon

ASKER

Is there a way to use Application.Exit() in the Startup() event? One of the things I do is to check a network drive for an updated version of the app and if it exists, to start another app called upgrade.exe and close the current app. The upgrade.exe app waits for my app to close and then replaces it with the updated version. Thanks.
There is actually a way to keep the main Form from showing (and thus make the application exit) by setting the e.Cancel property in the StartUp() event:

   "You can use the Cancel property of the e parameter to control the loading of an application's startup form. When the Cancel property is set to True, the startup form does not start."

        Private Sub MyApplication_Startup(ByVal sender As Object, ByVal e As Microsoft.VisualBasic.ApplicationServices.StartupEventArgs) Handles Me.Startup
            If someConidition Then
                e.Cancel = True
            End If
        End Sub
Avatar of rbichon

ASKER

Oh. That makes sense. I also found that Process.GetCurrentProcess.Kill() works. I'll go with your way because it seems like a more proper way to do it. Thanks again.