【问题标题】:Problem with MessageBox.Show in catch捕获中的 MessageBox.Show 问题
【发布时间】:2009-03-20 12:54:59
【问题描述】:

当我尝试运行以下代码时,它会导致未处理的异常。在对代码进行了多次修改后,我发现如果注释掉 MessageBox.Show 行,问题就会消失!不同寻常的是,我在代码的其他部分的其他 catch{ } 段中使用了 MessageBox.Show 语句,没有任何问题。我的问题是有人知道它为什么会导致异常吗?

(P.s Reports_Group_Chooser 是一个组合框)

代码:

string GroupName= (string)Reports_Group_Chooser.SelectedItem;
byte[] ConfigBytes= new byte[]{};
try{
    ConfigBytes= File.ReadAllBytes("Reports/"+ GroupName.ToLower() +".grp");
}catch{
    MessageBox.Show("The file for this group is missing. Cannot continue.","File Error",MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
    Reports_Group_Chooser.Items.RemoveAt(NewGroup);
    Reports_Group_Chooser.SelectedIndex= 0;
}

错误(大部分):

未处理的异常: System.NullReferenceException:对象 引用未设置为 对象在 System.Windows.Forms.ComboBox.DropDownListBoxFinished () [0x00000] 在 (包装 远程调用检查) System.Windows.Forms.ComboBox:DropDownListBoxFinished () 在 System.Windows.Forms.ComboBox+ComboListBox.HideWindow () [0x00000] 在 System.Windows.Forms.ComboBox+ComboListBox.OnMouseUp (System.Windows.Forms.MouseEventArgs e) [0x00000] 在 System.Windows.Forms.Control.WmLButtonUp (System.Windows.Forms.Message&m) [0x00000] 在 System.Windows.Forms.Control.WndProc (System.Windows.Forms.Message&m) [0x00000] 在 System.Windows.Forms.ComboBox+ComboListBox.WndProc (System.Windows.Forms.Message&m) [0x00000] 在 System.Windows.Forms.Control+ControlWindowTarget.OnMessage (System.Windows.Forms.Message&m) [0x00000] 在 System.Windows.Forms.Control+ControlNativeWindow.WndProc (System.Windows.Forms.Message&m) [0x00000] 在 System.Windows.Forms.NativeWindow.WndProc (IntPtr hWnd, Msg msg, IntPtr wParam, IntPtr lParam) [0x00000] 在 System.Windows.Forms.XplatUIX11.DispatchMessage (System.Windows.Forms.MSG&味精) [0x00000] 在 System.Windows.Forms.XplatUI.DispatchMessage (System.Windows.Forms.MSG&味精) [0x00000] 在 System.Windows.Forms.Application.RunLoop (布尔模态, System.Windows.Forms.ApplicationContext 上下文)[0x00000]

任何帮助表示赞赏 迈克尔

更新 这是一个在我的代码中工作的 MessageBox.Show 示例,它不会导致错误:

GlobalConfig= new Dictionary<string, string>();
byte[] ConfigBytes= new byte[]{};
try{
    ConfigBytes= System.IO.File.ReadAllBytes("Config.cfg");
}catch{
    MessageBox.Show("Global ettings file does not exist. Cannot continue.","File Error",MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
    GlobalConfig.Add("StoreNumber","");
    GlobalConfig.Add("Error","Y");
}

更新更新:

似乎问题只是在组合框事件中有 MessageBox.Show: 以下代码仍然出现同样的错误:

private void Reports_GroupChanged(object sender,EventArgs e){
    MessageBox.Show("The file for this group is missing. Cannot continue.","File Error",MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}

【问题讨论】:

  • 您的错误是组合框而不是消息框。问题是调用堆栈不包含行号。这是在 WPF 中吗? APS.NET? Windows 论坛?
  • 抱歉,我对 C# 编程非常陌生,您的意思是“这是在 WPF 中吗?APS.NET?Windows 论坛?”

标签: c# try-catch unhandled-exception


【解决方案1】:

当您显示 MessageBox 时,它不会暂停您的应用程序。相反,应用程序继续从操作系统中抽取消息。实际上,这允许您的 UI 继续处理。

这里可能发生的是,当 MessageBox 显示时,ComboBox 仍在处理鼠标按钮向上消息和空引用。请尝试使用以下调用。

System.Diagnostics.Debugger.Break();

【讨论】:

  • 我把 System.Diagnostics.Debugger.Break();在 MessageBox 之后的代码中,我没有收到任何错误,但我不希望程序在此错误上停止:(。
  • @redorkulated,它有助于缩小问题范围。在显示消息框之前尝试禁用 ComboBox。
  • hmm nope 在方法开始时禁用了组合框,但仍然出现相同的错误。
【解决方案2】:

先修复错误。

Reports_Group_Chooser.SelectedIndex= 0;
Reports_Group_Chooser.Items.RemoveAt(NewGroup);    
MessageBox.Show("The file for this group is missing. Cannot continue.","File Error",MessageBoxButtons.OK, MessageBoxIcon.Exclamation);    

【讨论】:

  • 我按照你的建议换了行,但这并没有什么不同。当 MessageBox 行未注释时,这两行工作正常,我是否收到错误。在捕获之后,我还将 MessageBox 移到了它自己的段中。 if(ConfigBytes.Length == 0){ MsgBox... } 还是出现了同样的错误。
【解决方案3】:

您应该在显示消息之前解决导致失败的问题。

下面是我的例子。

失败是由于将空对象转换为字符串引起的:

string str = dgv[e.ColumnIndex, e.RowIndex].Value.ToString();

然后在 catch 语句中我尝试显示消息并将先前的值分配给单元格:

MessageBox.Show(String.Format("Value must be between {0} and {1}.", minVal, maxVal));
dgv[e.ColumnIndex, e.RowIndex].Value = previousValue;

在调用 MessageBox 时出现空引用异常。

因此有必要在调用 MessageBox 之前修复单元格值(交换行),它就像一个魅力。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-20
    • 2011-11-22
    • 1970-01-01
    • 2011-03-09
    • 2012-12-29
    • 2011-03-16
    相关资源
    最近更新 更多