Customized button caption in messagebox show
Is it possible to change the caption of buttons of messagebox (change
'Yes' into 'Save' and 'No' into 'Discard'), If yes, then How it can
possible and access the values of the clicked button ??
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Amit ChoudharyPosted Feb 11, 2011, 1:10 PM
Here is the class for creating the custom message box... modify the class little bit.. to get the exact message box you want...
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace CustomMessageBox
{
public partial class FMessageBox : Form
{
public FMessageBox()
{
InitializeComponent();
button1.Click += new EventHandler(button1_Click);
button2.Click += new EventHandler(button2_Click);
button3.Click += new EventHandler(button3_Click);
}
void button3_Click(object sender, EventArgs e)
{
ButtonClicked = EButtonClicked.button3;
Close();
}
void button2_Click(object sender, EventArgs e)
{
ButtonClicked = EButtonClicked.button2;
Close();
}
void button1_Click(object sender, EventArgs e)
{
ButtonClicked = EButtonClicked.button1;
Close();
}
public enum EButtonClicked
{
button1,
button2,
button3,
notSet
}
public EButtonClicked ButtonClicked = EButtonClicked.notSet;
public static EMessageReturn Show(string aCaption, string aMessage, EMessageButtons eMessageButtons)
{
FMessageBox fMessageBox = new FMessageBox();
fMessageBox.Text = aCaption;
fMessageBox.tbMessage.Text = aMessage;
if (eMessageButtons == EMessageButtons.RetryExit)
{
fMessageBox.button1.Text = "Exit";
fMessageBox.button2.Text = "Retry";
fMessageBox.button3.Visible = false;
fMessageBox.ShowDialog();
switch (fMessageBox.ButtonClicked)
{
case EButtonClicked.button1:
return EMessageReturn.Exit;
case EButtonClicked.button2:
return EMessageReturn.Retry;
}
}
return EMessageReturn.NotSet;
}
}
public enum EMessageButtons
{
RetryExit,
}
public enum EMessageReturn
{
Retry,
Exit,
NotSet
}
}
namespace CustomMessageBox
{
partial class FMessageBox
{
///
/// Required designer variable.
///
private System.ComponentModel.IContainer components = null;
///
/// Clean up any resources being used.
///
/// true if managed resources should be disposed; otherwise, false.
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
///
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///
private void InitializeComponent()
{
this.flowLayoutPanel1 = new System.Windows.Forms.FlowLayoutPanel();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.tbMessage = new System.Windows.Forms.TextBox();
this.flowLayoutPanel1.SuspendLayout();
this.SuspendLayout();
//
// flowLayoutPanel1
//
this.flowLayoutPanel1.Controls.Add(this.button1);
this.flowLayoutPanel1.Controls.Add(this.button2);
this.flowLayoutPanel1.Controls.Add(this.button3);
this.flowLayoutPanel1.Dock = System.Windows.Forms.DockStyle.Bottom;
this.flowLayoutPanel1.FlowDirection = System.Windows.Forms.FlowDirection.RightToLeft;
this.flowLayoutPanel1.Location = new System.Drawing.Point(0, 71);
this.flowLayoutPanel1.Name = "flowLayoutPanel1";
this.flowLayoutPanel1.Size = new System.Drawing.Size(294, 30);
this.flowLayoutPanel1.TabIndex = 0;
//
// button1
//
this.button1.Location = new System.Drawing.Point(216, 3);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.UseVisualStyleBackColor = true;
//
// button2
//
this.button2.Location = new System.Drawing.Point(135, 3);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(75, 23);
this.button2.TabIndex = 1;
this.button2.Text = "button2";
this.button2.UseVisualStyleBackColor = true;
//
// button3
//
this.button3.Location = new System.Drawing.Point(54, 3);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(75, 23);
this.button3.TabIndex = 2;
this.button3.Text = "button3";
this.button3.UseVisualStyleBackColor = true;
//
// tbMessage
//
this.tbMessage.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.tbMessage.BackColor = System.Drawing.SystemColors.Control;
this.tbMessage.BorderStyle = System.Windows.Forms.BorderStyle.None;
this.tbMessage.Location = new System.Drawing.Point(12, 12);
this.tbMessage.Multiline = true;
this.tbMessage.Name = "tbMessage";
this.tbMessage.Size = new System.Drawing.Size(269, 47);
this.tbMessage.TabIndex = 1;
//
// FMessageBox
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(294, 101);
this.Controls.Add(this.tbMessage);
this.Controls.Add(this.flowLayoutPanel1);
this.Name = "FMessageBox";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "FMessageBox";
this.flowLayoutPanel1.ResumeLayout(false);
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.FlowLayoutPanel flowLayoutPanel1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.TextBox tbMessage;
}
}
Please do not forget to mark "Do you like this post?" if it saves your time.
Thanks
Hirendra SisodiyaPosted Feb 11, 2011, 11:27 AM