RE: How to show Selected treeview node's attribute,values in listbox t

  • Thread starter Thread starter Frank Uray
  • Start date Start date
F

Frank Uray

Hi

For example you can:
System.Windows.Forms.MessageBox.Show(((System.Windows.Forms.TreeView)sender).SelectedNode.Text);

In AfterSelect and with SelectedNode you can decide
what you like to do ... display something in another control and so on ...


Regards
Frank Uray
 
Hi

Well, it depends on what you like to do ??

With the line:
((System.Windows.Forms.TreeView)sender).SelectedNode
you get the currectly selected node.
In AfterSelect you can take all the informations of this node
and for example fill with this a TextBox or a ListView ... or
do something else ...

What do you need to do ??

Regards
Frank Uray

rasingh via DotNetMonster.com said:
Hi Frank,
tanx 4 replyg.
i cud not get the concept of this line: System.Windows.Forms.MessageBox.Show(
((System.Windows.Forms.TreeView)sender).SelectedNode.Text);

can u plz tell me wat to write in After Select()?
Hi

For example you can:
View)sender).SelectedNode.Text);

In AfterSelect and with SelectedNode you can decide
what you like to do ... display something in another control and so on ...

Regards
Frank Uray
Hi,
I have to create a project in C# in which i have to create a XML Tree View,
[quoted text clipped - 15 lines]
Please help me out as early as possible..
Thanks in advance.
 
Hi

I have never loaded a TreeView directly from XML.
I always first read the XML file into a DataSet and from
there I load the tree recursivly.

But, when you add the Line in AfterSelect
System.Windows.Forms.MessageBox.Show(((System.Windows.Forms.TreeView)sender).SelectedNode.Text);
you can set a BreakPoint there and look what values in SelectedNode.
When you know this, you can add code to fill your ListBox .

Best regards
Frank Uray




rasingh via DotNetMonster.com said:
i have a browse button to load an Xml file and a textbox to show the path of
file and a list box to show the attributes of a node selected in the Tree
view.
My code is like this:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Xml;
using System.Text;
using System.Windows.Forms;

namespace LoadXMLtreeDisplay
{
public partial class TreeDisplay : Form
{
//Declaring Treeview in Listbox
TreeView listViewobj = new TreeView();

//Declare XML Document object
XmlDocument xdoc = new XmlDocument();
//Constructor of class
public TreeDisplay( )
{
InitializeComponent();
this.Controls.Add(treeViewObj);
}//constructor

private void treeDocLoadMethod(string nameofFile)
{
try
{
txtBoxfile.Text = nameofFile;

//Create XML document & load the XML file.
xdoc.Load(nameofFile);

this.treeViewObj.Nodes.Clear();
this.treeViewObj.Nodes.Add(new TreeNode(xdoc.DocumentElement.
Name));
TreeNode tNodeObj = new TreeNode();
tNodeObj = this.treeViewObj.Nodes[0];

XmlNodeList nodeList = xdoc.SelectNodes
("//settings/tables/table/name[. ='{0}']");
XmlNode xNode = xdoc.DocumentElement;//nodeList.Item(0).
ParentNode;

AddNodes(treeViewObj.Nodes, xdoc.DocumentElement);
ConvertAllXmlnodetoTreenode(xdoc, treeViewObj.Nodes);

treeViewObj.Nodes[0].Expand();
treeViewObj.CollapseAll();

Cursor = System.Windows.Forms.Cursors.Default;
}

catch (XmlException xmlex)
{
MessageBox.Show(xmlex.Message, "ERROR");
}//catch
catch(Exception exp)
{
txtBoxfile.Text = exp.Message;
}
}//treeDocLoadMethod

private void ConvertAllXmlnodetoTreenode(XmlNode xmlNode,
TreeNodeCollection treeNodeCollection)
{
TreeNode treeNodeobj = treeNodeCollection.Add(xmlNode.Name);

switch (xmlNode.NodeType)
{
case XmlNodeType.ProcessingInstruction:
case XmlNodeType.XmlDeclaration:
treeNodeobj.Text = "<?" + xmlNode.Name + " " + xmlNode.
Value + "?>";
break;
case XmlNodeType.Element:
treeNodeobj.Text = "<" + xmlNode.Name + ">";
break;
case XmlNodeType.Attribute:
treeNodeobj.Text = "ATTRIBUTE: " + xmlNode.Name;
listBoxshow.Items.Add(treeNodeobj.Text);
break;
case XmlNodeType.Text:
case XmlNodeType.CDATA:
treeNodeobj.Text = xmlNode.Value;
listBoxshow.Items.Add(treeNodeobj.Text);
break;
case XmlNodeType.Comment:
treeNodeobj.Text = "<!--" + xmlNode.Value + "-->";
break;
}//switch
if (xmlNode.Attributes != null)
{
foreach (XmlAttribute attribute in xmlNode.Attributes)
{
ConvertAllXmlnodetoTreenode(attribute, treeNodeobj.Nodes)
;

}
}
foreach (XmlNode childNode in xmlNode.ChildNodes)
{
ConvertAllXmlnodetoTreenode(childNode, treeNodeobj.Nodes);

}
}// ConvertAllXmlnodetoTreenode Method

private void ConvertAddTreeNodestoTree(XmlNode xnode, TreeNode tnode)
{
XmlNode xNode;
TreeNode treeNode;
XmlNodeList nodeList;

if (xnode.HasChildNodes)
{
nodeList = xnode.ChildNodes;
for (int i = 0; i <= nodeList.Count-1;i++)
{
xNode = xnode.ChildNodes;
tnode.Nodes.Add(new TreeNode(xNode.Name));
treeNode = tnode.Nodes;
ConvertAllXmlnodetoTreenode(xNode,treeNode.Nodes);
}//for
}//if
else
{
tnode.Text = xnode.OuterXml.Trim();
}//else
}
private void btnBrowse_Click(object sender, EventArgs e)
{
txtBoxfile.Clear();
listBoxshow.Items.Clear();
listBoxeg.Items.Clear();
ExpandBtn.Text = "Expand TreeNodes";
OpenFileDialog open = new OpenFileDialog();
//open.InitialDirectory = @"C:\";
open.Filter = "XML Files(*.xml)|(*.xhtml)|All files(*.*)|*.*";
open.FilterIndex = 2;
open.RestoreDirectory = true;

if (open.ShowDialog(this)== DialogResult.OK)
{
txtBoxfile.Text = open.FileName;
treeDocLoadMethod(open.FileName); //this variable gives the
name of selected file
}

}//Browse button

private void treeViewObj_AfterSelect(object sender, TreeViewEventArgs
e)
{
//listBoxshow.Items.Clear();

listBoxeg.Items.Clear();
XmlNode xNode = e.Node.Tag as XmlNode;
XmlAttributeCollection xmlAttColl = xNode.Attributes;
String name = "<" + xNode.Name;
if (xNode != null)
{
foreach (XmlNode subNode in xNode.ChildNodes)
{
listBoxeg.Items.Add(subNode.Name);
}
}
}//treeViewObj_AfterSelect

private void AddNodes(TreeNodeCollection treeNodes, XmlNode xNode)
{
TreeNode subNode = treeNodes.Add(xNode.Name);
subNode.Tag = xNode;
foreach (XmlNode subElement in xNode.ChildNodes)
{
AddNodes(subNode.Nodes, subElement);
}
}
}//class
}//namespace

But i am unable to get the desired results.i want wen a user selects a node
in treeview its attributes and values should be displayed in listbox.
can u help me providing the right code to be written in AfterSelect()
Tanx again
Regards
RAch


Frank said:
Hi

Well, it depends on what you like to do ??

With the line:
((System.Windows.Forms.TreeView)sender).SelectedNode
you get the currectly selected node.
In AfterSelect you can take all the informations of this node
and for example fill with this a TextBox or a ListView ... or
do something else ...

What do you need to do ??

Regards
Frank Uray
Hi Frank,
tanx 4 replyg.
[quoted text clipped - 19 lines]
Please help me out as early as possible..
Thanks in advance.
 
Back
Top