我正在尝试使用.NET Compact Framework 3.5在表单应用程序上实现自定义消息框(确定,取消)。我是如何实现它的?
答案 0 :(得分:4)
您需要实现自己的自定义表单并使用
访问它myForm.ShowDialog();
以下是DialogBoxes的指南,您可以按照this guide this guide创建自己的对话框。
但如果你只使用OK / Cancel按钮,那么MessageBox有什么问题?
答案 1 :(得分:4)
如果您正在使用ok和取消按钮的消息框之后,可以使用
MessageBox.Show(this, "Message", "caption", MessageBoxButtons.OKCancel);
如果您想要自定义的外观/感觉以及通常在消息框中看不到的任何按钮,那么您必须制作自己的表单才能显示
答案 2 :(得分:2)
一位同事和我想出了以下课程作为一种动态消息框。
以下是设计师代码:
/// <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
}