在C#中使用树视图时,文本字段保存到一个数组(从阵列中提取数据)数组、阵列、字段、视图

2023-09-03 15:27:26 作者:被撕碎的爱情

好吧,我有我用来显示一些节点的树视图。我要附加数据(三个TextBox场)到每个节点,但我不希望它显示在树中。我想如果可能的话将数据保存到一个String []数组。我想在盒子中的数据保存到数组当我点击树中的一个新节点上拉从阵列中的信息的新节点。

Ok, I have a treeview which I am using to display a number of nodes. I wish to attach data (three textbox fields) to each node but I do not want to show it in the tree. I wish to save the data to a string[] Array if possible. I want the data in the boxes to save to the Arrays when I click on a new node in the tree and pull the information from the Arrays for the new node.

由于某些原因,code我不起作用。它不保存信息,有时它只是显示在文本框中随机数据,我点击一下。在code是:

For some reason the code I have does not work. It doesn't save the information and sometimes it just shows random data in the text boxes as I click about. The code is:

    These are global variables within the form:
    string[] desc1;
    string[] desc2;
    string[] desc3;

 private void treeView1_BeforeSelect(object sender, TreeViewEventArgs e)
    {

          // save the entered text into the local variables
        desc1[treeView1.SelectedNode.Index] = textBox4.Text;
        desc2[treeView1.SelectedNode.Index] = textBox5.Text;
        desc3[treeView1.SelectedNode.Index] = textBox6.Text;

    }

    private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
    {

        // update labels
        label23.Text = treeView1.SelectedNode.Text.ToString();
        label24.Text = (treeView1.SelectedNode.Index + 1).ToString();

        // enable all textbox fields
        textBox4.Enabled = true;
        textBox5.Enabled = true;
        textBox6.Enabled = true;

        // clear all textbox fields
        textBox4.Text = null;
        textBox5.Text = null;
        textBox6.Text = null;



        // if parent is selected then show as unselected - if not update text fields
        if (treeView1.SelectedNode.Text.ToString() == "Parent Name")
        {
            label23.Text = "Unselected";
            label24.Text = "Unselected";
            textBox4.Enabled = false;
            textBox5.Enabled = false;
            textBox6.Enabled = false;
        }
        else
        {
            // show the information from the array in the text fields
            textBox4.Text = desc1[treeView1.SelectedNode.Index];
            textBox5.Text = desc2[treeView1.SelectedNode.Index];
            textBox6.Text = desc3[treeView1.SelectedNode.Index];
        }

    }

任何人有什么我可以做一个想法?我已经拖网谷歌目前保持24小时,没有运气。 谢谢!

Anyone have an idea of what I can do? I have trawled google now for 24hours with no luck. Thanks!

推荐答案

树节点有一个标签属性你可以用它来连接任何类型的数据,你会喜欢的。

The TreeNodes have a Tag property that you can use to attach any sort of data that you would like.

您会使用这样的:

// To set the data:
myTreeNode.Tag = new string[] { "1", "2", "3" };

// To read the data:
var data = myTreeNod.Tag as string[];