C# InvalidOperationException
Nodo N;
foreach (string S in listBox_nodos.Items)
{
N = graph.getNodoName(S);
string comp = (string) listBox_nodos.SelectedItem;
if (comp == S)
System.Console.WriteLine(N.NAME);
}
我收到 InvalidOperationException
并且应用程序崩溃了。
不明白为什么。有什么帮助吗?谢谢。
编辑:“N.NAME”打印!然后崩溃了。
EDIT2:我试图捕获异常,应用程序无论如何都会崩溃。
Nodo N;
foreach (string S in listBox_nodos.Items)
{
N = graph.getNodoName(S);
string comp = (string) listBox_nodos.SelectedItem;
if (comp == S)
System.Console.WriteLine(N.NAME);
}
I get InvalidOperationException
and the application crashes.
Can't get why. Any help? Thanks.
EDIT: The 'N.NAME' prints! And then goes the crash.
EDIT2: I've tried to catch the Exception, the application crashes anyway.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您在哪一行收到异常?在不知道
listBox_nodos
具有什么数据类型的情况下,我的第一个猜测是Items
不包含大量string
,而是ListBoxItem
At which line do you get the exception? Without knowing what datatype
listBox_nodos
has, my first guess w2ould be thatItems
does not contain a lot ofstring
s, but ratherListBoxItem
s如前所述,完整的异常会很高兴看到(如果在 VS 调试器中,您可以从异常帮助器对话框或 Debug -> Windows -> Locals 中的 $exception 条目获取它)。最坏的情况你应该能够尝试 { .. } catch (Exception ex) { System.Console.WriteLine(ex); (异常的 ToString 包括堆栈跟踪)
当尝试显示内容时,可能还想尝试 MessageBox.Show,以防万一由于某种原因使用 Console 时出现异常。
与异常无关,但看起来您正在对列表框项目进行交互以查找所选项目 - 如果这是真的,为什么不直接使用所选项目?
as mentioned before, the full exception would be nice to see (if in the VS debugger, you can get it from the exception helper dialog or the $exception entry in Debug -> Windows -> Locals). Worst-case you should be able to try { .. } catch (Exception ex) { System.Console.WriteLine(ex); (Exception's ToString includes the stack trace)
when trying to display things, may also want to try MessageBox.Show just in case the exception is coming from the use of Console for some reason.
unrelated to the exception, but it looks like you're interating over the listbox items to find the selected item - if that's true, why not just use the selected item directly?
我以前也遇到过类似的问题,也与所选项目有关。我对其进行了广泛的调查,但未能找到解决方法。我最终重新制作了表单(尽管我认为只有控件是必要的),这似乎解决了它。
I have had a similar problem before, also related to selected items. I investigated it extensively and was not able to find a fix. I eventually wound up remaking the form (though I think just the control was necessary) and that seemed to fix it.