0
votes

I would like to have an array of treenode in such a way that if i add a custom node along with that the remaining Nodes declared in the array should be added as child nodes to that custom node added.

Initially i will have a treeview with a Header node if i righ click on that i will have contextmenu with an option addnew. when i click on that i will have a save file dialog option to save a file and i will bind it as child node to that root node. ALong with that i would like to add some more nodes as child nodes to the binded one

2
The question is not very clear (to me, at least). Can you elaborate a bit on what you want to do?Fredrik Mörk
How important is it that you use Treenode in this situation. Just from what I get out of the question, it almost sounds like XML would be the better choice...Christopher B. Adkins
Fredrik : I have updated my question if not clear i will explain againDeveloper

2 Answers

1
votes

This got the answer for me

private void AddNew_Click(object sender, EventArgs e)
{
    Stream myStream;
    SaveFileDialog saveFileDialog1 = new SaveFileDialog();

    saveFileDialog1.InitialDirectory = @"C:\";
    //saveFileDialog1.CheckFileExists = true;
    //saveFileDialog1.CheckPathExists = true;
    saveFileDialog1.DefaultExt = "txt";

    saveFileDialog1.Filter = "(*.txt)|*.txt";
    saveFileDialog1.FilterIndex = 2;
    saveFileDialog1.RestoreDirectory = true;

    if (saveFileDialog1.ShowDialog() == DialogResult.OK)
    {

        if ((myStream = saveFileDialog1.OpenFile()) != null)
        {

            string FileName = saveFileDialog1.FileName;
            TreeNode newNode = new TreeNode(FileName);
            newNode.SelectedImageIndex = 1;
            tvwACH.SelectedNode.Nodes.Add(newNode);
            newNode.Nodes.Add("FileHeader");
            newNode.Nodes.Add("BatchHeader");
            newNode.Nodes.Add("EntryDetail");
            // TODO: Add code here to save the current contents of the form to a file.
            //myStream.Close();                
        }
    }
}
0
votes

One option would be to simply make the array as stated and then when you want to add them as children of a newly created node foreach through them:

foreach(var node in nodeArray)
    newNode.Nodes.Add(node);

Something along those lines should do the trick for you.