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!");
            }
        }
复制代码

 

 

 

Copy and update from 

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

 

WPF edition.

复制代码
<Window x:Class="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">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition/>
            <RowDefinition/>
            <RowDefinition/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition/>
            <ColumnDefinition/>
            <ColumnDefinition/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        <Label Grid.Row="2" Grid.Column="1" Grid.ColumnSpan="3" x:Name="bodyLabel" HorizontalAlignment="Left"/>
        <Button Grid.Row="3" Grid.Column="1" x:Name="yesBtn" Click="YesBtnClick"/>
        <Button Grid.Row="3" Grid.Column="3" x:Name="noBtn" Click="NoBtnClick"/>
    </Grid>
</Window>
复制代码

.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
{
    /// <summary>
    /// Interaction logic for MsgBox.xaml
    /// </summary>
    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!");
            }
        }
复制代码

 

posted @   FredGrit  阅读(143)  评论(0编辑  收藏  举报
努力加载评论中...
点击右上角即可分享
微信分享提示