472,328 Members | 1,076 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,328 software developers and data experts.

Closing a Non-Modal Dialog

I get a Null Reference Exception if I close a non-modal dialog (that is, a
form opened with Show()) when a selection is made from a ComboBox. The error
message refers to Unsafe Native Methods, but the code is 100% managed. The
exception is not thrown if the dialog was modal (opened with ShowDialog()) or
if the selection is made from, say, a ListBox.

I have included a simple example below.

I am using C#.NET 2003, Standard Edition.

Thanks,
Andrew.

// sample code

using System;
using System.Drawing;
using System.ComponentModel;
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.ComponentModel.Container components = null;
private System.Windows.Forms.Button button1;
private A.FormA A;

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//

}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
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 InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(56, 16);
this.button1.Name = "button1";
this.button1.TabIndex = 0;
this.button1.Text = "open A";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(184, 61);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void button1_Click(object sender, System.EventArgs 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.Collections;
using System.ComponentModel;
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.ComponentModel.Container components = null;

public FormA()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
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 InitializeComponent()
{
this.comboBox1 = new System.Windows.Forms.ComboBox();
this.SuspendLayout();
//
// 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.SelectedIndexChanged += new
System.EventHandler(this.comboBox1_SelectedIndexCh anged);
//
// FormA
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(232, 61);
this.Controls.Add(this.comboBox1);
this.Name = "FormA";
this.Text = "FormA";
this.ResumeLayout(false);

}
#endregion

private void comboBox1_SelectedIndexChanged(object sender,
System.EventArgs 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.NullReferenceException: Object reference not set to an instance of
an object.
at System.Windows.Forms.UnsafeNativeMethods.CallWindo wProc(IntPtr wndProc,
IntPtr hWnd, Int32 msg, IntPtr wParam, IntPtr lParam)
at System.Windows.Forms.NativeWindow.DefWndProc(Messa ge& m)
at System.Windows.Forms.Control.DefWndProc(Message& m)
at System.Windows.Forms.Control.WmCommand(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ComboBox.WndProc(Message& m)
at System.Windows.Forms.ControlNativeWindow.OnMessage (Message& m)
at System.Windows.Forms.ControlNativeWindow.WndProc(M essage& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr 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__b77a5c561934e089/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__b77a5c561934e089/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__b03f5f7f11d50a3a/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:

<configuration>
<system.windows.forms jitDebugging="true" />
</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.
*/

Nov 16 '05 #1
3 7908
Looks like a bug in the framework...
You can try the following as a workaround:

* Declare the PostMessage API function with DllImportAttribute
* Declare the WM_CLOSE message constant
* Call PostMessage with the form's handle and WM_CLOSE as the message in the
SelectedIndexChanged.

--
Sincerely,
Dmitriy Lapshin [C# / .NET MVP]
Bring the power of unit testing to the VS .NET IDE today!
http://www.x-unity.net/teststudio.aspx

"Andrew" <An****@discussions.microsoft.com> wrote in message
news:6B**********************************@microsof t.com...
I get a Null Reference Exception if I close a non-modal dialog (that is, a
form opened with Show()) when a selection is made from a ComboBox. The
error
message refers to Unsafe Native Methods, but the code is 100% managed. The
exception is not thrown if the dialog was modal (opened with ShowDialog())
or
if the selection is made from, say, a ListBox.

I have included a simple example below.

I am using C#.NET 2003, Standard Edition.

Thanks,
Andrew.

// sample code

using System;
using System.Drawing;
using System.ComponentModel;
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.ComponentModel.Container components = null;
private System.Windows.Forms.Button button1;
private A.FormA A;

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//

}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
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 InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(56, 16);
this.button1.Name = "button1";
this.button1.TabIndex = 0;
this.button1.Text = "open A";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(184, 61);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void button1_Click(object sender, System.EventArgs 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.Collections;
using System.ComponentModel;
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.ComponentModel.Container components = null;

public FormA()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
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 InitializeComponent()
{
this.comboBox1 = new System.Windows.Forms.ComboBox();
this.SuspendLayout();
//
// 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.SelectedIndexChanged += new
System.EventHandler(this.comboBox1_SelectedIndexCh anged);
//
// FormA
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(232, 61);
this.Controls.Add(this.comboBox1);
this.Name = "FormA";
this.Text = "FormA";
this.ResumeLayout(false);

}
#endregion

private void comboBox1_SelectedIndexChanged(object sender,
System.EventArgs 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.NullReferenceException: Object reference not set to an instance of
an object.
at System.Windows.Forms.UnsafeNativeMethods.CallWindo wProc(IntPtr wndProc,
IntPtr hWnd, Int32 msg, IntPtr wParam, IntPtr lParam)
at System.Windows.Forms.NativeWindow.DefWndProc(Messa ge& m)
at System.Windows.Forms.Control.DefWndProc(Message& m)
at System.Windows.Forms.Control.WmCommand(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ComboBox.WndProc(Message& m)
at System.Windows.Forms.ControlNativeWindow.OnMessage (Message& m)
at System.Windows.Forms.ControlNativeWindow.WndProc(M essage& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr 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__b77a5c561934e089/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__b77a5c561934e089/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__b03f5f7f11d50a3a/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:

<configuration>
<system.windows.forms jitDebugging="true" />
</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.
*/


Nov 16 '05 #2
Thanks, Dmitriy.
I had a look at the documentation Using MessageWindow (Smart Device
Projects) in which
PostMessage appears, but could not understand it because I do not have a
background in Windows API.
I assume that "communication between native Windows applications and managed
applications" is system level
management that the C# programmer should not normally have to deal with when
the code is all managed.
What happens if this is a bug in the framework ? How are bug fixes
distributed ?
Regards,
Andrew.

"Dmitriy Lapshin [C# / .NET MVP]" wrote:
Looks like a bug in the framework...
You can try the following as a workaround:

* Declare the PostMessage API function with DllImportAttribute
* Declare the WM_CLOSE message constant
* Call PostMessage with the form's handle and WM_CLOSE as the message in the
SelectedIndexChanged.

--
Sincerely,
Dmitriy Lapshin [C# / .NET MVP]
Bring the power of unit testing to the VS .NET IDE today!
http://www.x-unity.net/teststudio.aspx

"Andrew" <An****@discussions.microsoft.com> wrote in message
news:6B**********************************@microsof t.com...
I get a Null Reference Exception if I close a non-modal dialog (that is, a
form opened with Show()) when a selection is made from a ComboBox. The
error
message refers to Unsafe Native Methods, but the code is 100% managed. The
exception is not thrown if the dialog was modal (opened with ShowDialog())
or
if the selection is made from, say, a ListBox.

I have included a simple example below.

I am using C#.NET 2003, Standard Edition.

Thanks,
Andrew.

// sample code

using System;
using System.Drawing;
using System.ComponentModel;
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.ComponentModel.Container components = null;
private System.Windows.Forms.Button button1;
private A.FormA A;

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//

}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
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 InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(56, 16);
this.button1.Name = "button1";
this.button1.TabIndex = 0;
this.button1.Text = "open A";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(184, 61);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void button1_Click(object sender, System.EventArgs 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.Collections;
using System.ComponentModel;
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.ComponentModel.Container components = null;

public FormA()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
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 InitializeComponent()
{
this.comboBox1 = new System.Windows.Forms.ComboBox();
this.SuspendLayout();
//
// 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.SelectedIndexChanged += new
System.EventHandler(this.comboBox1_SelectedIndexCh anged);
//
// FormA
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(232, 61);
this.Controls.Add(this.comboBox1);
this.Name = "FormA";
this.Text = "FormA";
this.ResumeLayout(false);

}
#endregion

private void comboBox1_SelectedIndexChanged(object sender,
System.EventArgs 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.NullReferenceException: Object reference not set to an instance of
an object.
at System.Windows.Forms.UnsafeNativeMethods.CallWindo wProc(IntPtr wndProc,
IntPtr hWnd, Int32 msg, IntPtr wParam, IntPtr lParam)
at System.Windows.Forms.NativeWindow.DefWndProc(Messa ge& m)
at System.Windows.Forms.Control.DefWndProc(Message& m)
at System.Windows.Forms.Control.WmCommand(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ComboBox.WndProc(Message& m)
at System.Windows.Forms.ControlNativeWindow.OnMessage (Message& m)
at System.Windows.Forms.ControlNativeWindow.WndProc(M essage& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr 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__b77a5c561934e089/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__b77a5c561934e089/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__b03f5f7f11d50a3a/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:

<configuration>
<system.windows.forms jitDebugging="true" />
</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.
*/


Nov 16 '05 #3
Andrew,
I assume that "communication between native Windows applications and
managed
applications" is system level management that the C# programmer should not
normally have to deal with when the code is all managed.
You are right, but sometimes you have to have system-level workarounds.
What happens if this is a bug in the framework ? How are bug fixes
distributed ?
I am not 100% sure, but looks like this is indeed a bug. There are service
packs for both versions of the Framework, they are available either from
Windows Update or from the Microsoft .NET website.

--
Sincerely,
Dmitriy Lapshin [C# / .NET MVP]
Bring the power of unit testing to the VS .NET IDE today!
http://www.x-unity.net/teststudio.aspx

"Andrew" <An****@discussions.microsoft.com> wrote in message
news:8D**********************************@microsof t.com... Thanks, Dmitriy.
I had a look at the documentation Using MessageWindow (Smart Device
Projects) in which
PostMessage appears, but could not understand it because I do not have a
background in Windows API.
I assume that "communication between native Windows applications and
managed
applications" is system level
management that the C# programmer should not normally have to deal with
when
the code is all managed.
What happens if this is a bug in the framework ? How are bug fixes
distributed ?
Regards,
Andrew.

"Dmitriy Lapshin [C# / .NET MVP]" wrote:
Looks like a bug in the framework...
You can try the following as a workaround:

* Declare the PostMessage API function with DllImportAttribute
* Declare the WM_CLOSE message constant
* Call PostMessage with the form's handle and WM_CLOSE as the message in
the
SelectedIndexChanged.

--
Sincerely,
Dmitriy Lapshin [C# / .NET MVP]
Bring the power of unit testing to the VS .NET IDE today!
http://www.x-unity.net/teststudio.aspx

"Andrew" <An****@discussions.microsoft.com> wrote in message
news:6B**********************************@microsof t.com...
>I get a Null Reference Exception if I close a non-modal dialog (that is,
>a
> form opened with Show()) when a selection is made from a ComboBox. The
> error
> message refers to Unsafe Native Methods, but the code is 100% managed.
> The
> exception is not thrown if the dialog was modal (opened with
> ShowDialog())
> or
> if the selection is made from, say, a ListBox.
>
> I have included a simple example below.
>
> I am using C#.NET 2003, Standard Edition.
>
> Thanks,
> Andrew.
>
> // sample code
>
> using System;
> using System.Drawing;
> using System.ComponentModel;
> 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.ComponentModel.Container components = null;
> private System.Windows.Forms.Button button1;
> private A.FormA A;
>
> public Form1()
> {
> //
> // Required for Windows Form Designer support
> //
> InitializeComponent();
>
> //
> // TODO: Add any constructor code after InitializeComponent call
> //
>
> }
>
> /// <summary>
> /// Clean up any resources being used.
> /// </summary>
> protected override void Dispose( bool disposing )
> {
> if( disposing )
> {
> if (components != null)
> {
> components.Dispose();
> }
> }
> 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 InitializeComponent()
> {
> this.button1 = new System.Windows.Forms.Button();
> this.SuspendLayout();
> //
> // button1
> //
> this.button1.Location = new System.Drawing.Point(56, 16);
> this.button1.Name = "button1";
> this.button1.TabIndex = 0;
> this.button1.Text = "open A";
> this.button1.Click += new System.EventHandler(this.button1_Click);
> //
> // Form1
> //
> this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
> this.ClientSize = new System.Drawing.Size(184, 61);
> this.Controls.Add(this.button1);
> this.Name = "Form1";
> this.Text = "Form1";
> this.ResumeLayout(false);
>
> }
> #endregion
>
> /// <summary>
> /// The main entry point for the application.
> /// </summary>
> [STAThread]
> static void Main()
> {
> Application.Run(new Form1());
> }
>
> private void button1_Click(object sender, System.EventArgs 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.Collections;
> using System.ComponentModel;
> 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.ComponentModel.Container components = null;
>
> public FormA()
> {
> //
> // Required for Windows Form Designer support
> //
> InitializeComponent();
>
> //
> // TODO: Add any constructor code after InitializeComponent call
> //
> }
>
> /// <summary>
> /// Clean up any resources being used.
> /// </summary>
> protected override void Dispose( bool disposing )
> {
> if( disposing )
> {
> if(components != null)
> {
> components.Dispose();
> }
> }
> 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 InitializeComponent()
> {
> this.comboBox1 = new System.Windows.Forms.ComboBox();
> this.SuspendLayout();
> //
> // 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.SelectedIndexChanged += new
> System.EventHandler(this.comboBox1_SelectedIndexCh anged);
> //
> // FormA
> //
> this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
> this.ClientSize = new System.Drawing.Size(232, 61);
> this.Controls.Add(this.comboBox1);
> this.Name = "FormA";
> this.Text = "FormA";
> this.ResumeLayout(false);
>
> }
> #endregion
>
> private void comboBox1_SelectedIndexChanged(object sender,
> System.EventArgs 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.NullReferenceException: Object reference not set to an instance
> of
> an object.
> at System.Windows.Forms.UnsafeNativeMethods.CallWindo wProc(IntPtr
> wndProc,
> IntPtr hWnd, Int32 msg, IntPtr wParam, IntPtr lParam)
> at System.Windows.Forms.NativeWindow.DefWndProc(Messa ge& m)
> at System.Windows.Forms.Control.DefWndProc(Message& m)
> at System.Windows.Forms.Control.WmCommand(Message& m)
> at System.Windows.Forms.Control.WndProc(Message& m)
> at System.Windows.Forms.ComboBox.WndProc(Message& m)
> at System.Windows.Forms.ControlNativeWindow.OnMessage (Message& m)
> at System.Windows.Forms.ControlNativeWindow.WndProc(M essage& m)
> at System.Windows.Forms.NativeWindow.Callback(IntPtr 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__b77a5c561934e089/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__b77a5c561934e089/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__b03f5f7f11d50a3a/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:
>
> <configuration>
> <system.windows.forms jitDebugging="true" />
> </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.
> */
>



Nov 16 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: simon place | last post by:
Spent some very frustrating hours recoding to find a way of closing a server socket, i'd not thought it would be any problem, however, after...
7
by: Miktor | last post by:
At the minute I am using the following rather unwieldy function to prevent the console window in Dev C++ from closing before I can see the output...
3
by: ThunderMusic | last post by:
Hi, I'm trying to have a MSN Messenger like form/app closing behavior. When I click on the X button, I only want the form to disappear and when I...
102
by: Skybuck Flying | last post by:
Sometime ago on the comp.lang.c, I saw a teacher's post asking why C compilers produce so many error messages as soon as a closing bracket is...
7
by: Geoff Olding | last post by:
In VB.Net, What events (if any), does an MDI child form get when the application closes, which I can use to cancel the close? I am relying on the...
6
by: **Developer** | last post by:
I've been looking but can't find out how in a form Closing event to know if the closing is because the form's "X" had been clicked or the main...
5
by: lightzizo | last post by:
Hi All, How can I prevent the user from closing the window? I need some how to prevent the user from closing the window even if he tried to close...
7
by: Rich | last post by:
I have the following script on all pop up windows, would like to have this work on all newer browsers if possible. Right now it only works on MSIE...
4
by: Academic | last post by:
I read the Help and some of the many Google hits I got but can't find out the difference between MyBase.Closing and MyBase.FormClosing Can...
14
by: =?Utf-8?B?UHVjY2E=?= | last post by:
Hi, I'm using VS2005 and .net 2.0. I'm creating an application that has 3 forms. I want allow users to move forward and backward with the forms...
0
by: tammygombez | last post by:
Hey everyone! I've been researching gaming laptops lately, and I must say, they can get pretty expensive. However, I've come across some great...
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
1
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.