I get a Null Reference Exception if I close a modeless form (that is, a form
displayed using Show()) when a selection is made from a ComboxBox. If the
form is modal (displayed using ShowDialog()) or the selection is made from,
say, a ListBox, no exception is thrown.
I have included a simple example below. The error message refers to Unsafe
Native Methods, but the code is 100% managed. What is going on ?
I am using C#.NET 2003, Standard Edition.
Thanks,
Andrew.
// sample code
using System;
using System.Drawing;
using System.Componen tModel;
using System.Windows. Forms;
namespace Main
{
/// <summary>
/// Summary description for Form1.
/// </summary>
///
public class Form1 : System.Windows. Forms.Form
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.Componen tModel.Containe r components = null;
private System.Windows. Forms.Button button1;
private A.FormA A;
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeCompo nent();
//
// TODO: Add any constructor code after InitializeCompo nent call
//
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Disp ose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeCompo nent()
{
this.button1 = new System.Windows. Forms.Button();
this.SuspendLay out();
//
// button1
//
this.button1.Lo cation = new System.Drawing. Point(56, 16);
this.button1.Na me = "button1";
this.button1.Ta bIndex = 0;
this.button1.Te xt = "open A";
this.button1.Cl ick += new System.EventHan dler(this.butto n1_Click);
//
// Form1
//
this.AutoScaleB aseSize = new System.Drawing. Size(5, 13);
this.ClientSize = new System.Drawing. Size(184, 61);
this.Controls.A dd(this.button1 );
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayo ut(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run (new Form1());
}
private void button1_Click(o bject sender, System.EventArg s e)
{
A = new A.FormA();
A.Show(); // modeless dialog : callback function crashes when selection
made from ComboBox in form A.
//A.ShowDialog(); // modal case works.
}
}
}
using System;
using System.Drawing;
using System.Collecti ons;
using System.Componen tModel;
using System.Windows. Forms;
namespace A
{
/// <summary>
/// Summary description for FormA.
/// </summary>
public class FormA : System.Windows. Forms.Form
{
private System.Windows. Forms.ComboBox comboBox1;
/// <summary>
/// Required designer variable.
/// </summary>
private System.Componen tModel.Containe r components = null;
public FormA()
{
//
// Required for Windows Form Designer support
//
InitializeCompo nent();
//
// TODO: Add any constructor code after InitializeCompo nent call
//
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Disp ose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeCompo nent()
{
this.comboBox1 = new System.Windows. Forms.ComboBox( );
this.SuspendLay out();
//
// comboBox1
//
this.comboBox1. Items.AddRange( new object[] {
"1",
"2"});
this.comboBox1. Location = new System.Drawing. Point(88, 16);
this.comboBox1. Name = "comboBox1" ;
this.comboBox1. Size = new System.Drawing. Size(80, 21);
this.comboBox1. TabIndex = 0;
this.comboBox1. Text = "1";
this.comboBox1. SelectedIndexCh anged += new
System.EventHan dler(this.combo Box1_SelectedIn dexChanged);
//
// FormA
//
this.AutoScaleB aseSize = new System.Drawing. Size(5, 13);
this.ClientSize = new System.Drawing. Size(232, 61);
this.Controls.A dd(this.comboBo x1);
this.Name = "FormA";
this.Text = "FormA";
this.ResumeLayo ut(false);
}
#endregion
private void comboBox1_Selec tedIndexChanged (object sender,
System.EventArg s e)
{
this.Close(); // does not work if FormA is a modeless dialog.
//MessageBox.Show (" Closing..."); // has not yet crashed
}
}
}
// *** ERROR MESSAGE ***
See the end of this message for details on invoking
just-in-time (JIT) debugging instead of this dialog box.
************** Exception Text **************
System.NullRefe renceException: Object reference not set to an instance of
an object.
at System.Windows. Forms.UnsafeNat iveMethods.Call WindowProc(IntP tr wndProc,
IntPtr hWnd, Int32 msg, IntPtr wParam, IntPtr lParam)
at System.Windows. Forms.NativeWin dow.DefWndProc( Message& m)
at System.Windows. Forms.Control.D efWndProc(Messa ge& m)
at System.Windows. Forms.Control.W mCommand(Messag e& m)
at System.Windows. Forms.Control.W ndProc(Message& m)
at System.Windows. Forms.ComboBox. WndProc(Message & m)
at System.Windows. Forms.ControlNa tiveWindow.OnMe ssage(Message& m)
at System.Windows. Forms.ControlNa tiveWindow.WndP roc(Message& m)
at System.Windows. Forms.NativeWin dow.Callback(In tPtr hWnd, Int32 msg,
IntPtr wparam, IntPtr lparam)
************** Loaded Assemblies **************
mscorlib
Assembly Version: 1.0.5000.0
Win32 Version: 1.1.4322.2032
CodeBase: file:///c:/winnt/microsoft.net/framework/v1.1.4322/mscorlib.dll
----------------------------------------
Dummy2
Assembly Version: 1.0.1794.22479
Win32 Version: 1.0.1794.22479
CodeBase: file:///C:/Source/Dummy2/bin/Release/Dummy2.exe
----------------------------------------
System.Windows. Forms
Assembly Version: 1.0.5000.0
Win32 Version: 1.1.4322.2032
CodeBase:
file:///c:/winnt/assembly/gac/system.windows. forms/1.0.5000.0__b77 a5c561934e089/system.windows. forms.dll
----------------------------------------
System
Assembly Version: 1.0.5000.0
Win32 Version: 1.1.4322.2032
CodeBase:
file:///c:/winnt/assembly/gac/system/1.0.5000.0__b77 a5c561934e089/system.dll
----------------------------------------
System.Drawing
Assembly Version: 1.0.5000.0
Win32 Version: 1.1.4322.2032
CodeBase:
file:///c:/winnt/assembly/gac/system.drawing/1.0.5000.0__b03 f5f7f11d50a3a/system.drawing. dll
----------------------------------------
************** JIT Debugging **************
To enable just in time (JIT) debugging, the config file for this
application or machine (machine.config ) must have the
jitDebugging value set in the system.windows. forms section.
The application must also be compiled with debugging
enabled.
For example:
<configuratio n>
<system.windows .forms jitDebugging="t rue" />
</configuration>
When JIT debugging is enabled, any unhandled exception
will be sent to the JIT debugger registered on the machine
rather than being handled by this dialog.