Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

.net

winforms

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

like image 289
Animesh Ghosh Avatar asked Aug 06 '13 18:08

Animesh Ghosh


People also ask

Which is the default MessageBox button?

The first button on the message box is the default button.

What is MessageBox class explain MessageBox () in detail?

A message box is a prefabricated modal dialog box that displays a text message to a user. You show a message box by calling the static Show method of the MessageBox class. The text message that is displayed is the string argument that you pass to Show.


2 Answers

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

like image 151
Sayse Avatar answered Sep 21 '22 11:09

Sayse


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
}
like image 44
Logarr Avatar answered Sep 20 '22 11:09

Logarr