Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi friends
I have a custom MessageBox with this code:
C#
public partial class MyMsgBox : Form
    {
        static MyMsgBox newMessageBox;
        static string b_id;

        public MyMsgBox()
        {
            InitializeComponent();
        }

        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;
            newMessageBox.ShowDialog();
            return b_id;
        }

        
        private void btnOk_Click(object sender, EventArgs e)
        {
            b_id = "1";
            newMessageBox.Dispose();
            Application.Exit();            
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            b_id = "2";
            newMessageBox.Dispose();


        }

    }


and I call it in btn_click event:
C#
MyMsgBox.ShowBox("Exit?","Error","ok","cancel");


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 form.so if user click on "OK" button application will exit and if click on "Cancel" button, nothing happen.
How can I achieve this?
Regards
Posted
Updated 6-Oct-12 9:31am
v4
Comments
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?
FM7 6-Oct-12 3:45am    
Form_closing Event:
MyMsgBox.ShowBox("Exit?","Error","ok","cancel");
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

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

C#
private void My_FormClosing(object sender, FormClosingEventArgs e)
{
 MyMsgBox.ShowBox("Exit?","Error","ok","cancel");
 e.cancel;
}


add this method to this.formclosing event as handler

C#
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
 
Share this answer
 
v2
Comments
psychic6000 6-Oct-12 7:26am    
please rate it 5 star if it fixes your problem. :)
FM7 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.
FM7 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.

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