Hello, When I try to run the following code It causes an Unhandled Exception. After much tweeking with the code I found if commented out the MessageBox.Show line the problem goes away! Unusually I have used MessageBox.Show statments in other catch{ } segments in other parts of the code with no problems. My question is does anyone know why its causing the exception?
(P.s Reports_Group_Chooser is a ComboBox)
The Code:
string GroupName= (string)Reports_Group_Chooser.SelectedItem;
byte[] ConfigBytes= new byte[]{};
try{
ConfigBytes= File.ReadAllBytes("Reports/"+ GroupName.ToLower() +".grp");
}catch{
MessageBox.Show("The file for this group is missing. Cannot continue.","File Error",MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
Reports_Group_Chooser.Items.RemoveAt(NewGroup);
Reports_Group_Chooser.SelectedIndex= 0;
}
The error (well most of it):
Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object at System.Windows.Forms.ComboBox.DropDownListBoxFinished () [0x00000] at (wrapper remoting-invoke-with-check) System.Windows.Forms.ComboBox:DropDownListBoxFinished () at System.Windows.Forms.ComboBox+ComboListBox.HideWindow () [0x00000] at System.Windows.Forms.ComboBox+ComboListBox.OnMouseUp (System.Windows.Forms.MouseEventArgs e) [0x00000] at System.Windows.Forms.Control.WmLButtonUp (System.Windows.Forms.Message& m) [0x00000] at System.Windows.Forms.Control.WndProc (System.Windows.Forms.Message& m) [0x00000] at System.Windows.Forms.ComboBox+ComboListBox.WndProc (System.Windows.Forms.Message& m) [0x00000] at System.Windows.Forms.Control+ControlWindowTarget.OnMessage (System.Windows.Forms.Message& m) [0x00000] at System.Windows.Forms.Control+ControlNativeWindow.WndProc (System.Windows.Forms.Message& m) [0x00000] at System.Windows.Forms.NativeWindow.WndProc (IntPtr hWnd, Msg msg, IntPtr wParam, IntPtr lParam) [0x00000] at System.Windows.Forms.XplatUIX11.DispatchMessage (System.Windows.Forms.MSG& msg) [0x00000] at System.Windows.Forms.XplatUI.DispatchMessage (System.Windows.Forms.MSG& msg) [0x00000] at System.Windows.Forms.Application.RunLoop (Boolean Modal, System.Windows.Forms.ApplicationContext context) [0x00000]
Any help appreciated Michael
UPDATE This is an example of a working MessageBox.Show in my code which does not cause an error:
GlobalConfig= new Dictionary<string, string>();
byte[] ConfigBytes= new byte[]{};
try{
ConfigBytes= System.IO.File.ReadAllBytes("Config.cfg");
}catch{
MessageBox.Show("Global ettings file does not exist. Cannot continue.","File Error",MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
GlobalConfig.Add("StoreNumber","");
GlobalConfig.Add("Error","Y");
}
Update update:
It seems the problem is just having the MessageBox.Show within a combobox event: The following code still presents the same error:
private void Reports_GroupChanged(object sender,EventArgs e){
MessageBox.Show("The file for this group is missing. Cannot continue.","File Error",MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}