Hi,
Developing a trad win app using VS 2010 C#.
Starting from loading a xml file into a XMLDocument object.
Code |
C# |
Now I have a multi column ListView to show the attributes whenever user selects a node from the TreeView component. Here's where it all gets totally wrong. My first idea was to use the tv.SelectedNode.Index (in the TreeView event listener _AfterSelect). But since this index is calculated relatively its parent the mapping to the xmlNodes is lost.
C#
private void treeView_AfterSelect(object sender, TreeViewEventArgs e, TreeView tv, ListView lv)
{
lv.Items.Clear();
ListViewItem lvItem;
currentNodeName = tv.SelectedNode.Text;
int currentNodeIx = tv.SelectedNode.Index;
xmlNodes = Global.mainXMLDoc.GetElementsByTagName(currentNodeName);
XmlNode xNode = xmlNodes.Item(currentNodeIx);
string attr;
string val;
for (int i = 0; i < xNode.Attributes.Count; i++)
{
attr = xNode.Attributes[i].Name;
val = xNode.Attributes[i].Value;
lvItem = new ListViewItem(attr);
string foo = FindXPath(xNode.Attributes[i]);
lvItem.SubItems.Add(val);
lvItem.SubItems.Add(foo);
lv.Items.Add(lvItem);
}
}So, is there a way you can do this using these standard components or do I need to design my own?
Cheers!
Cheers!

Sam HobbsRank #107 · 20.5K reputationPosted Apr 7, 2011, 9:48 AM EDT
A more advanced solution would be to derive a class from TreeNode and then you could put whatever you needed to in that class. Then whenever you had a TreeNode you would have the additional members too.
Sam HobbsRank #107 · 20.5K reputationPosted Apr 6, 2011, 2:13 PM EDT
You sure chose to jump right into deep waters. I get the impression that people don't want to study all that code.
Can you try to describe what you need to do? Instead of just showing code. Try to make it interesting; avoid many details. The more interesting it is to read, the more likely you will get help.
Peer9 reputationPosted Apr 6, 2011, 10:59 PM EDT
FroglegRank #1,020 · 1.9K reputationPosted Apr 7, 2011, 1:18 AM EDT
namespace treeNodes
{
public partial class Form1 : Form
{
TreeNode rootNode, sub1Node, sub2Node;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
rootNode = treeView1.Nodes.Add("RootNode");
rootNode.Tag = ("Xml one");
rootNode.Text = ("Xml one too");
sub1Node = rootNode.Nodes.Add("sub1Node");
sub1Node.Tag = ("Xml two");
sub1Node.Text = ("Xml two too");
sub2Node = sub1Node.Nodes.Add("sub2Node");
sub2Node.Tag = ("Xml three");
sub2Node.Text = ("Xml three too");
}
private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
{
MessageBox.Show(treeView1.SelectedNode.Tag.ToString() + ", " + treeView1.SelectedNode.Text);
}
private void treeView1_BeforeExpand(object sender, TreeViewCancelEventArgs e)
{
}
}
}
Peer9 reputationPosted Apr 7, 2011, 1:36 AM EDT
The TreeNode.Text in my app would get the value from a specific node in my xml. Now what I want is when selecting this TreeNode I need to know what xml node this was generated from. Anyway, I think I might have come up with a solution:
By using the HashCode in the xml. This should give me an unique identifier for each node I add.
Peer9 reputationPosted Apr 7, 2011, 11:06 PM EDT
However, so far my HashCode idea IS working or am I wrong again? so changing this to your solution might not be necessary even though that should be a more correct way of doing it.
Thanks!
Sam HobbsRank #107 · 20.5K reputationPosted Apr 7, 2011, 11:40 PM EDT
Peer9 reputationPosted Apr 8, 2011, 3:48 AM EDT