• 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!");
                }
            }
  • 相关阅读:
    ReverseFind的用法 ; 查找字符中最后一个字符
    sprintf_s() 、sprintf()和printf()区别和用法
    CString/string 区别及其转化
    CC++中strcat()函数
    C++中cstring.h和string.h的区别
    vs中CString的用法,以及所需的头文件
    头文件afx.h作用
    sprintf_s函数用法
    C++ format 函数
    C/C++ typedef用法
  • 原文地址:https://www.cnblogs.com/Fred1987/p/13495562.html
Copyright © 2020-2023  润新知