By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,906 Members | 1,715 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,906 IT Pros & Developers. It's quick & easy.

add new treenode to treeview as child of selected node by Context menu

P: 1
Create a windows c# application which will

Read a xml file and populate nodes in the treeview.
1 On selection of treenode display the child nodes of that node in listview control

2. Provide following view properties to listview, through View menu

a. Tile

b. Icon

c. Details

d. List

3. Provide context menu on right click of treenode, which will add new treenode to treeview. Also, write that node in the xml file.

i have copleted till 3rd and want to complete 4th step.

My Code is-

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml ;

namespace WindowsApplication6
public partial class Form1 : Form
XmlDocument dom = new XmlDocument();
TreeNode tNode;
TreeNode n = new TreeNode();

public Form1()

private void Form1_Load(object sender, EventArgs e)
String st1 = Application.StartupPath + "\\desktop.xml";
this.Text = "TreeView control from XML";

// SECTION 1. Create a DOM Document and load the XML data into it.
//XmlDocument dom = new XmlDocument();

// SECTION 2. Initialize the TreeView control.
treeView1.Nodes.Add(new TreeNode(dom.DocumentElement.Name));
TreeNode tNode = new TreeNode();
tNode = treeView1.Nodes[0];

// SECTION 3. Populate the TreeView with the DOM nodes.
AddNode(dom.DocumentElement, tNode);
catch (XmlException xmlEx)
catch (Exception ex)


private void AddNode(XmlNode inXmlNode, TreeNode inTreeNode)
//Local variable delaration
XmlNode xNode;
//TreeNode tNode;
XmlNodeList nodeList;
int cnt, max;

// Loop through the XML nodes until the leaf is reached.
// Add the nodes to the TreeView during the looping process.
if (inXmlNode.HasChildNodes)
nodeList = inXmlNode.ChildNodes;
max = nodeList.Count;
for (cnt = 0; cnt <= max - 1; cnt++)
xNode = inXmlNode.ChildNodes[cnt];
inTreeNode.Nodes.Add(new TreeNode(xNode.Name));
tNode = inTreeNode.Nodes[cnt];
AddNode(xNode, tNode);


// Adding Items from TreeView to Listview After Selecting
private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)

foreach (TreeNode t1 in treeView1.SelectedNode.Nodes)
listView1.Items.Add(t1.Text, t1.ImageIndex);
}//End For

//Display Item in Tile View
private void tilesToolStripMenuItem_Click(object sender, EventArgs e)
listView1.View = View.Tile;
listToolStripMenuItem.Checked = false;
detailsToolStripMenuItem.Checked = false;
iconsToolStripMenuItem.Checked = false;

//Display Item in Large Icon View
private void iconsToolStripMenuItem_Click(object sender, EventArgs e)
listView1.View = View.LargeIcon;
tilesToolStripMenuItem.Checked = false;
listToolStripMenuItem.Checked = false;
detailsToolStripMenuItem.Checked = false;

//Display items in list View
private void listToolStripMenuItem_Click(object sender, EventArgs e)
listView1.View = View.List;
tilesToolStripMenuItem.Checked = false;
detailsToolStripMenuItem.Checked = false;
iconsToolStripMenuItem.Checked = false;

//Display Items in Detail View
private void detailsToolStripMenuItem_Click(object sender, EventArgs e)
listView1.View = View.Details;
tilesToolStripMenuItem.Checked = false;
listToolStripMenuItem.Checked = false;
iconsToolStripMenuItem.Checked = false;

private void newNodeToolStripMenuItem_Click(object sender, EventArgs e)
int index;
string str = Microsoft.VisualBasic.Interaction.InputBox("ADD NODE","Input For new node", "", 10, 20);

TreeNode node = treeView1.SelectedNode;

index = treeView1.SelectedNode.Index;



My problem-I am not able to Add node on selected node.I want to add new node as child of selected node.(viz. newNodeToolStripMenuItem_Click() event)
i have problem in finding index of selected node through coding.

Plz try to help me out i am in need.

Feb 5 '08 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.