MessageBoxButton.YesNo in object - c#

The code that I cant get to work:
object MessageBoxButton = null;
if (MessageBox.Show(String.Format("{0:0,0}", Convert.ToInt32(txtQuantity.Text)), "OK ??????", MessageBoxButton.YesNo, MessageBoxImage.Question) == DialogResult == false)
Links to stuff I found/tried:
Link1
Link2
The error I get:
'object' does not contain a definition for 'YesNo' and no extension method 'YesNo'.

Why do you try to declare a local variable named MessageBoxButton? This is the name of class that already exists in the framework. If the compiler says that it cannot find the type MessageBoxButton, you have to include the namespace where it can be found using a using clause at the top of the file:
using System.Windows;
Then you can just write:
public class Class1
{
public static void Demo()
{
MessageBoxResult dialogResult = MessageBox.Show("Text", "Caption", MessageBoxButton.YesNo, MessageBoxImage.Information);
if(dialogResult == MessageBoxResult.Yes)
{
MessageBox.Show("Yes was clicked");
}
else
{
MessageBox.Show("No was clicked");
}
}
}
Note that the Microsoft Styleguide says not to use MessageBoxImage.Question. Only use Information, Warning or Error instead (or no Icon at all).

Try
if (MessageBox.Show(String.Format("{0:0,0}", Convert.ToInt32(txtQuantity.Text)), "OK ??????", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)

Related

C# if statement true cancel the remaining process

I have an if statement that checks if a textbox is not empty. HOwever, if it True, meaning empty i want it to cancel the rest of the process and go back to my form. Below is the IF statement that i have, i cant figure out how to Cancel the remainder of the process.
if (textBox2.Text.Equals(""))
{
MessageBox.Show("Field is Empty", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
Calling a method like
DoSomething();
causes it to start executing whatever is inside. In some point, if you no longer wish to continue in execution of that method call, use return statement with no return value for methods returning void or return something for methods with non-void return type, where something is type of the return type.
public void DoSomething()
{
... do something
if (condition)
return; // returns from a method call
}
http://msdn.microsoft.com/fr-fr/library/1dac1663%28v=vs.80%29.aspx
private void validateUserEntry2()
{
// Checks the value of the text.
if(serverName.Text.Length == 0)
{
// Initializes the variables to pass to the MessageBox.Show method.
string message = "You did not enter a server name. Cancel this operation?";
string caption = "No Server Name Specified";
MessageBoxButtons buttons = MessageBoxButtons.YesNo;
DialogResult result;
// Displays the MessageBox.
result = MessageBox.Show(this, message, caption, buttons,
MessageBoxIcon.Question, MessageBoxDefaultButton.Button1,
MessageBoxOptions.RightAlign);
if(result == DialogResult.Yes)
{
// Closes the parent form.
this.Close();
}
}
}

Cannot implicitly convert DialogResult to MessageBoxResult

I am receiving the error 'Cannot implicititly convert type 'System.Windows.Forms.DialogResult' to System.Windows.MessageBoxResult'
At first I thought this was just a missing import so I put imported System.Windows.Forms but it came to no avail as doing this throws up an error on my actual message box which is 'MessageBox' is an ambigous reference between 'System.Windows.MessageBox' and 'System.Windows.Forms.MessageBox' (Which isn't to hard to figure out :P) but the original message remains the same.
private void DisplayOnWebsiteChecked(Object sender, EventArgs e)
{
var departments = model.Name;
var departmentChildren = model.Children;
var messagebox = MessageBox.Show("Do you wish to hide all sub deparments and products.",
"List of Box",
MessageBoxButton.YesNo);
if (messagebox = System.Windows.Forms.DialogResult.Yes)
{
if (departmentChildren != null)
{
int zeroChildren = 0;
if (departmentChildren.Count.Equals(zeroChildren)) ;
{
foreach (Department Children in departmentChildren)
Children.IsVisibleOnWebsite = false;
}
}
}
else
return;
}
I have included the whole method just incase you need anything from it.
Given that you've now explained that this is a WPF app, if you're using System.Windows.MessageBox.Show you should be comparing against System.Windows.MessageBoxResult.
Basically, if you're doing WPF you almost certainly don't want any mention of System.Windows.Forms, and vice versa. (I'm surprised this was compiling at all - do you have references to both assemblies? That's generally a bad idea...)
Additionally, you need to use == for comparisons, not = (assignment)
var result = MessageBox.Show(...);
if (result == MessageBoxResult.Yes)
{
...
}
I think you've got your Windows Forms and WPF mixed up - if you're using the WPF MessageBox then your return value is different.
So it's not
messagebox = System.Windows.Forms.DialogResult.Yes
It's
messagebox == System.Windows.MessageBoxResult.Yes
Note also the == otherwise you're assigning the value, and the if test will always succeed (with a compiler warning).
You're problem is here:
if (messagebox = System.Windows.Forms.DialogResult.Yes)
First, you're assigning a DialogResult into a MessageBox. Secondly, you're attempting to compare a DialogResult with a MessageBox. Try something like:
if(MessageBox.Show("Message", MessageBoxButtons.YesNo) == DialogResult.Yes)
Or you can assign the DialogResult into its own variable, and compare that.

handle reply from message box c#

how can I handle message box reply example if the user click on yes do something if the user click on NO do another thing ?
Example (slightly modified) from the docs:
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)
{
//Do something for No
}
else if (result == DialogResult.Yes)
{
//Do something else for Yes
}
Addendum: In the event that you're still on .NET 2.0 and don't have access to the var keyword, declare result as a DialogResult. I.e:
DialogResult result = MessageBox.Show(...);
Missed the fact that this was tagged with WPF, so if you're using that then you'd be using the slightly (but not too much) different System.Windows.MessageBox class instead of System.Windows.Forms.Messagebox. The interaction is largely the same, but also uses the MessageBoxResult enum instead of DialogResult, the MessageBoxImage enum instead of MessageBoxIcon, and the MessageBoxButton enum instead of MessageBoxButtons (plural). You should be able to do something like this:
const string message =
"Are you sure that you would like to close the form?";
const string caption = "Form Closing";
MessageBoxResult result = MessageBox.Show(message, caption,
MessageBoxButton.YesNo,
MessageBoxImage.Question);
if (result == MessageBoxResult.No)
{
// Do something for No
}
else if (result == MessageBoxResult.Yes)
{
// Do something else for Yes
}
Since the tag states WPF and NOT WinForms, you will need to do something like this for a MessageBox:
MessageBoxResult result = MessageBox.Show("Foo Bar?", "Confirmation", MessageBoxButton.YesNoCancel);
if (result == MessageBoxResult.Yes)
{
// yeah yeah yeah stuff
}
else if (result == MessageBoxResult.No)
{
// no no no stuff
}
else
{
// forget about it
}
In addition the dialogs are dealt with differently in WPF as well, note the absence of DialogResult:
SomeDialog dialog = new SomeDialog();
dialog.ShowDialog();
if (dialog.DialogResult.HasValue && dialog.DialogResult.Value)
MessageBox.Show("Clicked ok");
else
MessageBox.Show("Clicked cancel");
You should try using google or msdn (the links are clickable).
Anyways, you should check the value of the messageboxresult returned by the show method.
http://msdn.microsoft.com/en-us/library/ms598674.aspx
The best for me is
if (MessageBox.Show("Are you sure you want to close the window ?", "Question", MessageBoxButton.YesNo, MessageBoxImage.Warning) == MessageBoxResult.No)
{
//do NO stuff
}
else
{
//do YES stuff
}
DialogResult result = MessageBox.Show("Some Text", "Title", MessageBoxButtons.YesNoCancel);
if(result == DialogResult.Yes)
{
// do something
}
Here is an example:
DialogResult userSelection = MessageBox.Show("Some question","Question",MessageBoxButtons.YesNo,MessageBoxIcon.Question);
// Do something with userSelection
childwindow's in WPF are asynchronous actions. You have to handle the Close event, and inside your close event then you can perform your logic.

How do I create a message box with "Yes", "No" choices and a DialogResult?

I want to make simple Yes/No choiced MessageBox, but I think it is nonsense to design a form for that. I thought I could use MessageBox, add buttons, etc. to accomplish this. It is simple, but since there is no DialogResult returned, how do I retrieve the result?
This should do it:
DialogResult dialogResult = MessageBox.Show("Sure", "Some Title", MessageBoxButtons.YesNo);
if(dialogResult == DialogResult.Yes)
{
//do something
}
else if (dialogResult == DialogResult.No)
{
//do something else
}
DialogResult dr = MessageBox.Show("Are you happy now?",
"Mood Test", MessageBoxButtons.YesNo);
switch(dr)
{
case DialogResult.Yes:
break;
case DialogResult.No:
break;
}
MessageBox class is what you are looking for.
MessageBox.Show(title, text, messageboxbuttons.yes/no)
This returns a DialogResult which you can check.
For example,
if(MessageBox.Show("","",MessageBoxButtons.YesNo) == DialogResult.Yes)
{
//do something
}
The MessageBox does produce a DialogResults
DialogResult r = MessageBox.Show("Some question here");
You can also specify the buttons easily enough. More documentation can be found at http://msdn.microsoft.com/en-us/library/ba2a6d06.aspx
Use:
MessageBoxResult m = MessageBox.Show("The file will be saved here.", "File Save", MessageBoxButton.OKCancel);
if(m == m.Yes)
{
// Do something
}
else if (m == m.No)
{
// Do something else
}
MessageBoxResult is used on Windows Phone instead of DialogResult...
You can also use this variant with text strings, here's the complete changed code (Code from Mikael), tested in C# 2012:
// Variable
string MessageBoxTitle = "Some Title";
string MessageBoxContent = "Sure";
DialogResult dialogResult = MessageBox.Show(MessageBoxContent, MessageBoxTitle, MessageBoxButtons.YesNo);
if(dialogResult == DialogResult.Yes)
{
//do something
}
else if (dialogResult == DialogResult.No)
{
//do something else
}
You can after
.YesNo
insert a message icon
, MessageBoxIcon.Question
#Mikael Svenson's answer is correct. I just wanted to add a small addition to it:
The Messagebox icon can also be included has an additional property like below:
DialogResult dialogResult = MessageBox.Show("Sure", "Please Confirm Your Action", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (MessageBox.Show("Please confirm before proceed" + "\n" + "Do you want to Continue ?", "Confirm", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
//do something if YES
}
else
{
//do something if NO
}
Try this:
if (MessageBox.Show("Are you sure", "Title_here", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
{
Do something here for 'Yes'...
}
This simple code worked for me. I grabbed it from MSDN here:
https://social.msdn.microsoft.com/Forums/en-US/d1092a96-96b0-4ca4-b716-0c8e55e42ee9/how-can-i-manage-messagebox-result-?forum=Vsexpressvcs
if (System.Windows.Forms.MessageBox.Show
("Are you sure you want to add the audit?", "Add",
System.Windows.Forms.MessageBoxButtons.YesNo,
System.Windows.Forms.MessageBoxIcon.Question)
==System.Windows.Forms.DialogResult.Yes)
// Do stuff after 'YES is clicked'
else
// DO stuff after 'NO is clicked'
dynamic MsgResult = this.ShowMessageBox("Do you want to cancel all pending changes ?", "Cancel Changes", MessageBoxOption.YesNo);
if (MsgResult == System.Windows.MessageBoxResult.Yes)
{
enter code here
}
else
{
enter code here
}
Check more detail from here

C#/MonoDevelop: GTK MessageDialogs require a doubleclick to close - why?

I'm a newbie programmer writting a program in MonoDevelop in C# and have a porblem with my gtk MessageDialogs.
The close button on the window boarders of my GTK Message dialogues require a double click to actually close them. The close button on the dialogue its self works fine. Could someone please tell me how I can fix this below is the code:
if (fchDestination.CurrentFolder == fchTarget.CurrentFolder) {
MessageDialog msdSame = new MessageDialog(this, DialogFlags.Modal, MessageType.Error, ButtonsType.Close, "Destination directory cannot be the same as the target directory");
msdSame.Title="Error";
if ((ResponseType) msdSame.Run() == ResponseType.Close) {
msdSame.Destroy();
}
return;
}
if (fchTarget.CurrentFolder.StartsWith(fchDestination.CurrentFolder)) {
MessageDialog msdContains = new MessageDialog(this, DialogFlags.Modal, MessageType.Error, ButtonsType.Close, "error");
msdContains.Title="Error";
if ((ResponseType) msdContains.Run() == ResponseType.Close) {
msdContains.Destroy();
}
return;
}
The response value given when you click on a dialog's "close window" button is not CLOSE, but DELETE_EVENT. That's why the destroy method is never called and the dialog lingers. The second time you close it (out of the context of the run method), the dialog is destroyed normally.
In short, you also need to check for ResponseType.DeleteEvent.
Update:
In code:
MessageDialog msdSame = ...
...
ResponseType response = (ResponseType) msdSame.Run();
if (response == ResponseType.Close || response == ResponseType.DeleteEvent) {
msdSame.Destroy();
}
Or, as ptomato mentions, you don't need to check the response, considering the user only has one choice: "close".
MessageDialog msdSame = ...
...
msdSame.Run();
msdSame.Destroy();
May be both the conditions get satisfied and hence you get two msgbox and it appears you've to give a double click to close it
A sample class could be:
using System;
using Gtk;
namespace Visitors.Clases.MessageBox
{
public static class MessageBox
{
public static Gtk.ResponseType Show(Gtk.Window window, Gtk.DialogFlags dialogflags, MessageType msgType,ButtonsType btnType,string Message,String caption)
{
MessageDialog md = new MessageDialog (window,dialogflags,msgType,btnType, Message);
md.Title = caption;
ResponseType tp = (Gtk.ResponseType)md.Run();
md.Destroy();
return tp;
}
}
}
The class in use:
ResponseType result = MessageBox.Show(this,DialogFlags.Modal,MessageType.Error,ButtonsType.Ok,Error,"ERROR");
if (result == Gtk.ResponseType.Yes)
{
MessageBox.Show (this, DialogFlags.Modal, MessageType.Other,ButtonsType.Ok, "YES", "EJEMPLO");
}
else
{
MessageBox.Show (this, DialogFlags.Modal, MessageType.Other,ButtonsType.Ok, "NO", "EJEMPLO");
}

Categories

Resources