C# 如何为具有多个类和属性的treeview节点分配标记

C# 如何为具有多个类和属性的treeview节点分配标记,c#,winforms,treeview,C#,Winforms,Treeview,我有大约4-5个类,它们与超级类嵌套,为其他类创建保持指针,这些类保存并组织信息,以便在两个单独的TreeView窗口中显示信息。现在的问题是,当我尝试在文本框中显示一些文本时,mouseclick eventfrom treeview被事件触发。我尝试使用tag属性并将指针分配给子节点,但由于某些原因,它只返回带有rootclases节点的对象,而不返回它们的子节点 以下是我指定对象标记的部分: foreach (HostClass ptr in HostClassHolderlist)

我有大约4-5个类,它们与超级类嵌套,为其他类创建保持指针,这些类保存并组织信息,以便在两个单独的TreeView窗口中显示信息。现在的问题是,当我尝试在文本框中显示一些文本时,mouseclick eventfrom treeview被事件触发。我尝试使用tag属性并将指针分配给子节点,但由于某些原因,它只返回带有rootclases节点的对象,而不返回它们的子节点

以下是我指定对象标记的部分:

foreach (HostClass ptr in HostClassHolderlist)
{
 subpcirootnode = pcirootnode.Nodes.Add("PCI CONFIG REGISTERS");

 foreach (KeyValuePair<string, UInt32> entry in ptr.PrintHT)
 {
        treeView1.CheckBoxes = true;
        uint tmp = entry.Value;
        subpcirootnode.Tag = ptr;//hostclass pointer
        subpcirootnode.Nodes.Add(entry.Key.ToString() + ":  0x" + tmp.ToString("X"));
  }
}
foreach(HostClassHolderlist中的HostClass ptr)
{
SubCirootNode=pcirootnode.Nodes.Add(“PCI配置寄存器”);
foreach(ptr.PrintHT中的KeyValuePair条目)
{
treeView1.checkbox=true;
uint tmp=输入值;
subscirootnode.Tag=ptr;//主机类指针
subCirootNode.Nodes.Add(entry.Key.ToString()+“:0x”+tmp.ToString(“X”);
}
}
以下是我在中得到错误的部分:

   void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
    //handler for mouse click,this method show on textbox info on field
    {     
        try
        {
            int num = e.Node.Text.IndexOf(" ");
            String key = e.Node.Text.Substring(0,num-1);
            //MessageBox.Show(key);
            HostClass ptr = (HostClass)e.Node.Tag; // <-this is the tricky part of my code!!
            textBox1.Text = ptr.PcidescDict[key];
         }
     }
void treeView1\u NodeMouseClick(对象发送方,TreeNodeMouseClickEventArgs e)
//用于鼠标单击的处理程序,此方法显示在字段的文本框信息上
{     
尝试
{
int num=e.Node.Text.IndexOf(“”);
String key=e.Node.Text.Substring(0,num-1);
//MessageBox.Show(键);

HostClass ptr=(HostClass)e.Node.Tag;//您没有将标记分配给子节点:

subpcirootnode.Nodes.Add(entry.Key.ToString() + ":  0x" + tmp.ToString("X"));
因此,将其更改为:

TreeNode tn = new TreeNode(entry.Key.ToString() + ":  0x" + tmp.ToString("X"));
tn.Tag = ptr;
subpcirootnode.Nodes.Add(tn);

对您的代码了解不够,但所有子节点都获得相同的类引用,因此不清楚这有什么好处。只需检查e.Node.Parent类是否为null,并检查父节点上的Tag属性,就可以获得相同的信息。

将代码张贴在分配值并尝试读取它的位置。y是什么我们正在尝试做的应该很好。代码有点长(大约3000行,包含多个类),我可以在代码中显示我尝试读取标记值并获取异常的部分?我改为使用e.Node.Parent,谢谢你的提示,否则我不会考虑它。