Form closing event not firing in win C#

Gani_tpt 1,586 Reputation points
2021-02-10T15:20:06.677+00:00

when i right click the form, form will be closed but, event not firing. i don't know why...?

i am not able to paste the code. i have taken the screenshot. below code for your reference.

66631-image.png

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,309 questions
0 comments No comments
{count} votes

Accepted answer
  1. Castorix31 81,831 Reputation points
    2021-02-10T15:42:51.307+00:00

    Have you added :

    this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.frmMain_FormClosing);
    

    (automatically added when you double-click in the Designer)

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Karen Payne MVP 35,196 Reputation points
    2021-02-10T16:49:53.953+00:00

    Try the following

    Paste into form

    const int WM_CONTEXTMENU = 0x007B;  
    protected override void WndProc(ref Message m)  
    {  
        if (m.Msg == WM_CONTEXTMENU)  
        {  
            m.Result = IntPtr.Zero;  
            Close();  
        }  
        else  
        {  
            base.WndProc(ref m);  
        }  
    }  
    

    In the form constructor

    Type Closing += press TAB which should give you an event e.g. where I added a MessageBox

    66652-11111111111.png

    Once you have right clicked the MessageBox is shown then the form closes.

    0 comments No comments