如何在.NET窗体应用程序中创建自定义按钮的消息框?

5

我正在尝试在.NET Compact Framework 3.5上的表单应用程序中实现自定义消息框(确定,取消)。我该如何实现?


2
你不能覆盖默认的消息框,你必须自己创建一个窗体。 - Sayse
你想要自定义什么?标准消息框中有什么不喜欢的地方吗? - Steve
@Sayse 重要的一点是,如果你想要程序在“对话框”关闭之前停止执行,类似于 MessageBox() 的工作方式,那么你需要使用 Form.ShowDialog()。如果你想让 Form.ShowDialog() 返回数据,你需要设置 Form.DialogResult 属性 - j.i.h.
@j.i.h. - 我已经对正确使用DialogResult进行了大量研究 :) - 此外,如果您使用ShowDialog,则应将声明包装在using块中。 - Sayse
一个好的例子:http://www.codeproject.com/Articles/327212/Custom-Message-Box-in-VC - Ghasem
3个回答

5

我和同事一起设计了以下类作为一种动态消息框。

这是设计者代码:

/// <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
}

需要解释。这个做了什么,标准的MessageBox选项没有做到的?最令人困惑的是,为什么你要费这么大劲,而代码似乎只支持标准的MessageBoxButtons - 我认为从标准消息框升级的第一步应该是更灵活地设置按钮上的文本。 - ToolmakerSteve
2
这个表单的主要目的是为了控制模态对话框在屏幕上的位置。内置的对话框无法移动而不使用Windows钩子。另外,由于它是一个自定义表单,它可以像任何其他表单一样进行设计修改,这也是问题的主要重点。 - Logarr

5

如果你需要一个带有“确定”和“取消”按钮的消息框,你可以使用以下代码:

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

如果您想要自定义外观和感觉,并添加任何通常不在消息框中看到的按钮,则必须制作自己的表单来显示。

MessageBoxButton选项


4

您需要实现自己的自定义表单,并使用以下方式访问它:

myForm.ShowDialog();

这是一个关于对话框的指南,你可以按照这个指南此指南创建自己的对话框。但如果你只使用确定/取消按钮,那么使用MessageBox有什么问题吗?

这是注释,而不是答案。 - Ehsan
4
我觉得它回答了“如何实施”的问题。 - Jonesopolis
请只返回翻译文本:但链接形式的答案并不好。答案应该是自给自足的。 - Ehsan
我倾向于同意@EhsanUllah的观点,因为第一部分只是一个链接,而第二部分显然是一条评论。 - Steve
Jonesy,仅包含链接的答案存在问题,如果链接失效,答案将变得无用。 - Sayse
显示剩余4条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接