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) 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>...
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?
[EDIT]
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);
}
}
}
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).