5

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

Animesh Ghosh
  • 331
  • 8
  • 16
  • 28
  • 2
    You can't override the default messagebox, you have to make your own form – Sayse Aug 06 '13 at 18:12
  • What do you wish to customize? What don't you like in the standard messagebox? – Steve Aug 06 '13 at 18:20
  • @Sayse An important thing to note though is that you need to use [`Form.ShowDialog()`](http://msdn.microsoft.com/en-us/library/System.Windows.Forms.Form.ShowDialog.aspx) if you want execution to stop until the "dialog" closes, similar to how `MessageBox()` works. You set the [`Form.DialogResult` property](http://msdn.microsoft.com/en-us/library/system.windows.forms.form.dialogresult.aspx) if you want `Form.ShowDialog()` to return data. – j.i.h. Aug 06 '13 at 18:22
  • @j.i.h. - I've done a lot of research about [using dialogresult correctly](http://stackoverflow.com/questions/16846573/using-dialogresult-correctly) :) - Also If you use ShowDialog then you should wrap your declaration in a using block – Sayse Aug 06 '13 at 18:25
  • A good example: http://www.codeproject.com/Articles/327212/Custom-Message-Box-in-VC – Ghasem Sep 24 '14 at 07:35

3 Answers3

5

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

Sayse
  • 42,633
  • 14
  • 77
  • 146
5

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
}
Logarr
  • 2,120
  • 1
  • 17
  • 29
  • Needs explanation. What does this do, that the standard MessageBox options don't? And most puzzlingly, why are you going to this trouble, yet the code behind seems to support only the standard MessageBoxButtons -- I would think the first step up from standard message box would be more flexibility as to the text on the buttons. – ToolmakerSteve Mar 24 '15 at 14:08
  • 2
    The primary reason this form was made was so that we could control the location of a modal dialog on screen. The build in dialog cannot be moved without using Windows hooks. Also, because it's a custom form it can be modified design wise just like any other form, which was the main point of the question. – Logarr Mar 24 '15 at 16:25
4

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?

SleuthEye
  • 14,379
  • 2
  • 32
  • 61
Jonesopolis
  • 25,034
  • 12
  • 68
  • 112
  • this is not an answer, this should be comment – Ehsan Aug 06 '13 at 18:14
  • 4
    I feel like it answers 'how to implement it' – Jonesopolis Aug 06 '13 at 18:15
  • but link only answers are not good. Answers should be self sufficient in themselves – Ehsan Aug 06 '13 at 18:15
  • I tend to agree with @EhsanUllah because the first part is just a link and the second part is clearly a comment. – Steve Aug 06 '13 at 18:17
  • I halfway agree with you, I've added some more information to help OP – Jonesopolis Aug 06 '13 at 18:20
  • NO, all answers should be like this to encourage self-documenting after some guidelines provided in the answer. This way only, you'll remain with some knowledge, otherwise, people will just copy-paste and eventually adapt a little their code to their needs. – VasileF Aug 06 '13 at 18:23
  • Jonesy, the Problem with answers that are just a link, is the answer becomes unhelpful if the link breaks – Sayse Aug 06 '13 at 18:26
  • you simply wanted the MessageBoxButtons... got it. – Jonesopolis Aug 06 '13 at 18:57
  • @AnimeshGhosh: Actually, there are several button options you can pick from with the default MessageBox class. http://msdn.microsoft.com/en-us/library/system.windows.forms.messageboxbuttons.aspx – Logarr Aug 06 '13 at 19:00