Click here to Skip to main content
13,089,690 members (50,781 online)
Rate this:
Please Sign up or sign in to vote.
See more:
hi friends
I have a custom MessageBox with this code:
public partial class MyMsgBox : Form
        static MyMsgBox newMessageBox;
        static string b_id;
        public MyMsgBox()
        public static string ShowBox(string txtMessage, string txtTitle, string txtOk, string txtcancel)
            newMessageBox = new MyMsgBox();
            newMessageBox.lblTitle.Text = txtTitle;
            newMessageBox.lblMassage.Text = txtMessage;
            newMessageBox.btnOk.Text = txtOk;
            newMessageBox.btnCancel.Text = txtcancel;
            return b_id;
        private void btnOk_Click(object sender, EventArgs e)
            b_id = "1";
        private void btnCancel_Click(object sender, EventArgs e)
            b_id = "2";


and I call it in btn_click event:

Now, it works just for button click event, however, when I use it for Form_closing Event and run the program and click on 'Red' close button of form, MyMessageBox will show. I expect that when the user clicks on "ok", the program should close and when the user clicks on "cancel", nothing should happen. But now clicking on any of the buttons closes the form, same for when I press Alt+F4.
note: i have 2 forms:one is MyMsgBox and second is program form.i want to run MyMsgBox when user tries to close the program if user click on "OK" button application will exit and if click on "Cancel" button, nothing happen.
How can I achieve this?
Posted 5-Oct-12 20:55pm
Updated 6-Oct-12 9:31am
OriginalGriff 6-Oct-12 3:16am
"it not work!!" is not a good description of the problem!
Try explaining in what way it "not work" - what did you expect to happen, and what actually did happen?
Use the "Improve question" widget to edit your question and provide better information.
Sandeep Mewara 6-Oct-12 3:33am
I don't see form_closing event code here. Where is it? If you are expecting a message on form close, where is the code related to it?
MP59 6-Oct-12 3:45am
Form_closing Event:
BillWoodruff 6-Oct-12 22:48pm
When you say: "I have a custom MessageBox:" do you mean that you are showing us somebody else's code you are trying to fix, or your own code ?

You need to tell us why you are using such a strange way of dealing with canelling or closing an application, for us to really help you. Your code example simply attempts to duplicate, poorly, the functionality that the built-in MessageBox object in .NET C# WinForms already provides.

What is your intention here; what value for the user of your application is added by this code ?

Your call to 'ShowBox returns a string which you never use: why is that ? If you never make use of a return value from a Method call, then set the Method return type to 'void.

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

make closing event handler which calls your msgbox but also cancels the closing event by default.

private void My_FormClosing(object sender, FormClosingEventArgs e)

add this method to this.formclosing event as handler

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

now inside method, if the user presses the cancel button, your closing event will stopped by the line "e.cancel", but if user clicks okay button, as you have coded, application will exit
psychic6000 6-Oct-12 7:26am
please rate it 5 star if it fixes your problem. :)
MP59 6-Oct-12 15:23pm
excuse me sir,
e.cancel; is not work!
BillWoodruff 6-Oct-12 22:42pm
direct soliciting a vote is not what QA comments, or posts, are for.
MP59 6-Oct-12 11:10am
Very good.Thanks
BillWoodruff 6-Oct-12 23:04pm
My vote of #1:

Your code shown here will cancel closing the Form no matter what button the user presses in the Form shown in the call to 'ShowBox !

This solution clearly reflects you do not understand the 'FormClosingEventArgs' parameter of the Form_Closing event, and understand that there are several other potential causes, besides a user of an Application, or Form, taking direct action in the UI to close a Form, or Application, that may close an entire Application.

In addition to your code not working, in this case: we do not have enough information, yet, from the OP, as to why they use such an elaborate way of handling a Form_Closing Event by using a second Form.

We do not know if the OP's scenario is one involving only one Main Form, or several other open Forms, etc.

Until we get more information from the OP about what they are trying achieve here, "solutions" are premature.
psychic6000 9-Oct-12 10:01am
@billwoodruff: i am not saying i am a master programmer or something, but .NET says if you call application.exit, it will not provoke any of the events (form.closing or closed etc). so thats the catch... despite the closing event is canceled or not, application will exit.
and i think asker is satisfied with the solution :)

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web01 | 2.8.170813.1 | Last Updated 6 Oct 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100