Следующий код:
Код:
public class Trees
{
public Trees()
{
}
public Trees(int NoteID, int ParentNoteID, string NoteName)
{
this.NoteID = NoteID;
this.ParentNoteID = ParentNoteID;
this.NoteName = NoteName;
}
public int NoteID { get; private set; }
public int ParentNoteID { get; private set; }
public string NoteName { get; private set; }
}
private void saveButton_Click(object sender, EventArgs e)
{
int processed = 0;
bool root = true;
saveTreeNode(treeView1.Nodes, 0, ref processed, ref root);
string str = String.Empty;
foreach (Trees t in l)
str += "\t" + t.NoteID + "\t" + t.ParentNoteID + "\t" + t.NoteName + Environment.NewLine;
MessageBox.Show(str);
str = String.Empty;
l.Clear();
}
List<Trees> l = new List<Trees>();
private void saveTreeNode(TreeNodeCollection nodes, int NoteID, ref int processed, ref bool root)
{
foreach (TreeNode node in nodes)
{
int ParentNoteID;
if (root)
{
ParentNoteID = -1;
root = false;
}
else
ParentNoteID = NoteID - 1;
l.Add(new Trees(NoteID, ParentNoteID, node.Text));
++processed;
saveTreeNode(node.Nodes, NoteID + 1, ref processed, ref root);
root = true;
NoteID = processed;
}
}
Выводит правильный результат только в случае линейного дерева, т.е.:
читать дальше »
/ 0 -1 parent
- 1 0 child
-- 2 1 child
-- 3 2 child
--- 4 3 child
/ 5 -1 parent
- 6 5 child
-- 7 6 child
--- 8 7 child
Но в случае не линейного дерева получается совсем не то что хотелось бы:
читать дальше »
/ 0 -1 parent
- 1 0 child
--2 1 child
--3 -1 child
- 4 -1 child
- 5 -1 child
/ 6 -1 parent
- 7 6 child
- 8 -1 child
- 9 -1 child