MessageBox 中的 C# 自定义异常标题
C# Custom Exception Title in MessageBox
标题有点混乱,所以希望我能在这里解释得更好一点。如果出现错误,我想更改屏幕上弹出的 MessageBox 的标题,因为默认消息非常冗长,我更愿意为用户可以理解的错误提供更好的解释。
private void Load_Click(object sender, RoutedEventArgs e)
{
if (comboBox.SelectedItem.ToString() == "Department Staff")
{
try
{
DataTable dt = dataSource.DataTableQuery("SELECT * FROM DepartmentStaff");
dataGrid.ItemsSource = dt.DefaultView;
}
catch (NullReferenceException ex)
{
MessageBox.Show("Unable To Connect To Database, Please Try Again Later.", ex.ToString());
}
}
else
{
try
{
DataTable dt = dataSource.DataTableQuery("SELECT * FROM Department");
dataGrid.ItemsSource = dt.DefaultView;
}
catch (NullReferenceException ex)
{
MessageBox.Show("Unable To Connect To Database, Please Try Again Later.", ex.ToString());
}
}
仔细查看 Message.Show()
参数:
Message.Show(text, caption); //the first one is text, the second one is caption.
第二个参数是 标题(或标题),而 第一个 是消息。现在在您的使用中,您将异常消息(通常很长)作为 标题 ,这就是为什么您得到 "extremely long winded" 标题(不是消息)。
MessageBox.Show("Unable To Connect To Database, Please Try Again Later.", ex.ToString());
不要那样做!相反,这样做:
MessageBox.Show("Unable To Connect To Database, Please Try Again Later. " + ex.ToString(), "Error");
只需将 "Error" 作为标题参数即可。
标题有点混乱,所以希望我能在这里解释得更好一点。如果出现错误,我想更改屏幕上弹出的 MessageBox 的标题,因为默认消息非常冗长,我更愿意为用户可以理解的错误提供更好的解释。
private void Load_Click(object sender, RoutedEventArgs e)
{
if (comboBox.SelectedItem.ToString() == "Department Staff")
{
try
{
DataTable dt = dataSource.DataTableQuery("SELECT * FROM DepartmentStaff");
dataGrid.ItemsSource = dt.DefaultView;
}
catch (NullReferenceException ex)
{
MessageBox.Show("Unable To Connect To Database, Please Try Again Later.", ex.ToString());
}
}
else
{
try
{
DataTable dt = dataSource.DataTableQuery("SELECT * FROM Department");
dataGrid.ItemsSource = dt.DefaultView;
}
catch (NullReferenceException ex)
{
MessageBox.Show("Unable To Connect To Database, Please Try Again Later.", ex.ToString());
}
}
仔细查看 Message.Show()
参数:
Message.Show(text, caption); //the first one is text, the second one is caption.
第二个参数是 标题(或标题),而 第一个 是消息。现在在您的使用中,您将异常消息(通常很长)作为 标题 ,这就是为什么您得到 "extremely long winded" 标题(不是消息)。
MessageBox.Show("Unable To Connect To Database, Please Try Again Later.", ex.ToString());
不要那样做!相反,这样做:
MessageBox.Show("Unable To Connect To Database, Please Try Again Later. " + ex.ToString(), "Error");
只需将 "Error" 作为标题参数即可。