D
D
Denis2022-04-05 19:59:54
C++ / C#
Denis, 2022-04-05 19:59:54

How to make treeview nodes show up?

In short, the essence is this, there is a TreeView in it a lot of directories .. there is a button to add the main node, and the child. When I add new nodes, everything is displayed on the TreeView as it should. There is also a button to add an item to the database. When you click on it, it is displayed, enter a name, description ... and at the very end there is a ComboBox, when you click on it, I pop up all the directories from the TreeView. The problem is that if I add nodes in that ComboBox in VisualStudio itself, everything will be displayed, but when I add nodes using the button, they are not displayed. And if, for example, half is added in the studio, half with the help of buttons, then only those added in the studio will be displayed.

Code for the add node button:

private void button1_Click(object sender, EventArgs e)  // Основная форма
        {
            NewTreeViewNode newForm = new NewTreeViewNode();
            newForm.ShowDialog();

            string name = newForm.textBox1.Text.ToString();
            if(name != "") treeView1.Nodes.Add(newForm.textBox1.Text.ToString());
        }

        private void button1_Click(object sender, EventArgs e) // Форма для ввода имени узла
        {
            if (textBox1.Text == "") MessageBox.Show("Введите текс", "Ошибка", MessageBoxButtons.OK ,MessageBoxIcon.Error);
            else
            {
                flag = true;
                this.Close();
            }
        }


Code to display in ComboBox:
void Combo()
        {
            Form1 f = new Form1();

            List<TreeNode> nameNode = GetAllNodes(f.treeView1.Nodes);

            foreach (var item in nameNode)
            {
                string text = item.ToString();
                comboBox2.Items.Add(text.Substring(10).ToString());
            }
        }
        static List<TreeNode> GetAllNodes(TreeNodeCollection nodes)
        {
            List<TreeNode> list = new List<TreeNode>();
            foreach (TreeNode node in nodes)
            {
                list.Add(node);
                GetNodes(node.Nodes, list);
            }
            return list;
        }
        static void GetNodes(TreeNodeCollection nodes, List<TreeNode> list)
        {
            if (nodes == null)
                return;
            foreach (TreeNode node in nodes)
            {
                list.Add(node);
                GetNodes(node.Nodes, list);
            }
        }


Hope for help. Thank you.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question