一、创建一个窗体,使用Form设计器,画成如下样子:

WINFORM学习手册——自定义提示框

WINFORM学习手册——自定义提示框

二、Form的重要属性:

1.配置FormBorderStyle为FixedDialog,这样,弹出框无法放大缩小

WINFORM学习手册——自定义提示框

2.将AcceptButton配置为确定按钮

WINFORM学习手册——自定义提示框

3.将CancelButton配置为取消按钮

WINFORM学习手册——自定义提示框

三、按钮配置:

1.将确认按钮的DialogResult属性配置为Yes

WINFORM学习手册——自定义提示框

2.将取消按钮的DialogResult属性配置为No

WINFORM学习手册——自定义提示框

四、修改上一章的MainForm.cs为如下代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WinFrom
{
    public class MainForm : Form//继承Form
    {

        private Button MyButton;//创建Button
        public MainForm()
        {
          
            this.FormClosed += CloseDialog;
            MyButton = new Button();//实例化Button
            MyButton.Left = 30;//设置控件离form左边的距离
            MyButton.Top = 130;//设置控件离form上边的距离
            MyButton.Width = 200;//设置控件的宽度
            MyButton.Text = "弹出对话框";//按钮显示名称
            MyButton.Click += ShowMessage;//按钮单机事件
            this.Controls.Add(MyButton);//将控件加入到form中
        }
        /// <summary>
        /// 弹出对话框
        /// </summary>
        /// <param name="sender">出发事件的对象</param>
        /// <param name="e">事件信息</param>
        private void ShowMessage(object sender, EventArgs e)
        {
            MessageBox.Show("这是最简单的弹出框", "弹出对话框");
        }
        /// <summary>
        /// 关闭提示
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CloseDialog(object sender, EventArgs e)
        {
            //创建我的提示框
            MyDialog dialog = new MyDialog();
            //注意提示框的返回值,是DialogResult这个枚举
            if (dialog.ShowDialog()== DialogResult.No)
            {
                ((System.ComponentModel.CancelEventArgs)e).Cancel = true;
            }
        }


    }
}

执行:

WINFORM学习手册——自定义提示框

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-05
  • 2021-05-27
  • 2022-12-23
  • 2021-04-27
  • 2021-11-03
猜你喜欢
  • 2021-12-06
  • 2022-12-23
  • 2021-09-14
  • 2022-12-23
  • 2021-06-02
  • 2022-12-23
  • 2021-07-10
相关资源
相似解决方案