Customize the message in the System.Windows.Forms.Form MessageBox

  public class MsgBox : System.Windows.Forms.Form
    {
        Label bodyLabel = new Label();        
        Button yesBtn = new Button();
        Button noBtn = new Button();
        public MsgBox()
        {
        }

        public MsgBox(string titleMsg, string bodyMsg, string yesMsg,string noMsg)
        {             
            this.ClientSize = new System.Drawing.Size(490, 150);
            this.Text = titleMsg;            

            yesBtn.Location = new System.Drawing.Point(111, 112);
            yesBtn.Size = new System.Drawing.Size(75, 23);
            yesBtn.Text = yesMsg;
            yesBtn.BackColor = Control.DefaultBackColor;
            yesBtn.Click += YesBtnClick;

            noBtn.Location = new System.Drawing.Point(311, 112);
            noBtn.Size = new System.Drawing.Size(75, 23);
            noBtn.Text = noMsg;
            noBtn.BackColor = Control.DefaultBackColor;
            noBtn.Click += NoBtnClick;

            bodyLabel.Location = new System.Drawing.Point(111, 50);
            bodyLabel.Text = bodyMsg;
            bodyLabel.Font = Control.DefaultFont;
            bodyLabel.AutoSize = true;

            this.BackColor = Color.White;
            this.ShowIcon = false;

            this.Controls.Add(noBtn);
            this.Controls.Add(yesBtn);
            this.Controls.Add(bodyLabel);
        }

      

        private void YesBtnClick(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Yes;
        }

        private void NoBtnClick(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.No;
        }
    }

 

 static void CustomizeMsgBoxDemo()
        {
            MsgBox msgBoxForm = new MsgBox(
            "Warning",
            "Is it right!",
            "Right!",
            "Wrong!"
            );
            msgBoxForm.FormBorderStyle = FormBorderStyle.FixedSingle;
            msgBoxForm.MaximizeBox = false;
            msgBoxForm.MinimizeBox = false;
            msgBoxForm.StartPosition = FormStartPosition.CenterParent;
            DialogResult dlgResult=msgBoxForm.ShowDialog();
            if(dlgResult==DialogResult.Yes)
            {
                MessageBox.Show("Right!");
            }
            else
            {
                MessageBox.Show("Wrong!");
            }
        }

 

Customize the message in the System.Windows.Forms.Form MessageBox_第1张图片

 

 

Copy and update from 

https://stackoverflow.com/questions/12704026/how-to-change-text-on-messagebox-buttons

 

WPF edition.

"WpfApp1.MsgBox"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp1"
        mc:Ignorable="d"
        Title="MsgBox" Height="300" Width="400">
    
        
            
            
            
            
            
        
        
            
            
            
            
            
        
        

.xaml.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace WpfApp1
{
    /// 
    /// Interaction logic for MsgBox.xaml
    /// 
    public partial class MsgBox : Window
    {
        public MsgBox()
        {
            InitializeComponent();
        }

        public MsgBox(string msgTitle,string msgBody,string yesMsg,string noMsg)
        {
            InitializeComponent();
            this.Title = msgTitle;
            this.bodyLabel.Content = msgBody;
            this.yesBtn.Content = yesMsg;
            this.noBtn.Content = noMsg;            
            WindowStartupLocation = WindowStartupLocation.CenterScreen;
            WindowStyle = WindowStyle.SingleBorderWindow;
            ResizeMode = ResizeMode.NoResize;
        }

        private void YesBtnClick(object sender, RoutedEventArgs e)
        {
            this.DialogResult = true;
        }

        private void NoBtnClick(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }
    }
}

invoke

 private void MsgDemo()
        {
            MsgBox msgBox = new MsgBox("Right or wrong?", "Is this description right?", "Right", "Wrong");
            bool? dlgResult = msgBox.ShowDialog();
            if(dlgResult==true)
            {
                MessageBox.Show("Right!");
            }
            else
            {
                MessageBox.Show("Wrong!");
            }
        }

 

你可能感兴趣的:(Customize the message in the System.Windows.Forms.Form MessageBox)