1
votes

I've got a shell tray icon with an attached context menu. The problem I'm having is that calling ShowDialog() from a context menu Clicked handler does not result in a modal dialog.

It's easy to reproduce this with a default C# project. Simply add the following code to the Form1.cs file:

protected override void OnLoad(EventArgs e)
{
    base.OnLoad(e);

    ToolStripMenuItem contextMenuShowMsg = new System.Windows.Forms.ToolStripMenuItem();
    contextMenuShowMsg.Name = "contextMenuShowMsg";
    contextMenuShowMsg.Text = "Show MessageBox...";
    contextMenuShowMsg.Click += new System.EventHandler(this.contextMenuShowMsg_Click);

    ContextMenuStrip contextMenuStrip = new System.Windows.Forms.ContextMenuStrip(this.components);
    contextMenuStrip.Items.Add(contextMenuShowMsg);

    NotifyIcon notifyIcon = new NotifyIcon();
    notifyIcon.Text = "DlgTest";
    notifyIcon.Icon = SystemIcons.Application;
    notifyIcon.Visible = true;
    notifyIcon.ContextMenuStrip = contextMenuStrip;
}

private void contextMenuShowMsg_Click(object sender, EventArgs e)
{
    MessageBox.Show(this, "Test MessageBox");
}

If you build and run this, you will be able to get two message boxes on the screen by simply choosing the context menu item twice. Shouldn't this be modal? Replacing this with a call to ShowDialog() for another form results in the same non-modal behavior.

My best guess is that the NotifyIcon isn't specifically tied to the Form, as it would be in a typical Windows application. But I don't see any way of doing that.

Any ideas? Thanks in advance for any help!

2

2 Answers

0
votes

I would suggest doing two things before you attempt to display a modal message box:

  1. Make your icon's owner-window visible.
  2. Give it focus.

Once you've done that, the this in the MessageBox.Show becomes a legal "modality parent".

Heck, it even makes more sense that the message box will be displayed on top of whatever program generated it, right? That way, the user has some context for what the message box is about!

0
votes

You will need to keep track of activations of your system tray menu, and disable it when a dialog is open.