如何在.net表单应用程序的消息框中创建自定义按钮

本文关键字:创建 自定义 按钮 消息 net 表单 应用程序 | 更新日期: 2023-09-27 18:26:47

我正试图在Form Application上使用.NET Compact Framework 3.5实现一个自定义消息框(Ok,Cancel)。我如何实现它?

如何在.net表单应用程序的消息框中创建自定义按钮

如果您想要一个带有"确定"answers"取消"按钮的消息框,则可以使用

 MessageBox.Show(this, "Message", "caption", MessageBoxButtons.OKCancel);

如果你想要一个自定义的外观/感觉和任何你通常在消息框上看不到的按钮,那么你必须制作自己的表格来显示

MessageBoxButton选项

我和一位同事提出了以下类作为一种动态消息框。

这是设计师代码:

/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
    if (disposing && (components != null))
    {
        components.Dispose();
    }
    base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
    this.lblMessage = new System.Windows.Forms.Label();
    this.btnRight = new System.Windows.Forms.Button();
    this.btnLeft = new System.Windows.Forms.Button();
    this.SuspendLayout();
    // 
    // lblMessage
    // 
    this.lblMessage.AutoSize = true;
    this.lblMessage.Location = new System.Drawing.Point(12, 39);
    this.lblMessage.Name = "lblMessage";
    this.lblMessage.Size = new System.Drawing.Size(35, 13);
    this.lblMessage.TabIndex = 0;
    this.lblMessage.Text = "label1";
    // 
    // btnRight
    // 
    this.btnRight.Anchor = System.Windows.Forms.AnchorStyles.Bottom;
    this.btnRight.Location = new System.Drawing.Point(89, 73);
    this.btnRight.Name = "btnRight";
    this.btnRight.Size = new System.Drawing.Size(75, 23);
    this.btnRight.TabIndex = 1;
    this.btnRight.UseVisualStyleBackColor = true;
    // 
    // btnLeft
    // 
    this.btnLeft.Anchor = System.Windows.Forms.AnchorStyles.Bottom;
    this.btnLeft.Location = new System.Drawing.Point(8, 73);
    this.btnLeft.Name = "btnLeft";
    this.btnLeft.Size = new System.Drawing.Size(75, 23);
    this.btnLeft.TabIndex = 0;
    this.btnLeft.UseVisualStyleBackColor = true;
    // 
    // CustomMessageBox
    // 
    this.AcceptButton = this.btnLeft;
    this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
    this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
    this.AutoSize = true;
    this.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
    this.ClientSize = new System.Drawing.Size(170, 114);
    this.ControlBox = false;
    this.Controls.Add(this.btnLeft);
    this.Controls.Add(this.btnRight);
    this.Controls.Add(this.lblMessage);
    this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
    this.KeyPreview = true;
    this.MinimumSize = new System.Drawing.Size(176, 120);
    this.Name = "CustomMessageBox";
    this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
    this.Text = "CustomMessageBox";
    this.Load += new System.EventHandler(this.frmCustomMessageBoxLoad);
    this.ResumeLayout(false);
    this.PerformLayout();
}
#endregion
private System.Windows.Forms.Label lblMessage;
private System.Windows.Forms.Button btnRight;
private System.Windows.Forms.Button btnLeft;

这是表单背后的代码:

internal partial class CustomMessageBox : Form
{
    #region Fields
    public readonly MessageBoxButtons _buttons;
    #endregion
    //need to seal properties to override from derived class
    #region Constructors
    /// <summary>
    /// This constructor is required for designer support.
    /// </summary>
    public CustomMessageBox()
    {
        InitializeComponent();
    }
    public CustomMessageBox(string message, string title, MessageBoxButtons buttons)
    {
        InitializeComponent();
        Text = title;
        lblMessage.Text = message;
        _buttons = buttons;
    }
    #endregion
    #region Properties
    public override sealed string Text
    {
        get { return base.Text; }
        set { base.Text = value; }
    }
    #endregion
    #region private
    private void frmCustomMessageBoxLoad(object sender, EventArgs e)
    {
        lblMessage.Left = (ClientSize.Width - lblMessage.Width) / 2;
        switch(_buttons)
        {
            case MessageBoxButtons.OKCancel:
                {
                    btnLeft.Text = @"OK";
                    btnLeft.DialogResult = DialogResult.OK;
                    btnRight.Text = @"Cancel";
                    btnRight.DialogResult = DialogResult.Cancel;
                    AcceptButton = btnLeft;
                    break;
                }
            case MessageBoxButtons.OK:
                {
                    btnLeft.Text = @"OK";
                    btnLeft.DialogResult = DialogResult.OK;
                    btnRight.Hide();
                    btnLeft.Left = (ClientSize.Width - btnLeft.Width) / 2;
                    AcceptButton = btnLeft;
                    break;
                }
            case MessageBoxButtons.YesNo:
                {
                    btnLeft.Text = @"Yes";
                    btnLeft.DialogResult = DialogResult.Yes;
                    btnRight.Text = @"No";
                    btnRight.DialogResult = DialogResult.No;
                    AcceptButton = btnLeft;
                    break;
                }
            default :
                {
                    btnLeft.Hide();
                    btnRight.Hide();
                    break;
                }
        }
        AcceptButton = btnLeft;
    }
    #endregion
}

您需要实现自己的自定义表单并使用访问它

myForm.ShowDialog();

这是对话框指南,您可以按照本指南创建自己的对话框。

但是,如果您只使用"确定"/"取消"按钮,MessageBox有什么问题?