Error when displaying a context menu

To all,

I'm writing this for a custom WinCE 5.0 device. This code was part of a .NET dll that worked fine when compiled in VS 2003. My company is moving to VS 2005 and this problem popped up in a DLL that we've been using for years,

This function is part of a custom NotifyIcon class that we wrote to put an icon in the Windows CE system tray and create a context menu when the user taps the icon. When compiled under VS 2003, everything works fine. When compiled under VS 2005, we get a runtime error when a user taps on the icon. Ive narrowed the error down to:

m_MenuForm.ContextMenu.Show(m_MenuForm,new System.Drawing.Point(0, 0));

Which should show the context menu after user tap. Instead, the program crashes with an error at that point.

So I have two questions:

#1. Is there a new way to put an icon in the system tray and use a context menu in .NET compact Framework 2.0 ?

#2. If there isn't, am I doing anything wrong in this section of code that is causing everything to crash?

Ryan

privatevoid ShowContextMenu()

{

if (m_PopupMenu !=null)

{

int iXpos = 0;

int iYpos = 0;

// Calculate the location of the context menu.

iXpos =Screen.PrimaryScreen.WorkingArea.Width - CalculateMenuWidth() - 35;

iYpos =Screen.PrimaryScreen.WorkingArea.Height - CalculateMenuHeight() + 10;

// Set the location of the form to display the context menu.

m_MenuForm.Location =new System.Drawing.Point(iXpos, iYpos);

m_MenuForm.ControlBox =false;

m_MenuForm.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;

m_MenuForm.Size =new System.Drawing.Size(1, 1);

m_MenuForm.ContextMenu = m_PopupMenu;

m_MenuForm.ContextMenu.Show(m_MenuForm,new System.Drawing.Point(0, 0));

}

}

[3033 byte] By [rmcbeth] at [2007-12-23]
# 1
What does the exception say? Where the ShowContextMenu() is called from?
AlexY at 2007-8-30 > top of Msdn Tech,Smart Device Development,.NET Compact Framework...
# 2

Ths ShowContextMenu() is being called from an event message from the system tray. All of this is wrapped in a custom NotifyIcon class. This is basically an application that sits in the system tray and acts as a keyboard wedge. When the icon is tapped, you get a menu to Exit, toggle the laser, and so on.

Like I said before, the menu pops up when compiled in 2003 under .NET 1.1, but the menu does not appear when compiled under VS2005 in .NET 2.0.

The exception is at System.Windows.Forms.ContextMenu.Show()

And Microsofy.WindowsCE.Forms.MessageWindow._WndProc()

Microsoft.AGL.Forms.EVL.EnterMainLoop();

It's almost as if this is trying to start another instance of the program instead of an instance of the menu.

Ryan

/// <summary>

/// Receive the messages from the Windows OS.

/// </summary>

protected override void WndProc(ref Microsoft.WindowsCE.Forms.Message msg)

{

// Check if the windows message is from the system tray.

if (msg.Msg == WM_NOTIFY_TRAY)

{

if((int)msg.LParam == WM_LBUTTONDOWN)

{

if ((int)msg.WParam == m_uID)

{

// Show the context menu if set.

notifyIcon.ShowContextMenu();

// Raise the event if clicked.

if (notifyIcon.Click != null)

{

notifyIcon.Click(notifyIcon, null);

}

}

}

}

}

rmcbeth at 2007-8-30 > top of Msdn Tech,Smart Device Development,.NET Compact Framework...