Hi,
Developing a trad win app using VS 2010 C#.
Starting from loading a xml file into a XMLDocument object.
openedFile = someFile.xml; |
private void AddNodeToTreeView(ref XmlNode inXmlNode, ref TreeNode inTreeNode) |
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.
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 HobbsPosted Apr 7, 2011, 1:48 PM
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.
PeerPosted Apr 8, 2011, 7:48 AM
Sam HobbsPosted Apr 8, 2011, 3:40 AM
PeerPosted Apr 8, 2011, 3:06 AM
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!
PeerPosted Apr 7, 2011, 5:36 AM
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.
FroglegPosted Apr 7, 2011, 5:18 AM
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)
{
}
}
}
PeerPosted Apr 7, 2011, 2:59 AM
Sam HobbsPosted Apr 6, 2011, 6:13 PM
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.