C# 通过新建Form自定义实现英文版弹窗

自己在写程序时,需要一个英文版的弹窗,即确认按钮为"OK"。在网上搜集了一些资料,整理出了适合自己需求的方法,其他有类似需求的可以参考:

public partial class CustomMessageBoxForm : Form
    {
        private Label messageLabel;
        private Button okButton;

        public CustomMessageBoxForm(string message)
        {
            InitializeComponent();
            InitializeControls(message);
        }

        private void InitializeControls(string message)
        {
            // 设置窗体属性
            this.FormBorderStyle = FormBorderStyle.FixedDialog;
            this.MaximizeBox = false;
            this.ClientSize = new Size(600, 150);
            this.StartPosition = FormStartPosition.CenterScreen;
            this.Text = "Message";

            // 设置消息文本label
            messageLabel = new Label();
            messageLabel.Text = message;
            messageLabel.Font = new Font("Segoe UI", 10, FontStyle.Regular);
            //messageLabel.AutoSize = false;
            messageLabel.Size = new Size(this.ClientSize.Width - 20, this.ClientSize.Height - 50);
            //messageLabel.TextAlign = ContentAlignment.MiddleCenter; // 设置文本居中对齐
            messageLabel.TextAlign = ContentAlignment.MiddleLeft;     // 设置文本左边对齐
            //messageLabel.Dock = DockStyle.Fill;                     // 填充整个窗体
            messageLabel.Location = new Point(20, 10);
            this.Controls.Add(messageLabel);                        //添加控件到窗体

            // 设置确认按钮
            okButton = new Button();
            okButton.Text = "OK";
            okButton.Font = new Font("Segoe UI", 10, FontStyle.Regular);
            okButton.Size = new Size(80, 25);
            okButton.Location = new Point(this.ClientSize.Width - okButton.Width - 30, this.ClientSize.Height - okButton.Height - 10);
            //okButton.Location = new Point(this.Width / 2 - okButton.Width / 2, 70);
            okButton.Click += OkButton_Click;
            this.Controls.Add(okButton);                            //添加控件到窗体
        }

        private void OkButton_Click(object sender, EventArgs e)
        {
            this.Close();                   //关闭窗口
            //Application.Exit();           //退出整个进程
        }

        public static void Show(string message)
        {
            using (CustomMessageBoxForm customMessageBox = new CustomMessageBoxForm(message))
            {
                customMessageBox.ShowDialog();
            }
        }
    }

主程序调用方法:CustomMessageBoxForm.Show();

显示效果(文本内容已打码):

 

  • 11
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值