C# – How to have an array of Treenode

ctreeviewwinforms

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

Best Answer

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();                
        }
    }
}