KeyPressed Event Crashes Application

Sep 2 2004 12:35 PM
Thanks in Advance for your help. I am writing an application in VS.NET2003 C#. I have two classes in the application. A main app class with the following code. *************************************************************************** namespace DataProcessing { /// /// Summary description for MainApp. /// public class MainApp { public static void Main() { Form1 f1 = new Form1(); Application.Run(); } } } **************************************************************************** public class Form1 : System.Windows.Forms.Form { #region Component Declarations NotifyIcon myTrayIcon= new NotifyIcon(); ContextMenu myMenu= new ContextMenu(); MenuItem mItem1 = new MenuItem(); #endregion public Form1() { mItem1.Text = "Open"; myMenu.MenuItems.Add(mItem1); mItem1.Click +=new EventHandler(mItem1_Click); myTrayIcon.Text = "Medex Data Processor"; myTrayIcon.Icon = new System.Drawing.Icon("C:\\QB.ico"); myTrayIcon.Visible = true; myTrayIcon.ContextMenu = myMenu; InitializeComponent(); // // TODO: Add any constructor code after InitializeComponent call // } } **************************************************************************** And of course the Form1 class directly above. In the constructor for the form class I have inserted a contextmenu and notifyIcon so the form can be made visible from the systemtray. I have also enabled a key capture on the form so when a user types "DEBUG" the form expands showing more functionality. The only problem is if you type DEBUG and then accidentally hit ENTER the application throws a NullReferenceException and brings up the error "Object is not set to the instance of and Object". I have tried catching the exception in the KeyPressed event on the form, but it doesn't seem to get there. I am wondering if I am running my application correctly from the separate class as above.

Answers (3)