473,785 Members | 2,768 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Closing a Modeless Dialog

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.
Nov 16 '05 #1
0 3185

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

Similar topics

3
3421
by: Rikkert | last post by:
I can't get my custom-made modeless dialog box (Visual C++.net Win32) to respond to clicking the close button or pressing Alt-F4. I am using the IsDialogMessage to check if messages are for my dialog. However, my dialog procedure doesn't seem to get a WM_DESTROY message when I click the close button. Also, pressing Alt-F4 does not close my application when the dialog has focus (it does terminate when my main window or another child of...
3
8152
by: Andrew | last post by:
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.
6
3229
by: Gary Miller | last post by:
Does anyone know how to detect a modeless form on closing by the form that invoked the modeless form? form.Show();
2
3338
by: Legendary Pansy | last post by:
Hello, I'm trying to accomplish the impossible by trying to do something equivalent of this example found here http://www.c-sharpcorner.com/Code/2003/Dec/DialogTutorial.as Starting with "Listing 2 - Changing the constructor of the modeless dialog to accept the parent object:" line and go onwards is what I'm trying to do Basically, there is the main form, and then there's the modeless form. I want the modeless form act like a modal form,...
1
2435
by: andrew | last post by:
Hi there, I'm having a problem with a modeless form in my app. I have a main form in my app and a socket that waits on data from a server (I use BeginReceive/EndReceive for that) and when I receive certain data from the server I want to create a new modeless form to show some of that data. But after receiving the data from the socket the modeless dialog does not appear (it seems to be receiving no windows message to update itself,...
6
2602
by: sramruttun | last post by:
hi I open a pop up window using window.showModalDialog. How can I close the parent window after the pop up appears ?
2
3058
by: proit_123 | last post by:
I am working on a windows forms application and have the following requirement. · I need to display a modeless dialog from the main form. o This allows user to continue to work with the application. o For the sake of example, the user could launch the modeless dialog from Form A and navigate to a Form B in the main window. · When the modeless dialog is closed, I need to perform certain logic in the main form based on the form that is...
0
3893
by: Ralstoj | last post by:
Hi I am programing in Autocad with VB Autodesk have not given users access to new note function in Autocad CIVIL3d API. I am trying to work round this by creating notes using the sendkey command in Vb. I can get the add note dialog box to come up ok but it does not have focus as I believe it is a modeless dialog box as it gets focus when you move the mouse cursor over it. What I am after is a VB eample on how to send key strokes to the...
3
5169
by: DoB | last post by:
What is the preferred way to refresh modeless dialogs? I am using the modeless dialog that shows a progress of a sequential process. The dialog class has nonvisual properties containg the progress status data and the dialog window has a timer and refreshes on every tick, but it does not seem to work correctly. Maybe I am doing it all wrong... what is a suggested way of programing this, provided that I want to have one general dialog...
0
9480
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10325
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10091
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9950
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7499
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5381
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3646
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2879
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.