如何为异常消息框编程
可以在应用程序中使用异常消息框,与 MessageBox 类相比,它可以大大提高对消息传递的控制。有关详细信息,请参阅异常消息框编程。有关如何获取和部署异常消息框 .dll 的信息,请参阅部署异常消息框应用程序。
过程
通过使用异常消息框处理异常
将托管代码项目中的一个引用添加到 Microsoft.ExceptionMessageBox.dll 程序集。
(可选)添加 using (C#) 或 Imports (Microsoft Visual Basic .NET) 指令以使用 Microsoft.SqlServer.MessageBox 命名空间。
创建 try-catch 块来处理可能出现的异常。
在 catch 块中,创建一个 ExceptionMessageBox 类的实例。传递由 try-catch 块处理的 Exception 对象。
(可选)为 ExceptionMessageBox 设置下列一个或多个属性:
Buttons - ExceptionMessageBoxButtons 枚举,用于指定在异常消息框中显示的按钮。
DefaultButton - ExceptionMessageBoxDefaultButton 枚举,用于指定异常消息框的默认按钮。
Options - ExceptionMessageBoxOptions 枚举,用于控制异常消息框的其他行为。
Symbol - ExceptionMessageBoxSymbol 枚举,用于指定在异常消息框中显示的符号。
调用 Show 方法。传递异常消息框所属的父窗口。
(可选)如果需要确定用户单击的按钮,请记下返回的 DialogResult 枚举的值。
显示没有异常的异常消息框
将托管代码项目中的一个引用添加到 Microsoft.ExceptionMessageBox.dll 程序集。
(可选)添加 using (C#) 或 Imports (Visual Basic .NET) 指令以使用 Microsoft.SqlServer.MessageBox 命名空间。
创建 ExceptionMessageBox 类的实例。将消息文本作为 String 值传递。
(可选)为 ExceptionMessageBox 设置下列一个或多个属性:
Buttons - ExceptionMessageBoxButtons 枚举,用于指定在异常消息框中显示的按钮。
Caption - 异常消息框的对话框标题。
DefaultButton - ExceptionMessageBoxDefaultButton 枚举,用于指定异常消息框对话框的默认按钮。
Options - ExceptionMessageBoxOptions 枚举,用于控制异常消息框的其他行为。
Symbol - ExceptionMessageBoxSymbol 枚举,用于指定在异常消息框中显示的符号。
调用 Show 方法。传递异常消息框所属的父窗口。
(可选)如果需要确定用户单击的按钮,请记下返回的 DialogResult 枚举的值。
显示具有自定义按钮的异常消息框
将托管代码项目中的一个引用添加到 Microsoft.ExceptionMessageBox.dll 程序集。
(可选)添加 using (C#) 或 Imports (Visual Basic .NET) 指令以使用 Microsoft.SqlServer.MessageBox 命名空间。
用以下两种方法之一创建 ExceptionMessageBox 类的实例:
为 Buttons 设置以下值之一:
AbortRetryIgnore - 显示**“中止”、“重试”和“忽略”**按钮。
Custom - 显示自定义按钮。
OK - 显示**“确定”**按钮。
OKCancel - 显示**“确定”和“取消”**按钮。
RetryCancel - 显示**“重试”和“取消”**按钮。
YesNo - 显示**“是”和“否”**按钮。
YesNoCancel - 显示**“是”、“否”和“取消”**按钮。
(可选)如果使用自定义按钮,调用 SetButtonText 方法的重载之一可指定多达五个自定义按钮的文本。
调用 Show 方法。传递异常消息框所属的父窗口。
(可选)如果需要确定用户单击的按钮,请记下返回的 DialogResult 枚举的值。如果使用自定义按钮,请记下 CustomDialogResult 属性的 ExceptionMessageBoxDialogResult 值以确定用户单击的自定义按钮。
让用户决定是否要显示异常消息框
将托管代码项目中的一个引用添加到 Microsoft.ExceptionMessageBox.dll 程序集。
(可选)添加 using (C#) 或 Imports (Visual Basic .NET) 指令以使用 Microsoft.SqlServer.MessageBox 命名空间。
用以下两种方法之一创建 ExceptionMessageBox 类的实例:
将 ShowCheckbox()()()() 属性设置为 true。
(可选)指定用于请求用户决定是否为 CheckboxText()()()() 再次显示异常消息框的文本。默认文本为“不再显示此消息。”
如果仅需在应用程序执行期间保留用户的决定,请将 IsCheckboxChecked()()()() 的值设置为全局 Boolean 变量。请在创建异常消息框的实例前计算此值。
如果需要永久存储用户的决定,请执行以下操作:
调用 CreateSubKey(String) 方法打开应用程序所使用的自定义注册表项,然后将 CheckboxRegistryKey()()()() 设置为返回的 RegistryKey 对象。
将 CheckboxRegistryValue()()()() 设置为所使用的注册表值的名称。
将 CheckboxRegistryMeansDoNotShowDialog()()()() 设置为 true。
调用 Show 方法。计算指定的注册表项,且仅当注册表项中存储的数据为 0 时才会显示异常消息框。如果显示对话框且用户在单击按钮前选中复选框,则注册表项中的数据将设置为 1。
示例
此示例使用只具有**“确定”**按钮的异常消息框来显示某个应用程序异常的信息,其中包括已处理的异常以及其他特定于应用程序的信息。
try
{
// Do something that may generate an exception.
throw new ApplicationException("An error has occured");
}
catch (ApplicationException ex)
{
// Define a new top-level error message.
string str = "The action failed.";
// Add the new top-level message to the handled exception.
ApplicationException exTop = new ApplicationException(str, ex);
exTop.Source = this.Text;
// Show an exception message box with an OK button (the default).
ExceptionMessageBox box = new ExceptionMessageBox(exTop);
box.Show(this);
}
Try
' Do something that may generate an exception.
Throw New ApplicationException("An error has occured")
Catch ex As ApplicationException
' Define a new top-level error message.
Dim str As String = "The action failed."
' Add the new top-level message to the handled exception.
Dim exTop As ApplicationException = New ApplicationException(str, ex)
exTop.Source = Me.Text
' Show an exception message box with an OK button (the default).
Dim box As ExceptionMessageBox = New ExceptionMessageBox(exTop)
box.Show(Me)
End Try
此示例使用具有可供用户从中选择**“是”和“否”**按钮的异常消息框。
// Define the message and caption to display.
string str = @"Are you sure you want to delete file 'c:\somefile.txt'?";
string caption = "Confirm File Deletion";
// Show the exception message box with Yes and No buttons.
ExceptionMessageBox box = new ExceptionMessageBox(str,
caption, ExceptionMessageBoxButtons.YesNo,
ExceptionMessageBoxSymbol.Question,
ExceptionMessageBoxDefaultButton.Button2);
if (DialogResult.Yes == box.Show(this))
{
// Delete the file.
}
' Define the message and caption to display.
Dim str As String = "Are you sure you want to delete file 'c:\somefile.txt'?"
Dim caption As String = "Confirm File Deletion"
' Show the exception message box with Yes and No buttons.
Dim box As ExceptionMessageBox = New ExceptionMessageBox(str, _
caption, ExceptionMessageBoxButtons.YesNo, _
ExceptionMessageBoxSymbol.Question, _
ExceptionMessageBoxDefaultButton.Button2)
If Windows.Forms.DialogResult.Yes = box.Show(Me) Then
' Delete the file.
End If
此示例使用具有自定义按钮的异常消息框。
try
{
// Do something that may cause an exception.
throw new ApplicationException("An error has occured");
}
catch (ApplicationException ex)
{
string str = "Action failed. What do you want to do?";
ApplicationException exTop = new ApplicationException(str, ex);
exTop.Source = this.Text;
// Show the exception message box with three custom buttons.
ExceptionMessageBox box = new ExceptionMessageBox(exTop);
// Set the names of the three custom buttons.
box.SetButtonText("Skip", "Retry", "Stop Processing");
// Set the Retry button as the default.
box.DefaultButton = ExceptionMessageBoxDefaultButton.Button2;
box.Symbol = ExceptionMessageBoxSymbol.Question;
box.Buttons = ExceptionMessageBoxButtons.Custom;
box.Show(this);
// Do something, depending on the button that the user clicks.
switch (box.CustomDialogResult)
{
case ExceptionMessageBoxDialogResult.Button1:
// Skip action
break;
case ExceptionMessageBoxDialogResult.Button2:
// Retry action
break;
case ExceptionMessageBoxDialogResult.Button3:
// Stop processing action
break;
}
}
Try
' Do something that may cause an exception.
Throw New ApplicationException("An error has occured")
Catch ex As ApplicationException
Dim str As String = "Action failed. What do you want to do?"
Dim exTop As ApplicationException = New ApplicationException(str, ex)
exTop.Source = Me.Text
' Show the exception message box with three custom buttons.
Dim box As ExceptionMessageBox = New ExceptionMessageBox(exTop)
' Set the names of the three custom buttons.
box.SetButtonText("Skip", "Retry", "Stop Processing")
' Set the Retry button as the default.
box.DefaultButton = ExceptionMessageBoxDefaultButton.Button2
box.Symbol = ExceptionMessageBoxSymbol.Question
box.Buttons = ExceptionMessageBoxButtons.Custom
box.Show(Me)
' Do something, depending on the button that the user clicks.
Select Case box.CustomDialogResult
Case ExceptionMessageBoxDialogResult.Button1
' Skip action
Case ExceptionMessageBoxDialogResult.Button2
' Retry action
Case ExceptionMessageBoxDialogResult.Button3
' Stop processing action
End Select
End Try
此示例使用复选框来确定是否显示异常消息框。
try
{
// Do something that may cause an exception.
throw new ApplicationException("An error has occured.");
}
catch (ApplicationException ex)
{
string str = "The action failed.";
ApplicationException exTop = new ApplicationException(str, ex);
exTop.Source = this.Text;
// Show a message box if the global variable is true.
if (alwaysShow)
{
ExceptionMessageBox box = new ExceptionMessageBox(exTop);
box.ShowCheckBox = true;
box.IsCheckBoxChecked = true;
box.CheckBoxText = "Always show this message";
box.Show(this);
// Set the global variable.
alwaysShow = box.IsCheckBoxChecked;
}
}
Try
' Do something that may cause an exception.
Throw New ApplicationException("An error has occured.")
Catch ex As ApplicationException
Dim str As String = "The action failed."
Dim exTop As ApplicationException = New ApplicationException(str, ex)
exTop.Source = Me.Text
' Show a message box if the global variable is true.
If alwaysShow Then
Dim box As ExceptionMessageBox = New ExceptionMessageBox(exTop)
box.ShowCheckBox = True
box.IsCheckBoxChecked = True
box.CheckBoxText = "Always show this message"
box.Show(Me)
' Set the global variable.
alwaysShow = box.IsCheckBoxChecked
End If
End Try
此示例使用复选框和注册表项来确定是否显示异常消息框。
try
{
// Do something that could generate an exception.
throw new ApplicationException("An error has occured.");
}
catch (ApplicationException ex)
{
string str = "The action failed. Do you want to continue?";
ApplicationException exTop = new ApplicationException(str, ex);
exTop.Source = this.Text;
// Show a message box with Yes and No buttons
ExceptionMessageBox box = new ExceptionMessageBox(exTop,
ExceptionMessageBoxButtons.YesNo,
ExceptionMessageBoxSymbol.Question,
ExceptionMessageBoxDefaultButton.Button2);
// Enable the check box.
box.ShowCheckBox = true;
// Define the registry key to use.
box.CheckBoxRegistryKey =
Microsoft.Win32.Registry.CurrentUser.CreateSubKey(
@"Software\TestApp");
box.CheckBoxRegistryValue = "DontShowActionFailedMessage";
box.CheckBoxRegistryMeansDoNotShowDialog = true;
box.DefaultDialogResult = DialogResult.Yes;
// The message box won’t be displayed if the
// "DontShowActionFailedMessage" value of the registry key
// contains a non-zero value.
if (box.Show(this) == DialogResult.No)
{
// Do something if the user clicks the No button.
this.Close();
}
}
Try
' Do something that could generate an exception.
Throw New ApplicationException("An error has occured.")
Catch ex As ApplicationException
Dim str As String = "The action failed. Do you want to continue?"
Dim exTop As ApplicationException = New ApplicationException(str, ex)
exTop.Source = Me.Text
' Show a message box with Yes and No buttons
Dim box As ExceptionMessageBox = New ExceptionMessageBox(exTop, _
ExceptionMessageBoxButtons.YesNo, _
ExceptionMessageBoxSymbol.Question, _
ExceptionMessageBoxDefaultButton.Button2)
' Enable the check box.
box.ShowCheckBox = True
' Define the registry key to use.
box.CheckBoxRegistryKey = _
Microsoft.Win32.Registry.CurrentUser.CreateSubKey( _
"Software\TestApp")
box.CheckBoxRegistryValue = "DontShowActionFailedMessage"
box.CheckBoxRegistryMeansDoNotShowDialog = True
box.DefaultDialogResult = Windows.Forms.DialogResult.Yes
' The message box won�t be displayed if the
' "DontShowActionFailedMessage" value of the registry key
' contains a non-zero value.
If box.Show(Me) = Windows.Forms.DialogResult.No Then
' Do something if the user clicks the No button.
Me.Close()
End If
End Try
此示例使用异常消息框来显示对故障排除或调试有帮助的其他信息。
try
{
// Do something that you don't expect to generate an exception.
throw new ApplicationException("Failed to connect to the server.");
}
catch (ApplicationException ex)
{
string str = "An unexpected error occurred. Please call Helpdesk.";
ApplicationException exTop = new ApplicationException(str, ex);
exTop.Source = this.Text;
// Information in the Data property of an exception that has a name
// beginning with "HelpLink.Advanced" is shown when the user
// clicks the Advanced Information button of the exception message
// box dialog box.
exTop.Data.Add("AdvancedInformation.FileName", "application.dll");
exTop.Data.Add("AdvancedInformation.FilePosition", "line 355");
exTop.Data.Add("AdvancedInformation.UserContext", "single user mode");
// Show the exception message box with additional information that
// is helpful when a user calls technical support.
ExceptionMessageBox box = new ExceptionMessageBox(exTop);
box.Show(this);
}
Try
' Do something that you don't expect to generate an exception.
Throw New ApplicationException("Failed to connect to the server.")
Catch ex As ApplicationException
Dim str As String = "An unexpected error occurred. Please call Helpdesk."
Dim exTop As ApplicationException = New ApplicationException(str, ex)
exTop.Source = Me.Text
' Information in the Data property of an exception that has a name
' beginning with "HelpLink.Advanced" is shown when the user
' clicks the Advanced Information button of the exception message
' box dialog box.
exTop.Data.Add("AdvancedInformation.FileName", "application.dll")
exTop.Data.Add("AdvancedInformation.FilePosition", "line 355")
exTop.Data.Add("AdvancedInformation.UserContext", "single user mode")
' Show the exception message box with additional information that
' is helpful when a user calls technical support.
Dim box As ExceptionMessageBox = New ExceptionMessageBox(exTop)
box.Show(Me)
End Try