如何编写Windows窗体中的关闭按钮? [英] How to code the close button in the windows form?

查看:144
本文介绍了如何编写Windows窗体中的关闭按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我可以问一下是否有人知道如何在表格的关闭按钮中编写代码。



例如,如果用户点击关闭按钮(带有X的红色框),则显示消息.box:你确定退出是/否。



谢谢!

Hi Guys,

Can I ask if someone have an idea on how can I write a code in the close button in the form.

Example, if the user click the close button (the red box with X) sho message.box : are you sure to exit yes/NO.

Thanks!

推荐答案

处理FormClosing事件:

Handle the FormClosing event:
private void frmMain_FormClosing(object sender, FormClosingEventArgs e)
    {
    if (MessageBox.Show("Are you sure?", "Close", MessageBoxButtons.YesNo) == DialogResult.No)
        {
        e.Cancel = true;
        }
    }


private void Form1_FormClosing(object sender, FormClosingEventArgs e)
      {

          var str = MessageBox.Show("exit", "", MessageBoxButtons.YesNo).ToString();

          if (str == "Yes")
              this.Close();
          else
              e.Cancel = true;

      }


在FormClosing事件中添加以下代码



At the event of FormClosing add the following code

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (DialogResult.No == MessageBox.Show("are you sure to exit", "Alert", MessageBoxButtons.YesNo, MessageBoxIcon.Asterisk))
            {
                e.Cancel = true;
            }

        }


这篇关于如何编写Windows窗体中的关闭按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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