472,340 Members | 1,830 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,340 software developers and data experts.

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

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

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


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

// SECTION 2. Initialize the TreeView control.
treeView1.Nodes.Clear();
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);
treeView1.ExpandAll();
}
catch (XmlException xmlEx)
{
MessageBox.Show(xmlEx.Message);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}

}

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)
{

listView1.Items.Clear();
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;

node.Nodes.Add(str);

}


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.

regards,
Divya
Feb 5 '08 #1
0 3154

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

Similar topics

3
by: Steve | last post by:
Visual Studio 2003 .NET / C# I have a treeview object on a form which acts as the main menu controller for my application. the treeview is...
2
by: worzel | last post by:
Can't suss this out for the life of me, googling of no help either. Okay, new to win forms; I have a treeview with several root nodes, each...
1
by: clintonG | last post by:
How do I get a TreeNode.Parent property when using the 2.0 TreeView control? When the data source is an XML file there may be redundant names in...
2
by: Sean | last post by:
Hi, I have a treeview and user can right click the treenodes, depending on the nodes, different shortcut menu will appear. I want only the...
1
by: Sergey Romanov | last post by:
I have app like Computer Managment app. on windows. I have everithing in tree. I whant deferent context menu appear on deferent treenode items. How...
2
by: paradox | last post by:
How can I go about having a TreeNode selected when I right click? I tried using the Click event, but the event does not pass in the node that was...
3
by: vijaynats | last post by:
I have a treeview with a ContextMenu attached. When i click on a node, AfterSelect fires but does not fire when right clicked (the context menu...
9
by: vincent90152900 | last post by:
How to pop up different text base on the selected TreeNode? I want to pop up different Text base on the selected TreeNode of the TreeView component....
4
by: ohad weiss | last post by:
Hi all I have a question abount renaming a selected treenode. My user can place the cursor on a treeview, click on the right button of the...
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.