Could someone tell me why this routine runs twice?
private void PopulateTreeView(string county)
{
TreeNode rootNode;
DirectoryInfo info = new DirectoryInfo(@"C:\Users\Scot\Documents\Landman\Runsheets\" +county);
if (info.Exists)
{
rootNode = new TreeNode(info.Name);
rootNode.Tag = info;
GetDirectories(info.GetDirectories(), rootNode);
treeView1.Nodes.Add(rootNode);
}
}
private void GetDirectories(DirectoryInfo[] subDirs,TreeNode nodeToAddTo)
{
TreeNode aNode;
DirectoryInfo[] subSubDirs;
foreach (DirectoryInfo subDir in subDirs)
{
aNode = new TreeNode(subDir.Name, 0, 0);
aNode.Tag = subDir;
aNode.ImageKey = "folder";
subSubDirs = subDir.GetDirectories();
if (subSubDirs.Length != 0)
{
GetDirectories(subSubDirs, aNode);
}
nodeToAddTo.Nodes.Add(aNode);
}
}
I got his from an article on how to populate a treeview. The only change I made was to add a parameter to the call so I could limit the contents of the treeview. But it runs twice. When it finishes there are two top level nodes with the same name and all of the sub nodes are the same.
Thanks for your help.
Loading
Scott StewartPosted Mar 15, 2012, 7:23 AM
Weird, but whatever. Time to move on to the next issue.
Sam HobbsPosted Mar 13, 2012, 2:25 AM
The documentation of the SelectedIndexChanged event does not give a clue for why it would be fired twice. It is typical for events such as this to occur twice; once for the previous item and again for the3 new item. I do not think this is happening here but it is a possibility. I did not take the time to say that since it is irrelevant if the SelectedIndexChanged event handler is executed only once.
I do not like solving problems without understanding the problem unless I have tried to understand and am forced to give up due to time. Trying to save time by not trying to understand is likely to cost a lot of time.
Walter KiessPosted Mar 13, 2012, 12:44 AM
Not sure if this will help. I have had several occasions whereby on populating a control I ended up accidentally calling the population routine again simply by the act of populating the control! Adding a control flag that says "Hey, I'm in the process of populating", checking this flag in the control's _Changed event and then resetting the flag when done may help to overcome this problem.
HTH
Walter
Sam HobbsPosted Mar 13, 2012, 12:27 AM
Scott StewartPosted Mar 12, 2012, 11:52 PM
After I posted I thought about the call and should have included it. So here it is;
private void tblCountiesComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
ComboBox cboCounties = sender as ComboBox;
string v = cboCounties.SelectedValue.ToString();
PopulateTreeView(v);
}
As I stepped through the routine I lost track of what loop I was on. It is recursive but I couldn't follow it. As I step through it the routine is called and runs through all the way then goes back to the calling line and runs again. Recursive calling has always posed a problem with me. Does the program automatically save it's place while the inner loop runs? the variables are named the same so how does it know what loop it is on? Whatever. I got this from the article and I am trying to figure out what calls it twice.
Sam HobbsPosted Mar 12, 2012, 9:39 PM
It is nearly certain that the cause is not in the code you posted; you do not speify where PopulateTreeView is used but that is the likely cause of the problem.