当您尝试通过关键字访问 winforms TreeView 并且该关键字不存在时会发生什么? [英] What happens when you attempt to access a winforms TreeView by keyword and that Keyword is not present?

查看:25
本文介绍了当您尝试通过关键字访问 winforms TreeView 并且该关键字不存在时会发生什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个应用程序,我在其中使用数据库架构填充 TreeView.我通过迭代每个表名并在 GetSchema 中键入来做到这一点.然后,根据数据类型和名称,我选择要向其中添加新项目的父节点.有时,树节点中不存在该项目(取决于用户设置,某些表可能已添加或未添加为树视图的节点),这很好,在这种情况下,我想要:

I'm writing an application where I fill out a TreeView with the schema of a database. I do so by iterating over each table name and type in GetSchema. Then, depending on the DataType and name, I select the parent Node into which I want to add a new item. Sometimes that item does not exist in the treenode (depending on user settings certain tables may or may not have been added as nodes of the treeview), which is fine, in that case I want either:

A) 要抛出的异常,所以我知道它未能按要求找到节点.或者 B) 为失败的访问器返回 null.

A) An exception to be thrown so I KNOW that it failed to find the node as asked for. Or B) null to be returned for the failed accessor.

我的代码的(高度修改的)片段:

A (highly modified) snippet of my code:

TreeNode parent = null;
if( tableName.StartsWith("prefix") )
{
    parent = tablesNode.Nodes["Node Name which might not exist"];
}

if (parent == null && IgnorePrefixedTables)
{
    continue;
}
else if (parent == null)
{
    throw Exception();
}
....<More Code For Filling Out that node>...

问题是,当我到达不存在的节点名称的 tablesNode.Nodes["Node Name which may not exist"] 时,当我逐步执行此代码(或者更确切地说,真正的代码)时,我无法捕获异常,因为没有抛出.如果我进入或越过那行代码,整个方法会将我返回到最高级别(我的表单立即显示并且 UI 部分完成).这是怎么回事?

The problem is, that when I step through this code (or rather, the real code) when I reach tablesNode.Nodes["Node Name which might not exist"] for a node name that doesn't exist I am unable to catch the exception because none is thrown. If I step into or over that line of code the whole method returns me up to the highest level (my form immediately gets shown and the UI is partially complete). What's up with that?

这是我的问题的一个非常简化的版本:

Here's a VERY simplified version of my problem:

namespace TestZone
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            treeView1.Nodes.Add("Hello", "Hello");
            var x = treeView1.Nodes["Hello"];
            x.Nodes.Add("World-PL", "Swiat");
            x.Nodes.Add("World-EN", "World");
            var y = treeView1.Nodes["World-EN"];
            MessageBox.Show(y.Text);
            y = treeView1.Nodes["World-SP"];
            MessageBox.Show(y.Text);
            y = treeView1.Nodes["World-PL"];
            MessageBox.Show(y.Text);
        }
    }
}

代码依赖于 Form1 上的 textBox1.(PS PL 是波兰语).显然,treeView1 也找不到 World-EN,这让我觉得我真的不了解 treeView 的工作原理.第一个 MessageBox 永远不会显示并且断点在 y = treeView1.Nodes["World-SP"];无法中断(因为那行代码永远不会被调用).

The code relies on textBox1 being on Form1. (P.S. PL is Polish). Apparently treeView1 can't find World-EN either which makes me think I'm really not understanding how treeView works. The first MessageBox never gets shown and breakpoints on y = treeView1.Nodes["World-SP"]; fail to break (since that line of code never gets called).

推荐答案

避免使用异常来控制程序流.使用 TreeViewCollection.IndexOfKey() 方法.

Avoid using exceptions to control program flow. Use the TreeViewCollection.IndexOfKey() method.

当窗体的 OnLoad() 方法或 Load 事件中引发异常并且附加了调试器时,64 位操作系统上会出现一个奇怪的错误.它在没有通知的情况下被吞下.听起来像是一场比赛.解决方法是将平台目标设置为 x86.

There's a strange bug on a 64-bit operating system when an exception is raised in the form's OnLoad() method or Load event and a debugger is attached. It is swallowed without notification. Sounds like a match. Workaround is to set the Platform target to x86.

这篇关于当您尝试通过关键字访问 winforms TreeView 并且该关键字不存在时会发生什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆