Select TreeView Node - c#

I've a TreeNode and I need to allow user to select only one child node for parent.
Example:
-Car
---Ferrari
---Lamborghini
---Porsche
-Shoes
---Nike
---Puma
---Adidas
I can select "Ferrari" and "Nike", but not other child in "Car" or "Shoes". How can I make it?
After I do this, I need to concat text of Parent and child like this: Car: Ferrari.
Can you help me?
Regards.

You could handle the BeforeCheck event and clear the siblings checkboxes, e.g. :
private bool skipEvents = false; // this flag to avoid infinite recursion
void treeView1_BeforeCheck(object sender, TreeViewCancelEventArgs e)
{
// if is a root (car or shoes), or it's a recursive call, just return
if (skipEvents || e.Node.Parent == null)
return;
skipEvents = true;
foreach (TreeNode sibling in e.Node.Parent.Nodes)
{
// set the other siblings to unchecked
if (sibling != e.Node)
sibling.Checked = false;
}
skipEvents = false;
}
Here's an example to concatenate the parents and childs selected:
public string GetSelectionString()
{
string categorySep = Environment.NewLine;
string parentChildSep = " : ";
StringBuilder sb = new StringBuilder();
foreach (TreeNode root in this.treeView1.Nodes)
{
foreach (TreeNode node in root.Nodes)
{
if (node.Checked)
{
if (sb.Length > 0)
sb.Append(categorySep);
sb.Append(root.Text);
sb.Append(parentChildSep);
sb.Append(node.Text);
break;
}
}
}
return sb.ToString();
}
for example if Ferrari and Puma are selected, it returns a string like this:
Car : Ferrari
Shoes : Puma
EDIT as per comment:
This code does what you ask in your comment (selection/deselection of parents children):
private bool skipEvents = false; // this flag to avoid infinite recursion
void treeView1_BeforeCheck(object sender, TreeViewCancelEventArgs e)
{
// if it's a recursive call, just return
if (skipEvents)
return;
skipEvents = true;
// it's a root (e.g. car or shoes)
if (e.Node.Parent == null)
{
// if root node is going to be checked, just cancel the action (i.e. block parent selection)
if (!e.Node.Checked)
{
e.Cancel = true;
}
}
else
{
foreach (TreeNode sibling in e.Node.Parent.Nodes)
{
// set the other siblings to unchecked
if (sibling != e.Node)
sibling.Checked = false;
}
}
skipEvents = false;
}
void treeView1_AfterCheck(object sender, TreeViewEventArgs e)
{
// if it's a recursive call, just return
if (skipEvents)
return;
this.skipEvents = true;
// it's a root (e.g. car or shoes)
if (e.Node.Parent == null)
{
// root node has been unchecked, so uncheck the children
if (!e.Node.Checked)
{
foreach (TreeNode child in e.Node.Nodes)
child.Checked = false;
}
}
else
{
// if a child node has been checked --> check the parent
// otherwise, uncheck the parent
e.Node.Parent.Checked = e.Node.Checked;
}
this.skipEvents = false;
}
N.B.
TreeView class has a known bug that arises in Vista/Windows7 concerning checkboxes.
Basically if you double-click a checkbox it doesn't lauch any event, so this management will be compromised.
To solve this issue, you can disable double-click by using the class explained in this post instead of TreeView.

If you need one selection per tree, I would suggest using two TreeViews. I would also question whether or not you need to be using TreeViews or whether two ListBoxes or ComboBoxes might be more appropriate.
If you don't know how many 'trees' you'll have, but you do know how deep they are, you could use two or more ListBoxes (or ListViews) to display basically a list of lists:
Categories:
Shoes: Nike
Cars: Ferrari
Fruits: Apple (selected)
Selected Category (Fruits):
Apple (selected)
Orange
Pear
Kiwi

Related

Getting all visible Nodes in a TreeView

I want to get the node's keys just 'only in view' on treeview.
Here is the example;
I am using below code to get all node recursively. It just return all nodes key as expected. however i need to get the keys that only in treeview's view;
public void PrintNodesRecursive(UltraTreeNode oParentNode)
{
if (oParentNode.Nodes.Count == 0)
{
return;
}
foreach (UltraTreeNode oSubNode in oParentNode.Nodes)
{
MessageBox.Show(oSubNode.Key.ToString());
PrintNodesRecursive(oSubNode);
}
}
private void ultraButton3_Click(object sender, EventArgs e)
{
PrintNodesRecursive(ultraTree1.Nodes[0]);
}
I don't know i should follow different path or just reorganize code.
I just stacked after many hours. Need your help.
You can find the first visible node using Nodes collection and IsVisible property of the Node. Then create a recursive method which uses NextVisibleNode to find the next visible node in TreeView.
private void button1_Click(object sender, EventArgs e)
{
var visibleNodes = GetVisibleNodes(treeView1).ToList();
}
public IEnumerable<TreeNode> GetVisibleNodes(TreeView t)
{
var node = t.Nodes.Cast<TreeNode>().Where(x => x.IsVisible).FirstOrDefault();
while (node != null)
{
var temp = node;
node = node.NextVisibleNode;
yield return temp;
}
}
Also as another option, you can rely on Descendants extension method to flatten the TreeView and then using IsVisible property, get all visible nodes.

Why when searching for an item in Tree View there is never a match?

In the textBox2 i type a word/string for example Hello and i checked Hello does exist but it never color it in Yellow. I used a breakpoint and it never pass this line and color it in Yellow: if (tn.Text == this.textBox2.Text)
private void FindByText()
{
TreeNodeCollection nodes = treeView1.Nodes;
foreach (TreeNode n in nodes)
{
FindRecursive(n);
}
}
private void FindRecursive(TreeNode treeNode)
{
foreach (TreeNode tn in treeNode.Nodes)
{
// if the text properties match, color the item
if (tn.Text == this.textBox2.Text)
tn.BackColor = Color.Yellow;
FindRecursive(tn);
}
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
FindByText();
}
EDIT
This is an example image of what i want to do: Free search text: I type in textBox2 for example na or hello and it will highlight anywhere the places hello for example. in nodes names in nodes childs inside nodes anyhwere just like it is in the image.
Ofcourse if i type only a or b or c don't highlight it but do highlight any string i type in the textBox2 that is longer then 2 letters
The image is only for example to show what i need to it to do:
You never check the top level, only the their children
(note: the way you do the comparison may need fixing as others have said, but that is a different issue.)
private void FindByText()
{
TreeNodeCollection nodes = treeView1.Nodes;
foreach (TreeNode n in nodes)
{
if (n.Text == this.textBox2.Text)
n.BackColor = Color.Yellow;
FindRecursive(n);
}
}
Use following code:
private void FindRecursive(TreeNode treeNode)
{
// Use this condition if you want a case sensitive search, else use treeNode.Text.ToLower() == this.textBox2.Text.ToLower()
if (treeNode.Text == this.textBox2.Text)
treeNode.BackColor = Color.Yellow;
else
treeNode.BackColor = Color.White; // Here use your default background color
foreach (TreeNode tn in treeNode.Nodes)
{
FindRecursive(tn);
}
}

Filter a Treeview with a Textbox in a C# winforms app

I have a TreeView in my a C# winform. I would like to be able to add a search functionality through a search box.
Basically as the user types in letters (I'm guessing on the _TextChanged event), I show only the nodes that contain childnodes with the inputed letters...
My TreeView contains 53 parent nodes for a total of over 15000 Nodes so I need something a bit performant. I build my TreeView from a csv that I load into a DataTable and then make queries on to get the Parent nodes with associated child nodes...
UPDATE
I have an idea.
The final aim is that when a user doubleclicks on a child node it gets added to a listView.
I had first implemented this search function in a simple list view where I didn't separate my data into categories.
My idea is that once the user starts typing in things, I turn off my Tree view and show the list view instead...
I'll try and implement and see what it gives performance wise... Any critics on this idea are welcome.
Finally this is what I did, it suits my requirements.
I first make a copy of my TreeView and store into fieldsTreeCache. I then clear the fieldsTree. I then search through the cache and add any node containing my search parameter to the fieldsTree. Note here that once you search, you no longer have the parent nodes that show. You just get all of the end nodes. I did this because if not I had 2 choices:
Expand all the parent nodes containing childs that match but then it was slow and one parent might have 50 children which isn't great visually.
Not expand the parent nodes but then you just get the categories and not the children nodes that you're searching for.
void fieldFilterTxtBx_TextChanged(object sender, EventArgs e)
{
//blocks repainting tree till all objects loaded
this.fieldsTree.BeginUpdate();
this.fieldsTree.Nodes.Clear();
if (this.fieldFilterTxtBx.Text != string.Empty)
{
foreach (TreeNode _parentNode in _fieldsTreeCache.Nodes)
{
foreach (TreeNode _childNode in _parentNode.Nodes)
{
if (_childNode.Text.StartsWith(this.fieldFilterTxtBx.Text))
{
this.fieldsTree.Nodes.Add((TreeNode)_childNode.Clone());
}
}
}
}
else
{
foreach (TreeNode _node in this._fieldsTreeCache.Nodes)
{
fieldsTree.Nodes.Add((TreeNode)_node.Clone());
}
}
//enables redrawing tree after all objects have been added
this.fieldsTree.EndUpdate();
}
Here's a small simple example (with code from msdn) is that a very simple way to filter out the TreeView node displays.
winforms in a tree view you can only add or remove TreeNode.
The search for the nodes can still be improved if the nodes are stored with their data into a dictionary (with a unique key).
using System.Collections;
using System.Windows.Forms;
namespace FilterWinFormsTreeview
{
// The basic Customer class.
public class Customer : System.Object
{
private string custName = "";
protected ArrayList custOrders = new ArrayList();
public Customer(string customername) {
this.custName = customername;
}
public string CustomerName {
get { return this.custName; }
set { this.custName = value; }
}
public ArrayList CustomerOrders {
get { return this.custOrders; }
}
}
// End Customer class
// The basic customer Order class.
public class Order : System.Object
{
private string ordID = "";
public Order(string orderid) {
this.ordID = orderid;
}
public string OrderID {
get { return this.ordID; }
set { this.ordID = value; }
}
}
// End Order class
public static class TreeViewHelper
{
// Create a new ArrayList to hold the Customer objects.
private static ArrayList customerArray = new ArrayList();
public static void FilterTreeView(TreeView treeView1, string orderText) {
if (string.IsNullOrEmpty(orderText)) {
FillMyTreeView(treeView1);
} else {
// Display a wait cursor while the TreeNodes are being created.
Cursor.Current = Cursors.WaitCursor;
// Suppress repainting the TreeView until all the objects have been created.
treeView1.BeginUpdate();
foreach (TreeNode customerNode in treeView1.Nodes) {
var customer = customerNode.Tag as Customer;
if (customer != null) {
customerNode.Nodes.Clear();
// Add a child treenode for each Order object in the current Customer object.
foreach (Order order in customer.CustomerOrders) {
if (order.OrderID.Contains(orderText)) {
var orderNode = new TreeNode(customer.CustomerName + "." + order.OrderID);
customerNode.Nodes.Add(orderNode);
}
}
}
}
// Reset the cursor to the default for all controls.
Cursor.Current = Cursors.Default;
// Begin repainting the TreeView.
treeView1.EndUpdate();
}
}
public static void FillMyTreeView(TreeView treeView1) {
// Add customers to the ArrayList of Customer objects.
if (customerArray.Count <= 0) {
for (int x = 0; x < 1000; x++) {
customerArray.Add(new Customer("Customer" + x.ToString()));
}
// Add orders to each Customer object in the ArrayList.
foreach (Customer customer1 in customerArray) {
for (int y = 0; y < 15; y++) {
customer1.CustomerOrders.Add(new Order("Order" + y.ToString()));
}
}
}
// Display a wait cursor while the TreeNodes are being created.
Cursor.Current = Cursors.WaitCursor;
// Suppress repainting the TreeView until all the objects have been created.
treeView1.BeginUpdate();
// Clear the TreeView each time the method is called.
treeView1.Nodes.Clear();
// Add a root TreeNode for each Customer object in the ArrayList.
foreach (Customer customer2 in customerArray) {
var customerNode = new TreeNode(customer2.CustomerName);
customerNode.Tag = customer2;
treeView1.Nodes.Add(customerNode);
// Add a child treenode for each Order object in the current Customer object.
foreach (Order order1 in customer2.CustomerOrders) {
var orderNode = new TreeNode(customer2.CustomerName + "." + order1.OrderID);
customerNode.Nodes.Add(orderNode);
}
}
// Reset the cursor to the default for all controls.
Cursor.Current = Cursors.Default;
// Begin repainting the TreeView.
treeView1.EndUpdate();
}
}
}
Every node in TreeView has Expanded and IsVisible properties. The number of items which are visible at the same time is limited (TreeView.VisibleCount). Based on this information you can reduce the number of nodes to probe dramatically.
When scanning the node and it's child nodes you can abort recursion as you find the first match inside collapsed node, thus you already know it has at least one child and will be visible anyway.
Perform filtering asynchronously. (use new Task() for instance) Start the first task after minimal number of chars was typed (let's say 3). Every next typed char must cancel the running task and start the new one.

Move Node in Tree up or Down

What is the most accurate way to move a node up and down in a treeview. I got a context menu on each node and the selected node should be moved with all its subnodes.
I'm using C# .Net 3.5 WinForms
You can use the following extensions :
public static class Extensions
{
public static void MoveUp(this TreeNode node)
{
TreeNode parent = node.Parent;
TreeView view = node.TreeView;
if (parent != null)
{
int index = parent.Nodes.IndexOf(node);
if (index > 0)
{
parent.Nodes.RemoveAt(index);
parent.Nodes.Insert(index - 1, node);
}
}
else if (node.TreeView.Nodes.Contains(node)) //root node
{
int index = view.Nodes.IndexOf(node);
if (index > 0)
{
view.Nodes.RemoveAt(index);
view.Nodes.Insert(index - 1, node);
}
}
}
public static void MoveDown(this TreeNode node)
{
TreeNode parent = node.Parent;
TreeView view = node.TreeView;
if (parent != null)
{
int index = parent.Nodes.IndexOf(node);
if (index < parent.Nodes.Count -1)
{
parent.Nodes.RemoveAt(index);
parent.Nodes.Insert(index + 1, node);
}
}
else if (view != null && view.Nodes.Contains(node)) //root node
{
int index = view.Nodes.IndexOf(node);
if (index < view.Nodes.Count - 1)
{
view.Nodes.RemoveAt(index);
view.Nodes.Insert(index + 1, node);
}
}
}
}
Child nodes will follow their parents.
EDIT: Added case that node to move is a root in the TreeView.
While I feel writing this code is a waste of time, given the lack of response to comments by the OP, the least I can do is show how the code example by Le-Savard can be fixed so that muliple clicks of the up or down choice on the context menu ... assuming the context menu is not auto-closed each time and the user is forced to select the same node over and over again ... will do the right thing with the orignally selected node, and not create un-intended side effects :
public static class Extensions
{
public static void MoveUp(this TreeNode node)
{
TreeNode parent = node.Parent;
if (parent != null)
{
int index = parent.Nodes.IndexOf(node);
if (index > 0)
{
parent.Nodes.RemoveAt(index);
parent.Nodes.Insert(index - 1, node);
// bw : add this line to restore the originally selected node as selected
node.TreeView.SelectedNode = node;
}
}
}
public static void MoveDown(this TreeNode node)
{
TreeNode parent = node.Parent;
if (parent != null)
{
int index = parent.Nodes.IndexOf(node);
if (index < parent.Nodes.Count - 1)
{
parent.Nodes.RemoveAt(index);
parent.Nodes.Insert(index + 1, node);
// bw : add this line to restore the originally selected node as selected
node.TreeView.SelectedNode = node;
}
}
}
}
Of course this fix, still does not address the fact that in the example code that multiple root nodes cannot be moved (since they are 'parentless) : that's easiliy fixable.
Nor does it address the more interesting case where moving up a top child-node means you make some interpretation of where that "promoted" child code should go : exactly the same "strategic choice" is involved where you "move down" the last child node of a parent node and are thus required to decide where it should go. In Dynami Le-Savard's code : these cases are just ignored.
However, it is a design-choice to restrict child node from only being moved within their parent nodes Nodes collection : a design choice that may be perfectly suitable for one solution.
Similarly, it is a design choice to force a user to select a node and context-click to get a context menu that allows a choice of moving up or down every single time they want to move it : that's not a design choice I'd make : I'd be using drag-and-drop here or buttons that allow repeated rapid-fire relocation of any selected node anywhere in the tree.
By the way I like Dynami Le-Savard's use of extensions here.
Here's a solution that allows you to drag & drop nodes to wherever you want. To move a node to the same level as another node, just hold down shift when dropping the node. This is a really easy way to go compared to the alternatives and their potential problems. Example was written with a more recent version of .Net (4.5).
Note: Be sure and AllowDrop=true on the treeview control otherwise you can't drop nodes.
/// <summary>
/// Handle user dragging nodes in treeview
/// </summary>
private void treeView1_ItemDrag(object sender, ItemDragEventArgs e)
{
DoDragDrop(e.Item, DragDropEffects.Move);
}
/// <summary>
/// Handle user dragging node into another node
/// </summary>
private void treeView1_DragEnter(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Move;
}
/// <summary>
/// Handle user dropping a dragged node onto another node
/// </summary>
private void treeView1_DragDrop(object sender, DragEventArgs e)
{
// Retrieve the client coordinates of the drop location.
Point targetPoint = treeView1.PointToClient(new Point(e.X, e.Y));
// Retrieve the node that was dragged.
TreeNode draggedNode = e.Data.GetData(typeof(TreeNode));
// Sanity check
if (draggedNode == null)
{
return;
}
// Retrieve the node at the drop location.
TreeNode targetNode = treeView1.GetNodeAt(targetPoint);
// Did the user drop the node
if (targetNode == null)
{
draggedNode.Remove();
treeView1.Nodes.Add(draggedNode);
draggedNode.Expand();
}
else
{
TreeNode parentNode = targetNode;
// Confirm that the node at the drop location is not
// the dragged node and that target node isn't null
// (for example if you drag outside the control)
if (!draggedNode.Equals(targetNode) && targetNode != null)
{
bool canDrop = true;
while (canDrop && (parentNode != null))
{
canDrop = !Object.ReferenceEquals(draggedNode, parentNode);
parentNode = parentNode.Parent;
}
if (canDrop)
{
// Have to remove nodes before you can move them.
draggedNode.Remove();
// Is the user holding down shift?
if (e.KeyState == 4)
{
// Is the targets parent node null?
if (targetNode.Parent == null)
{
// The target node has no parent. That means
// the target node is at the root level. We'll
// insert the node at the root level below the
// target node.
treeView1.Nodes.Insert(targetNode.Index + 1, draggedNode);
}
else
{
// The target node has a valid parent so we'll
// drop the node into it's index.
targetNode.Parent.Nodes.Insert(targetNode.Index + 1, draggedNode);
}
}
else
{
targetNode.Nodes.Add(draggedNode);
}
targetNode.Expand();
}
}
}
// Optional: The following lines are an example of how you might
// provide a better experience by highlighting and displaying the
// content of the dropped node.
// treeView1.SelectedNode = draggedNode;
// NavigateToNodeContent(draggedNode.Tag);
}

How do I loop through all layers of Treeview nodes?

Read EDIT 2 first
I am trying to set up some way to visually distinguish the nodes in a winform app. For example, alternating colors.
Can some one start me down that path? Also, has anyone else had to do that before and how did you do it?
Thanks
EDIT
I had seen the backcolor setting as well(Thank You) but I am having trouble getting it to work. I do not see a Paint() event for treeviews. I tried putting the below code in my Form Load() but it isn't working. Possibly because the treeview is loaded yet??
private void frmCaseNotes_Load(object sender, System.EventArgs e)
{
foreach (TreeNode treeNode in treeView1.Nodes[0].Nodes)
{
treeNode.BackColor = Color.DeepSkyBlue;
}
}
EDIT 2
Okay, I have the initial problem out of the way using the below on Form_Load()
foreach (TreeNode treeNode in treeView1.Nodes)
{
if (treeNode.Index % 2 == 0)
{
treeNode.ForeColor = Color.DodgerBlue;
}
else
{
treeNode.ForeColor = Color.Goldenrod;
}
Now what I need to figure out, with someone's help, is how to Loop through ALL layers of nodes and apply my alternating coloring. If I do something along the below lines I can achieve this.
foreach (TreeNode treeNode in treeView1.Nodes[1].Nodes[0].Nodes)
{
if (treeNode.Index % 2 == 0)
{
treeNode.ForeColor = Color.DodgerBlue;
}
else
{
treeNode.ForeColor = Color.Goldenrod;
}
How do I iterate through ALL layers programatically?
Recursion?
Edit: added code for eliminating color repetition
protected void ColorNodes(TreeNode root, Color firstColor, Color secondColor)
{
Color nextColor;
foreach (TreeNode childNode in root.Nodes)
{
nextColor = childNode.ForeColor = childNode.Index % 2 == 0 ? firstColor : secondColor;
if (childNode.Nodes.Count > 0)
{
// alternate colors for the next node
if (nextColor == firstColor)
ColorNodes(childNode, secondColor, firstColor);
else
ColorNodes(childNode, firstColor, secondColor);
}
}
}
private void frmCaseNotes_Load(object sender, System.EventArgs e)
{
foreach (TreeNode rootNode in treeView1.Nodes)
{
ColorNodes(rootNode, Color.Goldenrod, Color.DodgerBlue);
}
}
Dude, this one was serious trouble. The Broadth Search was easy, but you were right. Depth Search was a pain. I hope it helps you. Shame that such nice trick will probably fall into oblivion due to the sheer ammount of questions on this site. The depth algorithm is kind of crude and assume a head node without siblings.
//depth search on TreeView
TreeNode node = trv.Nodes[0];
Stack<TreeNode> list = new Stack<TreeNode>();
list.Push(node);
while (list.Count > 0)
{
while (node.Nodes.Count > 0)
{
list.Push(node.Nodes[0]);
node = node.Nodes[0];
}
//Will always have a leaf here as the current node. The leaf is not pushed.
//If it has a sibling, I will try to go deeper on it.
if (node.NextNode != null)
{
node = node.NextNode;
continue;
}
//If it does NOT have a sibling, I will pop as many parents I need until someone
//has a sibling, and go on from there.
while (list.Count > 0 && node.NextNode == null)
{
node = list.Pop();
}
if (node.NextNode != null) node = node.NextNode;
}
//broadth search on TreeView
Queue<TreeNode> list = new Queue<TreeNode>();
foreach(TreeNode node in trv.Nodes)
{
list.Enqueue(node);
}
foreach(TreeNode node in list)
{
foreach(TreeNode childNode in node.Nodes)
{
list.Enqueue(childNode);
}
}
The best way to do this would be to override the OnPaint event, and provide your own code for drawing in the control.
You can find many examples on overriding the onPaint method online.
EDIT: I actually looked into this some more, and you can set the BackColor of treeview nodes individually already.
Me.TreeView1.Nodes(0).BackColor = Color.AliceBlue
Okay, this is how far I got. Unfortunately it is very ugly and I am still manually coding how DEEP I go into it. Also it doesn't prevent like colors being next to each other. Suggestions?
foreach (TreeNode treeNode in treeView1.Nodes)
{
treeNode.ForeColor = treeNode.Index % 2 == 0 ? Color.DodgerBlue : Color.Goldenrod;
foreach (TreeNode childNode in treeNode.Nodes)
{
childNode.ForeColor = childNode.Index % 2 == 0 ? Color.Goldenrod : Color.DodgerBlue;
foreach (TreeNode childChildNode in childNode.Nodes)
{
childChildNode.ForeColor = childChildNode.Index % 2 == 0 ? Color.DodgerBlue : Color.Goldenrod;
foreach (TreeNode childChildChildNode in childChildNode.Nodes)
{
childChildChildNode.ForeColor = childChildChildNode.Index % 2 == 0 ? Color.Goldenrod : Color.DodgerBlue;
}
}
}
}
Do it recursively on a Control type using the children control from the TreeView root and check to see if the Control type is the type of the node you are looking for, cast, change backcolor and you are done.

Categories