DialogResult in WPF Application - c#

I am currently developing an application in C# using WPF, I have always only used WinForms. Normally if I want to ask the user a question instead of making my own dialogue I use
DialogResult result = MessageBox.Show(
"My Message Question", "My Title", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
This is the first time that I have used a WPF form and DialogResult does not seem to be available. What do I use to get the same effect?

Here is how you do the same in WPF:
MessageBoxResult result = MessageBox.Show("My Message Question", "My Title", MessageBoxButton.YesNo, MessageBoxImage.Question);
if (result == MessageBoxResult.Yes)
{
// Do this
}

Use MessageBoxResult instead. And use the MessageBox class. But this message box will look pretty ugly "classic" style.
Another option would be to use Extended WPF toolkit
Yet another option would be to go here and download CrossTechnologySamples.exe then look into the VistaBridge project. I recommend you give a good look here because you will find other samples for other dialogs (like FileOpen, FileSave etc.) that do not exist by default in WPF.

Related

How to create a dialog for use by an Excel AddIn?

I am familiar with the basics of Excel AddIns, but have no idea how to design, implement and later display an internal dialog.
See standing question with images here:
https://social.msdn.microsoft.com/Forums/en-US/935ebeae-1b88-4609-ba33-b0e522d2797f/how-to-create-a-dialog-for-use-by-an-excel-addin?forum=exceldev
TIA
Notes:
(1) My programming language is C#
(2) I prefer to design dialogs by drawing them.
You can use the MessageBox class, for example:
const string message =
"Are you sure that you would like to close the form?";
const string caption = "Form Closing";
var result = MessageBox.Show(message, caption,
MessageBoxButtons.YesNo,
MessageBoxIcon.Question);
// If the no button was pressed ...
if (result == DialogResult.No)
{
// cancel the closure of the form.
e.Cancel = true;
}
If you want to customize the dialog window on your own, you can add a new Windows Form to the project and then add the required controls. After creating an instance of the form in the code you may show it using the Show or ShowDialog methods.

Messagebox function show error

In my Windows application, I want to when ever user try to delete a row from datagridview then there should be a open a messagebox, asking user to confirm the deletion of that row.
For this purpose I have written this code:
DialogResult res = MessageBox.Show("Are You Sure", MessageBoxButtons.OKCancel);
and check the user user response but this line shows error.
What is wrong with this code?
Please help me.
When I write only this code
MessageBox.Show("Are You Sure");
then it is working fine, but I want to confirm the user again so I want his response.
You are almost there! If you want to specify MessageBoxButtons you need to add a title as well as a caption (or message text) in this manner:
MessageBox.Show(string, string, MessageBoxButtons);
Completed, your code should look something like this:
DialogResult res = MessageBox.Show("Are you sure?", "Title", MessageBoxButtons.OKCancel);
You are getting the error, because your debugger is expecting a string for a Title and is receiving MessageBoxButtons instead. Read this article for a detailed explanation.
According to the MessageBox API There is no method overload for Show(String, MessageBoxButtons)
I think you want
Show(String, String, MessageBoxButtons)
Use the MessageBoxResult instead:
MessageBoxResult res= MessageBox.Show("Are You Sure",MessageBoxButtons.OKCancel);

Custom image enum for MessageBoxResult

I try to create a custom MessageBoxImage for the build in MessageBoxResult
For the custom MessageBoxImage enumeration, I have:
public enum CustomBoxImage
{
Foo = new BitmapImage(new Uri(#"pack://application:,,,/MySoftware;component/Images/foo.png"))
}
and the MessageBoxResult, I have:
MessageBoxResult mrb = MessageBox.Show(
"This will kill you. Are you sure?",
"Kill you",
MessageBoxButton.YesNo, CustomBoxImage.Foo);
But it gives me this error:
Cannot convert from "...CustomBoxImage" to "System.Windows.MessageBoxImage'
How can I insert a customized image enumeration into MessageBoxResult? Or, is it even possible?
You can't customize the message boxes beyond the given options. If you need a fully customized one, you can use a 3rd party component. You can even make a window look fully like a message box and customize it if you really need to.
You can't change the signature of the Show method but you can create a converter between your enumeration and the MessageBoxImage enumeration.
if you want to use things that is not provide by the MessageBox, you can create your own messagebox.

how to translate buttons in DialogResult in compact framework?

DialogResult result = MessageBox.Show("Do you want to delete?", string.Empty, MessageBoxButtons.YesNo, MessageBoxIcon.None, MessageBoxDefaultButton.Button1);
if (result == DialogResult.No)
{
return;
}
And then i get popup Message with "Yes" and "No". Can i translate this two buttons and if yes how?
EDIT:
if anyone has similar problem look here:
http://www.christec.co.nz/blog/archives/134
No.
MessageBox.Show calls a native API function which shows a standard Windows dialog box.
The buttons will appear in the current system UI language.
If you want more control, create your own form.
If you're talking about the 'Yes'/'No' text in the popup, the text defaults to the local language set by the OS.
If you want to localize them to some other language, you'll have to implement your own MessageBox-like class.
On a computer with Russian language set a default UI language, your TextBox will display Da (Да) and Net (Нет) automatically instead of Yes and No. So basically you don't need to localize these.
P.S. Same applies to other UI localizations.

C#: Is there a winforms way to make C# MessageBox Buttons (YesNo) larger?

It would be nice to have larger MessageBox Buttons since the target for this application is a tablet.
DialogResult dialogResult = MessageBox.Show(
message, caption,
MessageBoxButtons.YesNo,
MessageBoxIcon.Question,
MessageBoxDefaultButton.Button2);
switch (dialogResult)
{
case DialogResult.Yes:
// ...
It is a system setting. Tablet PCs are normally already configured to make it easy to tap buttons like this so that it works well in any program, not just yours. To configure your tablet, in Win7, use Control Panel + Display, Personalization, Window Color. Click Advanced appearance settings, select "Message Box" in the Item combo. Increase the font size. Don't be fooled by the poor preview, the button will actually grow. There are additional settings in this dialog you might want to tweak to make it easier to manipulate the UI.
A messagebox is just a simple modal form. You can make one yourself and use ShowDialog()
I'm not sure if it's possible or not, but you could use a simple form instead of a dialog box then you can get the design exactly as you wish.
Winforms way? Do you mean, an "automagically via a property change" way? If so, none that I know of.
You can spin up your own custom dialog/form that is bigger and use it instead. While this is not as automagic as the one line MessageBox.Show(), it is not very difficult.
That's not possible with MessageBox which wraps the native system dialog.
You'll need to produce your own dialog or even better see if there is a way to configure the system to give your app (and all others) bigger buttons.
The downside of rolling your own is that you lose all the functionality that the native one provides.
Yeah, going along with what MattP said, you'll need to create a custom form and then use the ShowDialog() method to display the second form as a modal dialog.
private void button2_Click(object sender, System.EventArgs e) {
using (Form2 xForm = new Form2()) {
if (xForm.ShowDialog(this) == DialogResult.OK) {
// Take some action
}
}
}
You can make a 2nd form, then you can make the buttons as big as you want

Categories

Resources