C# 如何在 .net 表单应用程序的消息框中创建自定义按钮?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18087444/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How I create custom button in a messagebox in .net form application?
提问by Animesh Ghosh
I'm trying to implement a custom messagebox (Ok,Cancel) using .NET Compact Framework 3.5 on Form Application. How I implement it?
我正在尝试在表单应用程序上使用 .NET Compact Framework 3.5 实现自定义消息框(确定,取消)。我如何实施它?
采纳答案by Sayse
If you are after a messagebox with ok and cancel buttons you can use
如果您正在使用带有确定和取消按钮的消息框,则可以使用
MessageBox.Show(this, "Message", "caption", MessageBoxButtons.OKCancel);
If you want a custom look/feel and any buttons that you don't normally see on messageboxes, then you have to make your own form to display
如果您想要自定义外观/感觉以及您通常不会在消息框上看到的任何按钮,那么您必须制作自己的表单来显示
回答by Jonesopolis
You'll need to implement your own custom Form and access it with
您需要实现自己的自定义表单并使用
myForm.ShowDialog();
Here's a guide to DialogBoxesand you can follow this guidethis guideto create your own dialog box.
这里有一个指导DialogBoxes,你可以按照本指南本指南创建自己的对话框。
But if you're only using OK/Cancel buttons, what's wrong with MessageBox?
但是如果您只使用确定/取消按钮,那么 MessageBox 有什么问题?
回答by Logarr
A co-worker and I came up with the following class to act as a sort of dynamic message box.
我和一个同事想出了以下类来充当一种动态消息框。
Here's the designer code:
这是设计器代码:
/// <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;
And here's the code behind the form:
这是表单背后的代码:
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
}