Animesh Ghosh
Animesh Ghosh

Reputation: 341

How I create custom button in a messagebox in .net form application?

I'm trying to implement a custom messagebox (Ok,Cancel) using .NET Compact Framework 3.5 on Form Application. How I implement it?

Upvotes: 6

Views: 36039

Answers (3)

Jonesopolis
Jonesopolis

Reputation: 25370

You'll need to implement your own custom Form and access it with

myForm.ShowDialog();

Here's a guide to DialogBoxes and you can follow this guide this guide to create your own dialog box.

But if you're only using OK/Cancel buttons, what's wrong with MessageBox?

Upvotes: 5

Logarr
Logarr

Reputation: 2417

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
}

Upvotes: 5

Sayse
Sayse

Reputation: 43330

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

MessageBoxButton options

Upvotes: 6

Related Questions