473,395 Members | 1,639 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

MDI child, Overriden WndProc function and hidden child form

Hello:

I am encountering a very weird issue with MDI child, Overriden WndProc
function and hidden form.

Basically, the application has two forms, Form1(parent form),
Form2(Child form), Form2's WndProc method is overriden, if the message
is CLOSE message, just hide the form. I first initialize and show the
child form, then close it by clicking the Close(X) button, the
overriden WndProc method gets invoked, and the form is hidden. Then
from Form1(parent form), I explcitly call Form2.Close(), this time, the
overriden WndProc method is not called, instead, the flow goes to
Form2.Dispose() methid directly and clear all the resource. However, I
am expecting the overriden WndProc method should be invoked as well
when calling Form2.Close().

Interesting observation is that if I set form2.Visible = true before
calling Form2.Close(), this way, the overriden ProdWnc process would be
invoked.

COuld anyone shed a light on this issue? I am so frustrated with this
one.

Thanks a lot.

Below is the sample code for Form1 and Form2.
//
// FORM2
//

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Threading;

namespace WindowsApplication1
{
/// <summary>
/// Summary description for Form2.
/// </summary>
public class Form2 : System.Windows.Forms.Form
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

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

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

protected override void WndProc(ref Message m_)
{
if (m_.Msg == Win32.WM_CLOSE) // 0x0010
{
this.Hide();
}
else
{
base.WndProc(ref m_);
}
}

/// <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.components = new System.ComponentModel.Container();
this.Size = new System.Drawing.Size(300,300);
this.Text = "Form2";
// this.Closing += new
System.ComponentModel.CancelEventHandler(this.Form _Closing);
}
#endregion

}
}
//
// FORM1
//
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Diagnostics;
using System.Windows.Forms;

namespace WindowsApplication1
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
private System.Windows.Forms.Button button2;
private Form2 frm2;

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.button2 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(128, 80);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(136, 56);
this.button1.TabIndex = 0;
this.button1.Text = "CloseForm2";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(120, 184);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(144, 40);
this.button2.TabIndex = 1;
this.button2.Text = "Initialize";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);

}
#endregion

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

// protected override void WndProc(ref Message m_)
// {
// if (m_.Msg == Win32.WM_CLOSE) // 0x0010
// {
// this.Visible = false;
// MessageBox.Show("I am here");
// }
// else
// {
// base.WndProc(ref m_);
// }
// }

private void button1_Click(object sender, System.EventArgs e)
{
frm2.Close();
}

private void Form1_Load(object sender, System.EventArgs e)
{
this.IsMdiContainer = true;
}

private void button2_Click(object sender, System.EventArgs e)
{
if(frm2 == null)
{
frm2 = new Form2();
}

frm2.Text = "TestChild";
frm2.MdiParent = this;

frm2.Show();
frm2.Activate();
frm2.BringToFront();

}

}
}

Oct 18 '05 #1
0 2468

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

Similar topics

2
by: zobie | last post by:
In the parent of my MDI application I declare a child form private frmChild = null; which is to be activated on a menu click. In the click event I am using the following code: if (frmChild...
2
by: Guy Babbitt | last post by:
I have an MDI application that starts an instance of a child form at application start. I have an event handler on a combo box checking for the selected value to change. When the select value...
1
by: sshuangw | last post by:
Hello: I am encountering a werid problem, in my application, When I have hidden my form ("myform.Visible = false"), WndProc doesn't catch messages sent to the app. When the form is not hidden,...
4
by: Bonj | last post by:
Further to my last post, I have managed to get a child window to display. But its messages are routed to the same WNDPROC that the main window's messages are routed to - what is the way of...
4
by: mike2036 | last post by:
I'm having problems with the reliability of WndProc in my application. The application has a hidden form, which it instantiates in the Main() and then calls Application.Run(). There are cases...
0
by: sshuangw | last post by:
Hello: I am encountering a very weird issue with MDI child, Overriden WndProc function and hidden form. Basically, the application has two forms, Form1(parent form), Form2(Child form),...
5
by: midnight_use_only | last post by:
hi all, quick question, how do you submit a form to a parent window from a child popup window? i have the following and all online documentation *suggests* that it should work but it does NOT: ...
2
by: Matt Brown - identify | last post by:
Hello, I'm very new to this level of programming and .net in general. My background is VB. I am attempting to use the WebBrowser control to load a flash movie, and it is a known issue...
2
by: ewokspy | last post by:
I have read up a bit on WndProc. and I have the below in my code. But I have two issues. One it seems that the code only runs while the form is running code, so if I have the form on standby, hidden...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...

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.