C# 如何在.net表单应用程序的messagebox中创建自定义按钮?

C# 如何在.net表单应用程序的messagebox中创建自定义按钮?,c#,.net,winforms,C#,.net,Winforms,我正在尝试使用.NETCompactFramework3.5表单应用程序实现自定义messagebox(确定,取消)。如何实现它?您需要实现自己的自定义表单并使用 myForm.ShowDialog(); 这是一个指南,您可以按照它创建自己的对话框 但是,如果您只使用“确定/取消”按钮,MessageBox有什么问题吗?如果您在使用带有“确定”和“取消”按钮的MessageBox,您可以使用它 MessageBox.Show(this, "Message", "caption", Messa

我正在尝试使用.NETCompactFramework3.5表单应用程序实现自定义messagebox(确定,取消)。如何实现它?

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

myForm.ShowDialog();
这是一个指南,您可以按照它创建自己的对话框


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

如果您在使用带有“确定”和“取消”按钮的MessageBox,您可以使用它

 MessageBox.Show(this, "Message", "caption", MessageBoxButtons.OKCancel);
如果您想要自定义外观和MessageBox上通常看不到的任何按钮,那么您必须创建自己的表单来显示


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

以下是设计器代码:

/// <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;
//
///必需的设计器变量。
/// 
private System.ComponentModel.IContainer components=null;
/// 
///清理所有正在使用的资源。
/// 
///如果应处置托管资源,则为true;否则,错误。
受保护的覆盖无效处置(布尔处置)
{
if(处理和(组件!=null))
{
组件。Dispose();
}
基地。处置(处置);
}
#区域Windows窗体设计器生成的代码
/// 
///设计器支持所需的方法-不修改
///此方法的内容与代码编辑器一起使用。
/// 
私有void InitializeComponent()
{
this.lblMessage=new System.Windows.Forms.Label();
this.btnRight=new System.Windows.Forms.Button();
this.btnLeft=new System.Windows.Forms.Button();
这个.SuspendLayout();
// 
//LBL消息
// 
this.lblMessage.AutoSize=true;
this.lblMessage.Location=新系统.图纸.点(12,39);
this.lblMessage.Name=“lblMessage”;
this.lblMessage.Size=新系统图纸尺寸(35,13);
this.lblMessage.TabIndex=0;
this.lblMessage.Text=“label1”;
// 
//btnRight
// 
this.btnRight.Anchor=System.Windows.Forms.AnchorStyles.Bottom;
this.btnRight.Location=新系统图点(89,73);
this.btnRight.Name=“btnRight”;
this.btnRight.Size=新系统图纸尺寸(75,23);
this.btnRight.TabIndex=1;
this.btnRight.UseVisualStyleBackColor=true;
// 
//btnLeft
// 
this.btnLeft.Anchor=System.Windows.Forms.AnchorStyles.Bottom;
this.btnLeft.Location=新系统图点(8,73);
this.btnLeft.Name=“btnLeft”;
this.btnLeft.Size=新系统.图纸.尺寸(75,23);
this.btnLeft.TabIndex=0;
this.btnLeft.UseVisualStyleBackColor=true;
// 
//CustomMessageBox
// 
this.AcceptButton=this.btnLeft;
此.AutoScaleDimensions=新系统.Drawing.SizeF(6F,13F);
this.AutoScaleMode=System.Windows.Forms.AutoScaleMode.Font;
this.AutoSize=true;
this.AutoSizeMode=System.Windows.Forms.AutoSizeMode.growtandshrink;
this.ClientSize=新系统图纸尺寸(170114);
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;
此最小尺寸=新系统图纸尺寸(176120);
this.Name=“CustomMessageBox”;
this.StartPosition=System.Windows.Forms.FormStartPosition.CenterParent;
this.Text=“CustomMessageBox”;
this.Load+=new System.EventHandler(this.frmCustomMessageBoxLoad);
此选项为.resume布局(false);
这个。执行布局();
}
#端区
private System.Windows.Forms.Label lbl消息;
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
}
内部部分类CustomMessageBox:表单
{
#区域字段
公共只读MessageBoxButtons\u按钮;
#端区
//需要密封属性以从派生类重写
#区域构造函数
/// 
///设计器支持需要此构造函数。
/// 
公共CustomMessageBox()
{
初始化组件();
}
公共CustomMessageBox(字符串消息、字符串标题、MessageBox按钮)
{
初始化组件();
文本=标题;
lblMessage.Text=消息;
_按钮=按钮;
}
#端区
#区域属性
公共重写密封字符串文本
{
获取{return base.Text;}
set{base.Text=value;}
}
#端区
#地区私人
私有void frmCustomMessageBoxLoad(对象发送方,事件参数e)
{
lblMessage.Left=(ClientSize.Width-lblMessage.Width)/2;
开关(_按钮)
{
case MessageBoxButtons.ok取消:
{
btnLeft.Text=@“确定”;
btnLeft.DialogResult=DialogResult.OK;
btnRight.Text=@“取消”;
btnRight.DialogResult=DialogResult.Cancel;
AcceptButton=btnLeft;
打破
}
case MessageBoxButtons.OK:
{
btnLeft.Text=@“确定”;
btnLeft.DialogResult=DialogResult.OK;
btnRight.Hide();
btnLeft.Left=(ClientSize.Width-btnLeft.Width)/2;
AcceptButton=btnLeft;
打破
}
case MessageBoxButtons.YesNo:
{
btnLeft.Text=@“是”;
btnLeft.DialogResult=DialogResult.Yes;
btnRight.Text=@“否”;
btnRight.DialogResult=DialogResult.No;
AcceptButton=btnLeft;
打破
}
违约:
{
btnLeft.Hide();
btnRight.Hide();
打破
}
}
AcceptButton=btnLeft;
}
#端区
}
您不能执行以下操作