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

 

 

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!");
            }
        }

 

原文链接: https://www.cnblogs.com/Fred1987/p/13495562.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍;

也有高质量的技术群,里面有嵌入式、搜广推等BAT大佬

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

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/401934

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年4月19日 上午9:30
下一篇 2023年4月19日 上午9:30

相关推荐