Message.Box: Questions

L

Luigi Z

Hi all,
having a Message.Box like this:

MessageBox.Show("File " + fileName + " already exists.
Overwrite them?", "Rendiconto Export Files", MessageBoxButtons.YesNoCancel);

how can I intercept the Yes, or No button, to run separate methods?
And if I have several files, how can I implements a button like "Yes to
all", or "No to all"?

Thanks in advance.
 
A

Armin Zingler

Luigi said:
Hi all,
having a Message.Box like this:

MessageBox.Show("File " + fileName + " already
exists. Overwrite them?", "Rendiconto Export Files",
MessageBoxButtons.YesNoCancel);

how can I intercept the Yes, or No button, to run separate methods?

Show is a function. Use it's return value.
And if I have several files, how can I implements a button like "Yes
to all", or "No to all"?

That's not available with the common message box. Make your own Form.


Armin
 
M

Morten Wennevik [C# MVP]

Luigi Z said:
Hi all,
having a Message.Box like this:

MessageBox.Show("File " + fileName + " already exists.
Overwrite them?", "Rendiconto Export Files", MessageBoxButtons.YesNoCancel);

how can I intercept the Yes, or No button, to run separate methods?
And if I have several files, how can I implements a button like "Yes to
all", or "No to all"?

Thanks in advance.


Hi Luigi,

As Armin pointed out, Show will return a value, DialogResult, which is an
enum returning the same value as the button clicked.

DialogResult result = MessageBox.Show(...);
if(result == DialogResult.Yes)
{}
else if(result == DialogResult.No)
{}
else
{} // Cancel

You can also use this DialogResult to pass messages in your own Dialogs and
if you set AcceptButton and CancelButton on a Form, the DialogResult will be
automatically set to OK and Cancel.
 
K

Kevin

How about this

If My.Application.Question("Continue") Then
...
End If

Namespace My
Partial Friend Class MyApplication
''' <summary>
''' Ask question with NO as the default button
''' </summary>
''' <param name="QuestionText"></param>
''' <returns></returns>
''' <remarks></remarks>
Public Function Question(ByVal QuestionText As String) As Boolean
Return (Windows.Forms.MessageBox.Show(QuestionText,
My.Application.Info.Title, Windows.Forms.MessageBoxButtons.YesNo,
Windows.Forms.MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) =
MsgBoxResult.Yes)
End Function
''' <summary>
''' Ask question, NO is default button
''' </summary>
''' <param name="QuestionText"></param>
''' <param name="Title"></param>
''' <returns></returns>
''' <remarks></remarks>
Public Function Question(ByVal QuestionText As String, ByVal Title
As String) As Boolean
Return (Windows.Forms.MessageBox.Show(QuestionText, Title,
Windows.Forms.MessageBoxButtons.YesNo,
Windows.Forms.MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) =
MsgBoxResult.Yes)
End Function
''' <summary>
''' Ask question
''' </summary>
''' <param name="QuestionText">Question to ask</param>
''' <param name="Title">Text for dialog caption</param>
''' <param name="DefaultButton">Which button is the default</param>
''' <returns></returns>
''' <remarks></remarks>
Public Function Question(ByVal QuestionText As String, ByVal Title
As String, ByVal DefaultButton As MsgBoxResult) As Boolean
Dim db As Windows.Forms.MessageBoxDefaultButton
If DefaultButton = MsgBoxResult.No Then
db = Windows.Forms.MessageBoxDefaultButton.Button2
End If
Return (Windows.Forms.MessageBox.Show(QuestionText, Title,
Windows.Forms.MessageBoxButtons.YesNo,
Windows.Forms.MessageBoxIcon.Question, db) = MsgBoxResult.Yes)
End Function
End Class

End Namespace
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top