Click here to Skip to main content
15,881,281 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
Hi All,

I am trying to get the parent form to close and its not working. If I send the exact same thing to say Notepad it works fine and closes the window but not in my solution. Here is my code:

C#
protected override void OnMouseClick(MouseEventArgs e)
{
    if (e.Button == MouseButtons.Left)
    {
        if (aaaRectClose.Contains(e.Location) && this.Parent != null)
        {            
            IntPtr frmHandle = (this.Parent is Form) ? this.Parent.Handle : 0;
            if(frmHandle > 0)
            {
                SendMessage(frmHandle, ((uint)0x0112), ((int)0xF060), 0);  
            }          
        }
    }
}

[DllImport("user32.dll")]
public static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, int wParam, int lParam);


Any ideas why this works for other applications but not my own? Any help or suggestions would be greatly appreciated!

Thank you!
Posted
Updated 23-Jun-11 19:29pm
v2

Look easy way

C#
public partial class Form2 : Form
    {
        private Form _ParentForm;           

        public Form2(Form form)
        {
            InitializeComponent();
            this._ParentForm = form;            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this._ParentForm.Close();
        }
    }


Now to use do this

C#
Form2 form = new Form2(this);
            form.Show();
 
Share this answer
 
Comments
Christian Graus 23-Jun-11 23:25pm    
Ideally, you should just use the Parent property to find the form in question, or use a delegate to tell it to close, not keep references to forms like this.
charles henington 23-Jun-11 23:38pm    
true but actually the correct way is to use ownedForm and owner like this

Form2 newForm = new Form2();
this.AddOwnedForm(newForm);
newForm.Show();

then on the exit call use

this.Owner.Close();

Check out the example Windows-Form-application by SAKryukov
Ok, after hours of checking MSDN and poking around System.Windows.Forms.Form using Telerik I finally have a working solution and here's what I got:

C#
if (aaaRectClose.Contains(e.Location) && aaaParentForm != null)
{
    if (aaaRectClose.Contains(e.Location) && this.Parent != null)
    {
        IntPtr frmHandle = (this.Parent is Form) ? this.Parent.Handle : IntPtr.Zero;
        if (frmHandle != IntPtr.Zero)
        {
            SendMessage(frmHandle, 16, 0, 0);
        }
    }
}


Basically here is what I was trying to accomplish: A custom control could be included in any form in the project. This control contained several "Virtual" buttons which where part of an image. aaaRectClose was a Rectangle that contained the bounds for the "Close" button. When pressed it needed to close the form as if pressing the "X" in the upper right corner of standard Windows forms. The reason for including this close button is some of the forms may be borderless.

Anyways thank you to everyone who read my question and proposed a solution your efforts have been appreciated.

The above code does work for anyone who may eventually have the same silly requirements thrown in their lap with someone higher than yourself demanding things that make no since to you. Otherwise simply issue the "this.Parent.Close()" statement as its much simpler.

Whilst I couldn't find or there is no documentation on "SendMessage(IntPtr, 16, 0, 0)" nor does it coincide with what is on MSDN it does work.

-Phil
 
Share this answer
 
v2
Comments
[no name] 24-Jun-11 1:53am    
Nice one. 5ed too.
charles henington 24-Jun-11 2:06am    
if it's a Control couldn't you just as easily use Control.FindForm();
PhillipFromAZ 24-Jun-11 2:20am    
Finding the form wasn't the issue I had the correct handle but for some reason the SendMessage wasn't working with the WParams and Msg code outlined in MSDN. Once I changed to 16,0,0 it worked... something to do with the way the Form.base parses the WndProc messages.
PhillipFromAZ 24-Jun-11 2:24am    
Thank you Ramalinga!
Dave Kreskowiak 24-Jun-11 10:39am    
I really hate to tell you this, but you did it the hard way. All you had to do was replace the handle and SendMessage code with:
<pre>
Form parentForm = (this.Parent is Form) ? this.Parent : null;
if (parentForm != null)
{
parentForm.Close();
}
</pre>
Or something very similar to it. You could even modify this to go up teh control chain to find the parent form in the case where your custom control is sitting inside another container, such as a Panel.
This makes no sense at all. Your parent form won't close while a child is open, and as you have the parent form, it's inside your app, why not just tell it to close, or tell the Application to close, instead of using SendMessage ? You should write obsfucated code like this only when you have to.
 
Share this answer
 
Comments
PhillipFromAZ 23-Jun-11 20:16pm    
Ok, this is a nested control inside of a form. Its a sort of generic control panel that is used in most of our forms. The portion in question is the close button (the button isn't a button per-si but a rectangle area). I recently got a suggestion from a co-worker to use sendmessage for compatibility issues. I originally had this.Parent.Close() and worked great.
TRK3 23-Jun-11 20:46pm    
There isn't any compatibility issue with this.Parent.Close() -- that would be the standard way to tell your parent to close...
charles henington 23-Jun-11 23:00pm    
What do You Mean "This makes no sense at all. Your parent form won't close while a child is open" Of Course it will you need to reevaluate that statement!!!
Christian Graus 23-Jun-11 23:24pm    
If your parent form is the main form, and it's really marked as the parent/owner, I don't believe it will close, when it's got a child form open.
PhillipFromAZ 23-Jun-11 23:03pm    
TRK3, thank you for your comment it appears as though I will need to go back to that as it seems as though I have stumped more than just myself. I just really hate to admit defeat.
<big>Application.Exit();</big>
 
Share this answer
 
Comments
PhillipFromAZ 23-Jun-11 23:01pm    
Charles, Application.Exit() is not the answer as its not intended to exit the application. Intention is just to close the parent form and continue processing in the normal way as if the user pressed the "X" button in the upper right hand of the window.
I just don't understand why it will close any other window I tell it, just not its parent window and yes it is a control and not a child form.
Any help?
[no name] 24-Jun-11 1:29am    
It won't help so i gave you 1 vote.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900