How to Change the Button Text for 'Yes' and 'No' Buttons in the Messagebox.Show Dialog

How to change the button text for 'Yes' and 'No' buttons in the MessageBox.Show dialog?

Just add a new form and add buttons and a label. Give the value to be shown and the text of the button, etc. in its constructor, and call it from anywhere you want in the project.

In project -> Add Component -> Windows Form and select a form

Add some label and buttons.

Initialize the value in constructor and call it from anywhere.

public class form1:System.Windows.Forms.Form
{
public form1()
{
}

public form1(string message,string buttonText1,string buttonText2)
{
lblMessage.Text = message;
button1.Text = buttonText1;
button2.Text = buttonText2;
}
}

// Write code for button1 and button2 's click event in order to call
// from any where in your current project.

// Calling

Form1 frm = new Form1("message to show", "buttontext1", "buttontext2");
frm.ShowDialog();

Message Box Buttons Name Change possible in C#?

It is possible using a MessageBoxManager.dll, which you can get in the following link,

http://www.codeproject.com/Articles/18399/Localizing-System-MessageBox

Messagebox button text

You can't do it directly. Short of creating your own MessageBox, you could use Win32.SetWindowText() as described here (convert the VB.NET code here). If you don't want to have to deal with native functions, then a custom MessageBox will be the best option.

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

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

.Net MessageBoxButtons any way to change the Yes/No order?

No, there is nothing like that. Built-in message box functionality has only few moving parts: message text, title, help page and choice from predefined set of icons and button sets (not mentioning few other minor options).

If you want to suggest the user answer "No" as default one, go with Yes-No buttons and make No the default button using MessageBoxDefaultButton.Button2 (use this MessageBox.Show() overload). This is a standard in handling the need where answer No is preferred/recommended.

So based on your example, you can use

MessageBox.Show(@"Do you want to confirm?",
"Problem!",
MessageBoxButtons.YesNo,
MessageBoxIcon.Exclamation,
MessageBoxDefaultButton.Button2);

Further reading: User interface guidelines on confirmation dialogs at MSDN.

Message Box with Yes and No buttons

You can use this custom Message Box

SLMessageBox.xaml

<controls:ChildWindow x:Class="SLMessageBoxEX.SLMessageBox"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:controls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls"
xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
Title="SL Custom Message Box"
Width="320"
Height="150">
<controls:ChildWindow.Resources>
<Style x:Key="TextBoxStyle" TargetType="TextBox">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="TextBox">
<Grid x:Name="RootElement">
<ScrollViewer x:Name="ContentElement"
Background="{TemplateBinding Background}"
BorderThickness="0"
Padding="{TemplateBinding Padding}" />
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</controls:ChildWindow.Resources>
<controls:ChildWindow.Style>
<StaticResource ResourceKey="AlertBoxStyle" />
</controls:ChildWindow.Style>

<Grid x:Name="LayoutRoot" Margin="2">
<Grid.RowDefinitions>
<RowDefinition Height="80" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>

<TextBox x:Name="txtMsg"
Width="195"
Margin="59,22,0,22"
HorizontalAlignment="Left"
VerticalAlignment="Center"
Background="#02FFFFFF"
BorderBrush="{x:Null}"
Cursor="Arrow"
FontSize="11"
FontWeight="SemiBold"
IsReadOnly="True"
SelectionBackground="#FF727272"
Style="{StaticResource TextBoxStyle}"
Text="SysInformation Healthcare India Pvt Ltd."
TextAlignment="Center"
TextWrapping="Wrap" />
<StackPanel Grid.Row="1"
HorizontalAlignment="Center"
Orientation="Horizontal">
<Button x:Name="btnYes"
Width="65"
Height="23"
Margin="10,0,5,0"
Click="OKButton_Click"
Content="Yes"
Style="{StaticResource ButtonStyle1}" />
<Button x:Name="btnNo"
Width="65"
Height="23"
Margin="5,0,5,0"
Click="btnNo_Click"
Content="No"
Style="{StaticResource ButtonStyle1}" />
<Button x:Name="btnCancel"
Width="65"
Height="23"
Margin="5,0,5,0"
Click="CancelButton_Click"
Content="Cancel"
Style="{StaticResource ButtonStyle1}" />
</StackPanel>

<Image x:Name="imgIcon"
Width="45"
Height="40"
Margin="10,20,0,20"
HorizontalAlignment="Left"
VerticalAlignment="Center">
<Image.Effect>
<DropShadowEffect Color="#FF434343" />
</Image.Effect>
</Image>

</Grid>

SLMessageBox.xaml.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace SLMessageBoxEX
{
public partial class SLMessageBox : ChildWindow
{
public delegate void MessageBoxClosedDelegate(MessageBoxResult result);
public event MessageBoxClosedDelegate OnMessageBoxClosed;
public MessageBoxResult Result { get; set; }

public SLMessageBox(string title, string message, MessageBoxButtons buttons,MessageBoxIcon icon)
{
InitializeComponent();
this.Closed += new EventHandler(MessageBoxChildWindow_Closed);

this.Title = title;
this.txtMsg.Text = message;
DisplayButtons(buttons);
DisplayIcon(icon);
}

public enum MessageBoxButtons
{
Ok, YesNo, YesNoCancel, OkCancel
}

public enum MessageBoxIcon
{
Question, Information, Error, None, Warning, Logout, ThankYou
}

private void DisplayIcon(MessageBoxIcon icon)
{
switch (icon)
{
case MessageBoxIcon.Error:
imgIcon.Source = new BitmapImage(new Uri("/SLMessageBox;component/Images/error.png", UriKind.Relative));
break;

case MessageBoxIcon.Information:
imgIcon.Source = new BitmapImage(new Uri("/SLMessageBoxEX;component/Images/Information.png", UriKind.Relative));
break;

case MessageBoxIcon.Question:
imgIcon.Source = new BitmapImage(new Uri("/SLMessageBoxEX;component/Images/SIHIImages/question.png", UriKind.Relative));
break;

case MessageBoxIcon.Warning:
imgIcon.Source = new BitmapImage(new Uri("/SLMessageBoxEX;component/Images/warning.png", UriKind.Relative));
break;

case MessageBoxIcon.None:
imgIcon.Source = new BitmapImage(new Uri("/SLMessageBox;component/Images/Information.png", UriKind.Relative));
break;

case MessageBoxIcon.Logout:
imgIcon.Source = new BitmapImage(new Uri("/SLMessageBoxEX;component/Images/logout1.png", UriKind.Relative));
break;

case MessageBoxIcon.ThankYou:
imgIcon.Source = new BitmapImage(new Uri("/SLMessageBoxEX;component/Images/ThankYou.png", UriKind.Relative));
break;
}
}

private void DisplayButtons(MessageBoxButtons buttons)
{

switch (buttons)
{
case MessageBoxButtons.Ok:
btnCancel.Visibility = Visibility.Collapsed;
btnNo.Visibility = Visibility.Collapsed;
btnYes.Visibility = Visibility.Visible;
btnYes.Content = "Ok";
break;

case MessageBoxButtons.OkCancel:
btnCancel.Visibility = Visibility.Visible;
btnNo.Visibility = Visibility.Collapsed;
btnYes.Visibility = Visibility.Visible;
btnYes.Content = "Ok";
break;

case MessageBoxButtons.YesNo:
btnCancel.Visibility = Visibility.Collapsed;
btnNo.Visibility = Visibility.Visible;
btnYes.Visibility = Visibility.Visible;
break;

case MessageBoxButtons.YesNoCancel:
btnCancel.Visibility = Visibility.Visible;
btnNo.Visibility = Visibility.Visible;
btnYes.Visibility = Visibility.Visible;
break;
}
}

private void MessageBoxChildWindow_Closed(object sender, EventArgs e)
{
if (OnMessageBoxClosed != null)
OnMessageBoxClosed(this.Result);
}

private void OKButton_Click(object sender, RoutedEventArgs e)
{
if (btnYes.Content.ToString().ToLower().Equals("yes") == true)
{
//yes button
this.Result = MessageBoxResult.Yes;
}
else
{
//ok button
this.Result = MessageBoxResult.OK;
}

this.Close();
}

private void CancelButton_Click(object sender, RoutedEventArgs e)
{
this.Result = MessageBoxResult.Cancel;
this.Close();
}

private void btnNo_Click(object sender, RoutedEventArgs e)
{
this.Result = MessageBoxResult.Cancel;
this.Close();
}
}

}

And you can call this Message Box

For Yes No Buttons

SLMessageBox messBox;
messBox = new SLMessageBox("Message", "Yes or No Message Box...!", SLMessageBox.MessageBoxButtons.YesNo, SLMessageBox.MessageBoxIcon.Information);
messBox.Show();

For Only Ok Button

SLMessageBox messBox;
messBox = new SLMessageBox("Message", "Ok Message Box...!", SLMessageBox.MessageBoxButtons.Ok, SLMessageBox.MessageBoxIcon.Information);
messBox.Show();

For Ok and Cancel Buttons

SLMessageBox messBox;
messBox = new SLMessageBox("Message", "Ok and Cancel Message Box...!", SLMessageBox.MessageBoxButtons.OkCancel, SLMessageBox.MessageBoxIcon.Information);
messBox.Show();

Edit:
And to check the result of the MessageBox what the user clicked

SLMessageBox messBox;
messBox = new SLMessageBox("Message", "Yes, No and Cancel Buttons Message Box...!", SLMessageBox.MessageBoxButtons.YesNoCancel, , SLMessageBox.MessageBoxIcon.Information);
messBox.Show();
messBox.OnMessageBoxClosed += messBox_OnDeleteMessageBoxClosed;

private void messBox_OnDeleteMessageBoxClosed(MessageBoxResult result)
{
if(result==MessageBoxResult.Yes)
{
//....
}
else if(result==MessageBoxResult.No)
{
//....
}
else
{
//...
}
}

Change button text MessageBox

I think @Paul Rooney has a very good point that tkinter will be cross platform. And there is a little bit more overhead than one might like to call a message box.

Looking at the MessageBox documentation from Microsoft (MessageBoxW is the unicode version of MessageBox), it seems you have a set number of options for what the buttons can be and that is determined by the 4th argument in the function call:

MB_ABORTRETRYIGNORE = 2
MB_CANCELTRYCONTINUE = 6
MB_HELP = 0x4000 = 16384
MB_OK = 0
MB_OKCANCEL = 1
MB_RETRYCANCEL = 5
MB_YESNO = 4
MB_YESNOCANCEL = 3

If these choices are good for you and you're strictly Windows, this could be a winner for you. It's nice because you only have the ctypes import and the actual function call. Though to be a little safer you should consider using the argtypes function from ctypes to make a function prototype.

To do it the tkinter way, you still have most of the same options for a simple message box (e.g. Yes/No, OK/Cancel, etc). If you really need to control the button text, then you'll have to layout a basic form. Here's a basic example of making your own form. I think you'll find it quite tedious.

from tkinter import Tk, LEFT, RIGHT, BOTH, RAISED, Message
from tkinter.ttk import Frame, Button, Style, Label

class Example(Frame):

def __init__(self):
super().__init__()

self.initUI()

def initUI(self):

self.master.title("Buttons")
self.style = Style()
self.style.theme_use("default")

frame = Frame(self, relief=RAISED, borderwidth=1)

message = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua... '

lbl1 = Message(frame, text=message)
lbl1.pack(side=LEFT, padx=5, pady=5)

frame.pack(fill=BOTH, expand=True)

self.pack(fill=BOTH, expand=True)

button1 = Button(self, text="button1")
button1.pack(side=RIGHT, padx=5, pady=5)
button2 = Button(self, text="second button")
button2.pack(side=RIGHT)

def main():

root = Tk()
root.geometry("300x200+300+300")
app = Example()
root.mainloop()

if __name__ == '__main__':
main()

MessageBox buttons - set language?

There is no native support for this in .NET (as far as I know, anyway; please correct me if I'm wrong, anyone). I did come across this CodeProject article, that seem to do the trick with some message hooking and P/Invoke:
http://www.codeproject.com/KB/miscctrl/Localizing_MessageBox.aspx



Related Topics



Leave a reply



Submit