Main Module, Dialog Box, and Notify Icon

joe_pool_is

Contributor
Joined
Jan 18, 2004
Messages
507
Location
Longview, TX [USA]
We've got an old application that has been modified quite a bit.

Originally, it was just a single form application that hid in the TaskBar Tray using a Notify Icon.

Later, we had to add a Custom Dialog Box to the project.

Now, it has been determined that the project would be best served by the Dialog Box only, and eliminate the main form.

So, the project is now a Console-like Application that starts with a Main Module. The Notify Icon had to be included with the Dialog Box, otherwise we weren't able to make it work.

The NotifyIcon has a Context Menu Strip and events for Click and Double Click.

When the Main Module starts, it creates a new instance of the Dialog Box, but does not show it.

When the Dialog Box is created, it creates and shows the Notify Icon.

If you are still reading this, here is the problem:
Unless the Dialog Box is being displayed (dialog1.ShowModal), all the Notify Icon will do is show its Balloon Message. The Click, Double Click, and Context Menu Strip does not react at all.

Any idea why? How would I get this to work?
 
It sounds like you are no longer running your application like a Windows Forms application. One of the biggest differences between a console application and a Windows Forms application (other than the latter having windows) is that a console application doesn't have a "message pump," the program's main loop that processes events and messages from Windows.

Without this message pump, your application may not be able to properly interact with Windows or the user (hence the problems you've been having with the NotifyIcon).

Since you are loading a Form right off the bat, why not just run your application like a normal Windows Forms application (except don't show the window until it's needed).

If you don't want to or can't do that, your best bet would be to create a message pump using a variant of that of a Windows Forms app, something like so:


C# Code[HorizontalRule]magic hidden text[/HorizontalRule]// Manages our program for us
static class Program
{
····static ApplicationContext appContext;

····[STAThread]
····static void Main() {
········// Set us up the program
········appContext = new ApplicationContext();
········Application.EnableVisualStyles();

········// Start the message pump
········Application.Run(appContext);
····}

····// Call this method to close the application
····public static void EndApplication() {
········// It might be a good idea to store a reference to your
········// dialog and notify icon in the program class and make
········// sure that they are disposed here.
········appContext.ExitThread();
····}
}
[HorizontalRule]Why are you quoting me?[/HorizontalRule]
 
You are a coding fool, Marble Eater. Are you really only 23?

Anyway, thanks for the tip! I was trying to get out of duplicating my Dialog Box onto a new form, but will probably solve my problem (and others that might creep in) easiest.
 
I am 23. It's not a lie. And while I try to figure out whether I was just insulted or complimented, hopefully you will get your program up and running.
 
Back
Top