0

I have a treeview like below,

tables
--table1
  --tab1
  --tab2
--table2
  --tab2
  --tab2

I am giving a text "tab" and using treeViewMenu.SelectedNode = txtbox1.Text, it selects the very first match (i.e node: table1)

I want all the nodes having "tab" in its name to be selected. (i.e table1 and table2)

I have tried passing an array to treeViewMenu.SelectedNode =array[], but this is not working.

Scrobi
  • 1,215
  • 10
  • 13
Pyd
  • 6,017
  • 18
  • 52
  • 109
  • What did you want to do after they are selected? As the possible duplicate shows the tree view was not designed for multi-select. – Scrobi Jul 07 '17 at 09:44
  • Also here: https://stackoverflow.com/questions/25499249/how-do-i-select-multiple-nodes-at-a-time-from-treeview-control-using-c-sharp-and – MalvEarp Jul 07 '17 at 10:28

1 Answers1

0

A TreeView is not designed for MultiSelect. But if you only want to see which Nodes match to the string in the TextBox you can change the BackColor of this Nodes.

private void txtbox1_TextChanged(object sender, EventArgs e)
{
    foreach (TreeNode tn in this.treeViewMenu.Nodes)
    {
        SetColor(tn);
    }
}

private void SetColor(TreeNode Node)
{
    if (Node.Text.Contains(this.txtbox1.Text))
    {
        Node.BackColor = System.Drawing.Color.Blue;
        Node.ForeColor = System.Drawing.Color.White;
        Node.Tag = true; //for later to find out which Nodes are "selected"
    }
    else
    {
        Node.BackColor = System.Drawing.Color.FromKnownColor(System.Drawing.KnownColor.Window);
        Node.ForeColor = System.Drawing.Color.FromKnownColor(System.Drawing.KnownColor.WindowText);
        Node.Tag = false;
    }

    foreach (TreeNode subNode in Node.Nodes)
    {
        SetColor(subNode);
    }
}

To find out which Nodes are "selected" you can use this:

private void btn_GetSelected_Click(object sender, EventArgs e)
{
    string selectedNodes = "";

    foreach (TreeNode tn in this.treeViewMenu.Nodes)
    {
        GetSelectedNodesByTag(tn, ref selectedNodes);
    }
    MessageBox.Show(selectedNodes, "Selected Nodes");
}

private void GetSelectedNodesByTag(TreeNode Node, ref string SelectedNodes)
{
    if ((bool)Node.Tag == true)
    {
        if (SelectedNodes != "") SelectedNodes += "\n";
        SelectedNodes += Node.FullPath;
    }

    foreach (TreeNode subNode in Node.Nodes)
    {
        GetSelectedNodesByTag(subNode, ref SelectedNodes);
    }
}

You also could use the CheckBoxes-Property to show the selected Nodes:

private void SetChecked(TreeNode Node)
{
    if (Node.Text.Contains(this.txtbox1.Text))
    {
        Node.Checked = true;
    }
    else
    {
        Node.Checked = false;
    }

    foreach (TreeNode subNode in Node.Nodes)
    {
        SetChecked(subNode);
    }
}

And find out which Nodes are checked:

private void GetCheckedNodes(TreeNode Node, ref string SelectedNodes)
{
    if (Node.Checked)
    {
        if (SelectedNodes != "") SelectedNodes += "\n";
        SelectedNodes += Node.FullPath;
    }

    foreach (TreeNode subNode in Node.Nodes)
    {
        GetCheckedNodes(subNode, ref SelectedNodes);
    }
}
Wudge
  • 357
  • 1
  • 6
  • 14