Getting returns from a messagebox
-
Ok, in my text editor, when a user clicks new when the document hasn't been saved, it prompts the message:
MessageBox.Show("This document has not been saved, would you like to save this document?", "Save", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
But how do I now get returns from this function, so I can use an if statement such as:if(WHATERVER GOES HERE == OK){Messagebox.Show("User clicked OK");
Thanks, Any reply is appreciated. -
Ok, in my text editor, when a user clicks new when the document hasn't been saved, it prompts the message:
MessageBox.Show("This document has not been saved, would you like to save this document?", "Save", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
But how do I now get returns from this function, so I can use an if statement such as:if(WHATERVER GOES HERE == OK){Messagebox.Show("User clicked OK");
Thanks, Any reply is appreciated. -
MEssageBox.Show
returns aDialogResult
value. That's the return value you want.-- Rules of thumb should not be taken for the whole hand.
So if I used:
if(MessageBox.Show == DialogResult.OK) { MessageBox.Show("User selected OK"); }
that should work? Hmm, how does the compiler know that MessageBox.Show in the if statement is the same as the one with the buttons? Do I have to do something like:MessageBox myBox = new MessageBox(); myBox.Show(ALL THE CODE HERE); if(myBox == DialogResult.OK) { CODE HERE }
Should that work? -
So if I used:
if(MessageBox.Show == DialogResult.OK) { MessageBox.Show("User selected OK"); }
that should work? Hmm, how does the compiler know that MessageBox.Show in the if statement is the same as the one with the buttons? Do I have to do something like:MessageBox myBox = new MessageBox(); myBox.Show(ALL THE CODE HERE); if(myBox == DialogResult.OK) { CODE HERE }
Should that work?You can do something like this.
if (MessageBox.Show("Press Ok or Cancel", "Test", MessageBoxButtons.OKCancel) == DialogResult.OK) { MessageBox.Show("You pressed Ok"); } else { MessageBox.Show("You pressed Cancel"); }
You cannot use new on MessageBox because its a static class (all functions are static and the constructor is private). Also since the MessageBox.Show function does not return unless the user press a button, there is no confusion for the compiler.--- "Drawing on my superior command of language I said nothing."
-
So if I used:
if(MessageBox.Show == DialogResult.OK) { MessageBox.Show("User selected OK"); }
that should work? Hmm, how does the compiler know that MessageBox.Show in the if statement is the same as the one with the buttons? Do I have to do something like:MessageBox myBox = new MessageBox(); myBox.Show(ALL THE CODE HERE); if(myBox == DialogResult.OK) { CODE HERE }
Should that work?DialogResult result = MessageBox.Show("do something?", "title here", MessageBoxButtons.YesNo);
if(result == DialogResult.Yes)
{
// He clicked yes.
}
else
{
// he clicked no
}Tech, life, family, faith: Give me a visit. I'm currently blogging about: God-as-Judge, God-as-Forgiver The apostle Paul, modernly speaking: Epistles of Paul Judah Himango
-
So if I used:
if(MessageBox.Show == DialogResult.OK) { MessageBox.Show("User selected OK"); }
that should work? Hmm, how does the compiler know that MessageBox.Show in the if statement is the same as the one with the buttons? Do I have to do something like:MessageBox myBox = new MessageBox(); myBox.Show(ALL THE CODE HERE); if(myBox == DialogResult.OK) { CODE HERE }
Should that work?When all else fails, study the documentation. http://msdn2.microsoft.com/en-us/library/0x49kd7z.aspx[^] Their sample code answers your question.
-
You can do something like this.
if (MessageBox.Show("Press Ok or Cancel", "Test", MessageBoxButtons.OKCancel) == DialogResult.OK) { MessageBox.Show("You pressed Ok"); } else { MessageBox.Show("You pressed Cancel"); }
You cannot use new on MessageBox because its a static class (all functions are static and the constructor is private). Also since the MessageBox.Show function does not return unless the user press a button, there is no confusion for the compiler.--- "Drawing on my superior command of language I said nothing."
-
DialogResult result = MessageBox.Show("do something?", "title here", MessageBoxButtons.YesNo);
if(result == DialogResult.Yes)
{
// He clicked yes.
}
else
{
// he clicked no
}Tech, life, family, faith: Give me a visit. I'm currently blogging about: God-as-Judge, God-as-Forgiver The apostle Paul, modernly speaking: Epistles of Paul Judah Himango
-
So if I used:
if(MessageBox.Show == DialogResult.OK) { MessageBox.Show("User selected OK"); }
that should work? Hmm, how does the compiler know that MessageBox.Show in the if statement is the same as the one with the buttons? Do I have to do something like:MessageBox myBox = new MessageBox(); myBox.Show(ALL THE CODE HERE); if(myBox == DialogResult.OK) { CODE HERE }
Should that work?Hi Blekk. Exists two way for using. 1)
if( MessageBox.Show(Handle, msg, caption, MessageBoxButtons.RetryCancel, MessageBoxIcon.Question) == DialogResult.Retry) { //Do something... }
2)DialogResult p = MessageBox.Show(Handle, msg, caption, MessageBoxButtons.RetryCancel, MessageBoxIcon.Question); if( p == DialogResult.Retry ) { //Do something... }
SINCERELY. ANTHONY ACUÑA PREFERED PHRASE: SOMEBODY TELL ME WHY IS MORE REAL WHEN I DREAM THAT I AM WAKE?
-
Thanks, I'll try that after I finish my ICT Essay :P And yeah, I realised you cannot use MessageBox as a normal class, using new etc. Out of interest, why is this the case?
The way I think about it is like this: We create normal classes (with constructor) because we might want to have multiple instances of that class at the same time. There is no need to create multiple instances of the MessageBox class because the only thing you use a MessageBox is to show it to the user and wait for a input. The MessageBox is a modal dialog and hence the application will have to sit and wait till user clicks a button. Once the user clicks the button the only information you will need is which button the user clicked (if there is more than one button). So the entire purpose for which MessageBox class exists is finished when we call the Show method and check its return value. Hence the natural way is to make it a static class so that its very easy for use (means write less code). User never needs to create an instance.
--- "Drawing on my superior command of language I said nothing."
-
Ok, in my text editor, when a user clicks new when the document hasn't been saved, it prompts the message:
MessageBox.Show("This document has not been saved, would you like to save this document?", "Save", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
But how do I now get returns from this function, so I can use an if statement such as:if(WHATERVER GOES HERE == OK){Messagebox.Show("User clicked OK");
Thanks, Any reply is appreciated. -
Thanks, I'll try that after I finish my ICT Essay :P And yeah, I realised you cannot use MessageBox as a normal class, using new etc. Out of interest, why is this the case?
Blekk wrote:
And yeah, I realised you cannot use MessageBox as a normal class, using new etc.
That's because MessageBox a) doesn't have any constructors b) the
Show
method is declaredstatic
, which means that you don't need an instance of the class and call it directly likeClass.Method
hope this helps, regards -
Blekk wrote:
And yeah, I realised you cannot use MessageBox as a normal class, using new etc.
That's because MessageBox a) doesn't have any constructors b) the
Show
method is declaredstatic
, which means that you don't need an instance of the class and call it directly likeClass.Method
hope this helps, regardsThanks Pradeep C and Greeeg, I now understand why you don't have to create an instance and you have both also made the whole concept of the C# language a little more clear. There are still many things I don't really know at this stage, but probably should know, meh, I would rather find it out this way than reading through endless chapters of a book and then forgetting what it was at the end of the book. :laugh: Thanks.