Message.Box: Questions

  • Thread starter Thread starter Luigi Z
  • Start date Start date
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.
 
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
 
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.
 
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
 
Back
Top