简体   繁体   English

选择listBox中的文本时,在树形视图中显示文本C#

[英]Display text in treeview when text in listBox is selected C#

I am working on a window form application which has a list box and a treeview. 我正在使用具有列表框和树视图的窗口表单应用程序。 In my listBox, I have 3 items and they are student, teacher, and staff. 在我的列表框中,我有3个项目,分别是学生,老师和工作人员。 I don't have anything in my treeview yet. 我的树状视图中还没有任何内容。 In my treeview, I want the text changed according to what I select in my listBox. 在树状视图中,我希望文本根据我在列表框中选择的内容进行更改。 Are there way to do this? 有办法吗? I tried to put treeView1.Nodes.Add("Class") in my list box method which see if it is equals to student. 我试图将treeView1.Nodes.Add(“ Class”)放入列表框方法中,以查看它是否等于学生。 But all it does is to add the node class to my treeview everytime I click student. 但是它所做的就是每次我单击学生时将节点类添加到我的树形视图中。 I am not sure how to fix this. 我不确定如何解决此问题。 Help will be appreciated, thanks. 帮助将不胜感激,谢谢。

list box method 列表框方法

 private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

            foreach (var listItem in roleListBox.SelectedItems)
            {
                if (String.Equals(listItem.ToString(), "teacher", StringComparison.CurrentCultureIgnoreCase)) 
                {
                    //Display Class
                   treeView1.Nodes.Add("Class");
                }

                if(String.Equals(listItem.ToString(), "student", StringComparison.CurrentCultureIgnoreCase))
                {
                  //Display Salary
                  treeView1.Nodes.Add("Salary");
                }

                if(String.Equals(listItem.ToString(), "staff", StringComparison.CurrentCultureIgnoreCase))
                {
                    //Display Department
                }
            }
        }

treeview method 树视图方法

private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {



        }

Before adding node, Check whether it is already added and set the key of the node. 添加节点之前,请检查是否已添加节点并设置节点的密钥。

if (String.Equals(listItem.ToString(), "teacher", StringComparison.CurrentCultureIgnoreCase)) 
{ 
    if(treeView1.Nodes.ContainsKey("Class")==false)
    {
       //Display Class
       treeView1.Nodes.Add("Class","Class");
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM