Let me report to you the first problem I could catch at first glance:
for (int k = 0; k < treeView1.Nodes[i].Nodes[j].Nodes.Count; k++) {
}
The problem here is: depending on how you insert, the value of
Count
can grow at the same rate of faster than
k
, so the condition of the ending of the loop may never be met. It can be an infinite loop. In other words, if you need to repeat some loop body exactly
treeView1.Nodes[i].Nodes[j].Nodes.Count
times, you would need a very different code like this:
int nodeCount = treeView1.Nodes[i].Nodes[j].Nodes.Count;
for (int k = 0; k < nodeCount; ++k) {
}
Do you see the difference?
A side note: never ever name any members or variables like
treeView1
. It looks like an auto-generated code which violates (good) Microsoft naming conventions. You should always rename those names to something semantic. And of course, never use the names like
i
,
j
or
k
. In theory, it does not matter, but in practice — a usual source of many inconveniences or even bugs. The names should have semantic meaning and be longer:
index
,
parentIndex
,
childIndex
,
sourceIndex
,
targetIndex
, etc.
—SA