473,394 Members | 1,663 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 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 3264

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 always in sight, and the form it is on acts as the...
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 having a single level of child nodes underneath. I also...
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 the tree. For example, when a child node with the...
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 shortcut menu to appear if the point of the right...
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 can I do this?
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 clicked. Currently you have to left click a node...
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 pops up). (Background: I have loaded a list of...
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. So, I create a TreeView and a ModalPopupExtender....
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 mouse, and a contextmenue is beeing populated, in which...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.