473,703 Members | 2,308 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

1 New Member
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.Collecti ons.Generic;
using System.Componen tModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows. Forms;
using System.Xml ;

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

public Form1()
InitializeCompo nent();

private void Form1_Load(obje ct sender, EventArgs e)
String st1 = Application.Sta rtupPath + "\\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 .Clear();
treeView1.Nodes .Add(new TreeNode(dom.Do cumentElement.N ame));
TreeNode tNode = new TreeNode();
tNode = treeView1.Nodes[0];

// SECTION 3. Populate the TreeView with the DOM nodes.
AddNode(dom.Doc umentElement, tNode);
treeView1.Expan dAll();
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.HasC hildNodes)
nodeList = inXmlNode.Child Nodes;
max = nodeList.Count;
for (cnt = 0; cnt <= max - 1; cnt++)
xNode = inXmlNode.Child Nodes[cnt];
inTreeNode.Node s.Add(new TreeNode(xNode. Name));
tNode = inTreeNode.Node s[cnt];
AddNode(xNode, tNode);


// Adding Items from TreeView to Listview After Selecting
private void treeView1_After Select(object sender, TreeViewEventAr gs e)

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

//Display Item in Tile View
private void tilesToolStripM enuItem_Click(o bject sender, EventArgs e)
listView1.View = View.Tile;
listToolStripMe nuItem.Checked = false;
detailsToolStri pMenuItem.Check ed = false;
iconsToolStripM enuItem.Checked = false;

//Display Item in Large Icon View
private void iconsToolStripM enuItem_Click(o bject sender, EventArgs e)
listView1.View = View.LargeIcon;
tilesToolStripM enuItem.Checked = false;
listToolStripMe nuItem.Checked = false;
detailsToolStri pMenuItem.Check ed = false;

//Display items in list View
private void listToolStripMe nuItem_Click(ob ject sender, EventArgs e)
listView1.View = View.List;
tilesToolStripM enuItem.Checked = false;
detailsToolStri pMenuItem.Check ed = false;
iconsToolStripM enuItem.Checked = false;

//Display Items in Detail View
private void detailsToolStri pMenuItem_Click (object sender, EventArgs e)
listView1.View = View.Details;
tilesToolStripM enuItem.Checked = false;
listToolStripMe nuItem.Checked = false;
iconsToolStripM enuItem.Checked = false;

private void newNodeToolStri pMenuItem_Click (object sender, EventArgs e)
int index;
string str = Microsoft.Visua lBasic.Interact ion.InputBox("A DD NODE","Input For new node", "", 10, 20);

TreeNode node = treeView1.Selec tedNode;

index = treeView1.Selec tedNode.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. newNodeToolStri pMenuItem_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
0 3308

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

Similar topics

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 always in sight, and the form it is on acts as the main container for the whole application. All other screens are UserControls, which get added and removed from/to the main form, when the user clicks an item on the tree view. I thought the best way to handle navigating around would be to define...
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 having a single level of child nodes underneath. I also havea context (popup) menu which, what I want, is for it to pop up with certain options (menuItems) when a root treeNode is selected and different options when a child node is selected. It is all working well except for one problem - if I right...
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 the tree. For example, when a child node with the value of 'name' is selected I don't have a clue how to get the name of its parent which may be 'person' or 'school.' <%= Clinton Gallagher
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 shortcut menu to appear if the point of the right mouse click is within the label text of the treenode instead of row-wide.
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 can I do this?
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 clicked. Currently you have to left click a node in order to select it. I want to be able to write click and bring up a context menu with only certain options available depending on the node selected. I got the context menu to come up (which is very easy to do), however, you first have to left...
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 pops up). (Background: I have loaded a list of disk file names onto the tree and have to open the file thru the contextmenu for editing. It opens the file whose treeview node was selected earlier using a left mouse click). Any solutions?
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. So, I create a TreeView and a ModalPopupExtender. Following is my codes. <asp:ScriptManager ID="ScriptManager1" runat="server"> </asp:ScriptManager> <asp:TreeView ID="TreeView1" runat="server" OnSelectedNodeChanged="TreeView1_SelectedNodeChanged"> <Nodes>...
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 mouse, and a contextmenue is beeing populated, in which he can Add, rename or delete a node from the treeview. When he select the new option, the code I use is: treeview1.Nodes.Add("New")
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.