对Form_closing事件使用自定义MessageBox [英] Using a custom MessageBox For Form_closing Event

查看:194
本文介绍了对Form_closing事件使用自定义MessageBox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨朋友
我有一个使用以下代码的自定义MessageBox:

hi friends
I have a custom MessageBox with this code:

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();


        }

    }



我在btn_click事件中称呼它:



and I call it in btn_click event:

MyMsgBox.ShowBox("Exit?","Error","ok","cancel");



现在,它仅适用于按钮单击事件,但是,当我将其用于Form_closing事件并运行程序并单击表单的红色"关闭按钮时,将显示MyMessageBox.我希望当用户单击确定"时,该程序应关闭,而当用户单击取消"时,则不会发生任何事情.但是现在单击任何按钮都将关闭表单,与我按Alt + F4时相同.
注意:我有2种形式:一种是MyMsgBox,第二种是程序形式.我想在用户尝试关闭程序形式时运行MyMsgBox.因此,如果用户单击确定"按钮,应用程序将退出并如果单击取消"按钮,则没有任何反应.
我该如何实现?
关于



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

推荐答案

制作关闭事件处理程序,该处理程序调用您的msgbox,但默认情况下也会取消关闭事件.

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

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



将此方法添加为this.formclosing事件作为处理程序



add this method to this.formclosing event as handler

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



现在在方法中,如果用户按下取消"按钮,则关闭事件将在"e.cancel"行停止,但是如果用户单击确定"按钮(如编码),则应用程序将退出



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


这篇关于对Form_closing事件使用自定义MessageBox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆