I'm having a windows form with a tree view control. This tree view has a Root node and 2 child nodes. My requirement is i need to hide the first child node.
Is it possible to make visible false that particular child nod
Yes you could inherit from tree node and create your own behaviour. Like so.
public class RootNode : TreeNode
{
public List<ChildNode> ChildNodes { get; set; }
public RootNode()
{
ChildNodes = new List<ChildNode>();
}
public void PopulateChildren()
{
this.Nodes.Clear();
var visibleNodes =
ChildNodes
.Where(x => x.Visible)
.ToArray();
this.Nodes.AddRange(visibleNodes);
}
//you would use this instead of (Nodes.Add)
public void AddNode(ChildNode node)
{
if (!ChildNodes.Contains(node))
{
node.ParentNode = this;
ChildNodes.Add(node);
PopulateChildren();
}
}
//you would use this instead of (Nodes.Remove)
public void RemoveNode(ChildNode node)
{
if (ChildNodes.Contains(node))
{
node.ParentNode = null;
ChildNodes.Remove(node);
PopulateChildren();
}
}
}
public class ChildNode : TreeNode
{
public RootNode ParentNode { get; set; }
private bool visible;
public bool Visible { get { return visible; } set { visible = value;OnVisibleChanged(): } }
private void OnVisibleChanged()
{
if (ParentNode != null)
{
ParentNode.PopulateChildren();
}
}
}
No, there is no way to make node invisible. You should remove it instead of making invisible. And later you will have to add it back into its original position.
If you are loading a treeview with a sitemap file, then another approach is to do something like this. Here the user's credentials have been read from a DB and written to a cookie.
private void ManageTreeMenu()
{
var value = Utilities.Cookies.GetCookieValue("IsAdmin");
bool.TryParse(value, out var isAdmin);
var dir = Server.MapPath("~");
File.Delete(dir + "Web.sitemap");
if (isAdmin)
File.Copy(dir + "WebAdmin.sitemap", dir + "/Web.sitemap");
else
File.Copy(dir + "WebOper.sitemap", dir + "/Web.sitemap");
}
You'd have to do this again if the user's role was changed in the program. I have only verified this in Visual Studio, not in a deployed web application. Caveat emptor.
Related
I'm having a windows form with a tree view control. This tree view has a Root node and 2 child nodes. My requirement is i need to hide the first child node.
Is it possible to make visible false that particular child nod
Yes you could inherit from tree node and create your own behaviour. Like so.
public class RootNode : TreeNode
{
public List<ChildNode> ChildNodes { get; set; }
public RootNode()
{
ChildNodes = new List<ChildNode>();
}
public void PopulateChildren()
{
this.Nodes.Clear();
var visibleNodes =
ChildNodes
.Where(x => x.Visible)
.ToArray();
this.Nodes.AddRange(visibleNodes);
}
//you would use this instead of (Nodes.Add)
public void AddNode(ChildNode node)
{
if (!ChildNodes.Contains(node))
{
node.ParentNode = this;
ChildNodes.Add(node);
PopulateChildren();
}
}
//you would use this instead of (Nodes.Remove)
public void RemoveNode(ChildNode node)
{
if (ChildNodes.Contains(node))
{
node.ParentNode = null;
ChildNodes.Remove(node);
PopulateChildren();
}
}
}
public class ChildNode : TreeNode
{
public RootNode ParentNode { get; set; }
private bool visible;
public bool Visible { get { return visible; } set { visible = value;OnVisibleChanged(): } }
private void OnVisibleChanged()
{
if (ParentNode != null)
{
ParentNode.PopulateChildren();
}
}
}
No, there is no way to make node invisible. You should remove it instead of making invisible. And later you will have to add it back into its original position.
If you are loading a treeview with a sitemap file, then another approach is to do something like this. Here the user's credentials have been read from a DB and written to a cookie.
private void ManageTreeMenu()
{
var value = Utilities.Cookies.GetCookieValue("IsAdmin");
bool.TryParse(value, out var isAdmin);
var dir = Server.MapPath("~");
File.Delete(dir + "Web.sitemap");
if (isAdmin)
File.Copy(dir + "WebAdmin.sitemap", dir + "/Web.sitemap");
else
File.Copy(dir + "WebOper.sitemap", dir + "/Web.sitemap");
}
You'd have to do this again if the user's role was changed in the program. I have only verified this in Visual Studio, not in a deployed web application. Caveat emptor.
I have a TreeView control in which I need to add some nodes passing a string to the Add() method, and add some nodes passing a derived TreeNode class instance to the Add() method.
I'm trying to see which leaf nodes are checked and I just can't figure out how to iterate through the nodes that are derived TreeNode class (EmojiNode) instances shown below.
TreeNode groupnode = EmojisTreeView.Nodes.Add("Group Name");
EmojiNode emojiNode = new EmojiNode(subgroupEmojiName, unicodeEndStr, emojiConvertResultStr);
int groupsubemojinode = groupsubnode.Nodes.Add(emojiNode);
foreach (var node in EmojisTreeView.Nodes.OfType<EmojiNode>())
{
// count is 0
}
public class EmojiNode : TreeNode
{
public string emojiName;
public string emojiValue;
public string emojiConvertedResultStr;
public EmojiNode(string emojiName, string emojiValue, string emojiConvertedResultStr)
{
this.emojiName = emojiName;
this.emojiValue = emojiValue;
this.emojiConvertedResultStr = emojiConvertedResultStr;
this.Text = this.emojiConvertedResultStr + " " + this.emojiName;
}
public EmojiNode(string emojiName)
{
this.emojiName = emojiName;
this.Text = this.emojiName;
}
public string getEmojiName()
{
return this.emojiName;
}
public string getEmojiValue()
{
return this.emojiValue;
}
public string getEmojiConvertedResultStr()
{
return this.emojiConvertedResultStr;
}
}
Title may be misleading as I had some trouble searching and even creating a proper question, so let me give a real problem I'm struggling with:
I have a Graph class. Since graphs need nodes and edges I created two additional classes Node (vertex) and Edge. My structure looks like this:
class Graph
{
List<Node> nodes;
List<Edge> edges;
public Graph( ... ) { /* populate lists */ }
}
class Node { ... }
class Edge { ... }
I wrote some methods for Node class, one is particularly problematic for me. Signature:
public List<Node> GetNeighbours(List<Edge> edges) { ... }
Pretty standard. Given a graph I ask a node: how many neighbours do you have? I need list of edges to resolve it.
How can I refactor this code so that I can use Graph properties/fields inside instead of passing a list of edges every time? Is something like this possible:
public List<Node> GetNeighbours()
{
// ...
foreach(edge in *BASE*.edges) { ... }
}
I know that I can't use the base keyword because I don't want any inheritance here (why would a node have to inherit from graph?!) and nested classes seem not to help me as well (no access to "parent's" fields).
This code is working right now, but I feel it's not elegant and I'd like to experience a proper solution.
Pass a reference to the parent class in the Graph constructor.
Something like:
class Graph
{
private ParentType parent;
public void Graph(ref ParentType parent)
{
this.parent = parent;
}
}
Then, in the GetNeighbours method (assuming the ParentType has an Edges collection property):
public List<Node> GetNeighbours()
{
// ...
foreach(var edge in parent.Edges) { ... }
}
From this description of what you're trying to do:
Given a graph I ask a node: how many neighbours do you have?
Are you sure that this should be a method of a Node? Since Graph contains the Nodes and Edges perhaps this method is better off in Graph.
public List<Node> GetNeighbours(Node node)
{
if(!nodes.Contains(node)
{
return new List<Node>(); //No neighbors. Return an empty list.
}
// Find and return the neighbors. This method is in Graph so it
// has access to all of Graph's internals.
}
My reasoning is that since in a sense Graph is a parent and it contains Nodes, Node does not need to know about Graph. Its purpose (Single Responsibility) is complete without any references to Graph.
I would have a method like Graph.AddNodes() or Graph.AddEdges() on Graph so that this is a central place to make sure that all Nodes (and/or Edges) have the reference that it needs. I'm thinking something like this, depending on the model of Node and Edge for you.
class Graph
{
List<Node> nodes;
List<Edge> edges;
public Graph( ... ) { /* populate lists */ }
public void AddEdges(params Edge[] edges) {
foreach (var edge in edges) {
edge.Node1.Parent = this;
edge.Node2.Parent = this;
}
}
}
class Node {
public Graph Parent { get; set; }
public List<Node> GetNeighbours()
{
var neighbors = new List<Node>();
foreach(var edge in parent.Edges) {
if (edge.Node1 == this && !neighbors.Contains(edge.Node2)) {
neighbors.Add(edge.Node2);
}
else if (edge.Node2 == this && !neighbors.Contains(edge.Node1)) {
neighbors.Add(edge.Node1);
}
}
}
}
class Edge {
public Node Node1 { get; set; }
public Node Node2 { get; set; }
}
Here is an alternative approach. Instead of passing the parent reference, you could make each edge aware of the nodes on each end. And make each node aware of the edges connected to them.
A massive advantage of this is that you do not need enumerate possibly massive amounts of nodes/edges to find what you need. You already have what you need so it is much faster.
Here is quick sample of the approach I described along with some tests:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace GraphModelTest
{
class Program
{
static void Main(string[] args)
{
TestA();
TestB();
TestC();
}
private static void TestC()
{
//A <-> B
//| |
//D <-> C
Node a = new Node("a");
Node b = new Node("b");
Node c = new Node("c");
Node d = new Node("d");
Edge ab = a.ConnectTo(b);
Edge bc = b.ConnectTo(c);
Edge cd = c.ConnectTo(d);
Edge da = d.ConnectTo(a);
Graph g = new Graph();
g.Nodes.Add(a);
g.Nodes.Add(b);
g.Nodes.Add(c);
g.Nodes.Add(d);
g.Edges.Add(ab);
g.Edges.Add(bc);
g.Edges.Add(cd);
g.Edges.Add(da);
Console.WriteLine(g.ToString());
Console.WriteLine("Neighbours of B");
foreach (Node n in b.GetNeighbours())
{
Console.WriteLine(n.ToString());
}
Console.WriteLine("Neighbours of D");
foreach (Node n in d.GetNeighbours())
{
Console.WriteLine(n.ToString());
}
}
private static void TestB()
{
//A <-> B <-> C
Node a = new Node("a");
Node b = new Node("b");
Edge ab = a.ConnectTo(b);
Node c = new Node("c");
Edge bc = b.ConnectTo(c);
Graph g = new Graph();
g.Nodes.Add(a);
g.Nodes.Add(b);
g.Nodes.Add(c);
g.Edges.Add(ab);
g.Edges.Add(bc);
Console.WriteLine(g.ToString());
Console.WriteLine("Neighbours of B");
foreach (Node n in b.GetNeighbours())
{
Console.WriteLine(n.ToString());
}
}
private static void TestA()
{
//A <-> B
Node a = new Node("a");
Node b = new Node("b");
Edge ab = a.ConnectTo(b);
Graph g = new Graph();
g.Nodes.Add(a);
g.Nodes.Add(b);
g.Edges.Add(ab);
Console.WriteLine(g.ToString());
}
}
class Edge
{
public Edge(string name, Node a, Node b)
{
Name = name;
A = a;
B = b;
}
public Node A { get; private set; }
public Node B { get; private set; }
public string Name { get; private set; }
public override string ToString() => $"{Name}";
}
class Node
{
public Node(string name)
{
Name = name;
connectedEdges = new List<Edge>();
}
public string Name { get; private set; }
private ICollection<Edge> connectedEdges;
public IEnumerable<Edge> ConnectedEdges
{
get
{
return connectedEdges.AsEnumerable();
}
}
public void AddConnectedEdge(Edge e)
{
connectedEdges.Add(e);
}
public Edge ConnectTo(Node n)
{
//Create the edge with references to nodes
Edge e = new Edge($"{Name} <-> {n.Name}", this, n);
//Add edge reference to this node
AddConnectedEdge(e);
//Add edge reference to the other node
n.AddConnectedEdge(e);
return e;
}
public IEnumerable<Node> GetNeighbours()
{
foreach (Edge e in ConnectedEdges)
{
//Have to figure which one is not this node
Node node = e.A != this ? e.A : e.B;
yield return node;
}
}
public override string ToString() => $"{Name}";
}
class Graph
{
public Graph()
{
Nodes = new List<Node>();
Edges = new List<Edge>();
}
public ICollection<Node> Nodes { get; set; }
public ICollection<Edge> Edges { get; set; }
public override string ToString()
{
StringBuilder str = new StringBuilder();
str.AppendLine("Graph:");
str.AppendLine("Nodes:");
foreach (Node n in Nodes)
{
str.AppendLine(n.ToString());
}
str.AppendLine("Edges:");
foreach (Edge e in Edges)
{
str.AppendLine(e.ToString());
}
return str.ToString();
}
}
}
I have a node class that contains only value type properties, and one reference type: it's parent node. When performing tree searches, these nodes are created and destroyed hundreds of thousands of times in a very short time span.
public class Node
{
public Node Parent { get; set; }
public int A { get; set; }
public int B { get; set; }
public int C { get; set; }
public int D { get; set; }
}
The tree search looks something like this:
public static Node GetDepthFirstBest(this ITree tree, Node root)
{
Node bestNode = root;
float bestScore = tree.Evaluate(root);
var stack = new Stack<Node>();
stack.Push(root);
while(stack.Count > 0)
{
var current = stack.Pop();
float score = tree.Evaluate(current);
if (score > bestScore)
{
bestNode = current;
bestScore = score;
}
var children = tree.GetChildren(current);
foreach(var c in children) { stack.Push(c); }
}
return bestNode;
}
Because this is done in a Mono runtime that has a very old GC, I wanted to try and pool the node objects. However, I am at a loss on how to know when a node object is safe to return to the pool, since other nodes that are still in use might reference it as a parent. At the end of the search, the best node is returned and a list of nodes is formed by walking back through its ancestors. I have full control over how the nodes are created inside the tree, if that's useful.
What options could I try and implement?
So, fortunately, if you're doing a Depth-First-Search, which you appear to be, this is a bit easier. Any time you reach a leaf node, there are two possibilities: that leaf node is part of the current deepest tree, or it's not.
If it's not, that means it's safe to return this node to the pool. If it is, that means we can return any nodes in our old tree back to our pool that are not in our own ancestor chain.
Now, if we're not a leafnode, we don't know if we can be freed until after we've finished checking our children. then, once all our children are checked, we find out if any of our children said they were the current best. if so, we keep ourselves
this does mean we're doing quite a bit more checking.
Here's some sudo code:
List bestNodes;
bool evalNode(node, score)
{
if (childCount == 0)
{
if (score > bestScore)
{
bestScore = score;
bestNode = node;
bestNodes.Add(node);
return true;
}
else
{
freeNode(this);
return false;
}
}
else
{
bool inLongest = false;
foreach (child in children)
{
inLongest = evalNode(child, score + 1) || inLongest;
}
if (!inLongest)
{
freeNode(node);
}
else
{
free(bestNodes[score]);
bestNodes[score] = node;
}
return inLongest;
}
}
Try using the ref keyword if your node is a struct, this avoids copying the node every time you pass it through to a function.
Thus:
struct Node
{
object obj;
Node children;
}
public void DoStuffWithNode(ref Node pNode){...Logic...}
I'm converting a WPF client in Windows Forms and I got some problems trying to replicate the TreeView control structure.
In the first project I have a custom factory that builds a structure starting from an input string that is basically a XML.
The return type is a collection.
Custom TreeNode:
public class TreeViewNode
{
public TreeViewNode() { }
public DocumentKey DocKey { get; set; }
public string Text { get; set; }
public IList<TreeViewNode> Children { get; set; }
}
Factory:
public class TreeViewFactory
{
public IList<TreeViewNode> GetSctructure(DocumentKey docKey, string structure, bool loadAllParents)
{
XDocument xmlDocstructure = CommonXmlValueParser.GetXDocument(structure);
var parentsNodes = (from item in xmlDocstructure.Descendants("structure_item")
where (CommonXmlValueParser.GetAttribute(item, "level") == "1")
select new TreeViewNode
{
Text = GetNodeText(item),
DocKey = new DocumentKey()
{
Bank = docKey.Bank,
Ud = int.Parse(CommonXmlValueParser.GetElement(item.Element("ud"))),
Master = int.Parse(CommonXmlValueParser.GetElement(item.Element("master"))),
NVig = int.Parse(CommonXmlValueParser.GetElement(item.Element("nvig"))),
Subjects = docKey.Subjects
},
Children = GetChildrenNodes(item, 2, docKey.Bank)
}).ToList();
return parentsNodes;
}
private IList<TreeViewNode> GetChildrenNodes(XElement element, int level, int dataBank)
{
var childrenNodes = (from item in element.Descendants("structure_item")
where (CommonXmlValueParser.GetAttribute(item, "level") == level.ToString())
select new TreeViewNode
{
Text = GetNodeText(item),
DocKey = new DocumentKey()
{
Bank = dataBank,
Ud = int.Parse(CommonXmlValueParser.GetElement(item.Element("ud"))),
Master = int.Parse(CommonXmlValueParser.GetElement(item.Element("master"))),
NVig = int.Parse(CommonXmlValueParser.GetElement(item.Element("nvig"))),
},
Children = GetChildrenNodes(item, level + 1, dataBank)
}).ToList();
return childrenNodes;
}
}
Binding:
void CreateTree(object tree, EventArgs e)
{
//...
TreeViewFactory treeFactory = new TreeViewFactory();
var documentStructure = treeFactory.Structure(document.DocumentKey, document.XmlStructure, true);
this.tabMainControl.document.SetTreeViewStructureNodes(documentStructure);
}
public void SetTreeViewStructureNodes(IList<TreeViewNode> nodes)
{
this.treeView.ItemsSource = nodes;
}
Update:
I made the TreeViewNode derive from TreeNode and changed the method SetTreeViewStructureNodes in:
private TreeView SetTreeViewStructureNodes(IList<TreeViewNode> nodes)
{
TreeView treeView = new TreeView();
treeView.Nodes.AddRange(nodes.ToArray());
return treeView;
}
still that doesn't achieve my goal as it's still not rendered...
In Windows Forms as far as I know it's not possible to associate a sort of datasource that is a whatever type collection (implements IEnumerable).
Apart from using 3rd party components, how can I solve my problem. My experience on WinForms is pretty short and just when I learnt to manage much better WPF they decided to shift it :(
Appreciate all your help, regards.
Update2:
Piece of WinForms User Control where treeView is filled:
TreeView treeView = (TreeView)documentViewControl.Controls["treeViewStructure"];
TreeViewFactory treeFactory = new TreeViewFactory();
var documentStructure = treeFactory.GetStructure(document.DocumentKey, document.XmlStructure, true);
treeView = this.SetTreeViewStructureNodes(documentStructure);
Basically I'm moving from an UC to another. Both of them are part of 2 Tabs, children of a TabControl.
(Answered by the OP as a question edit. Converted to a community wiki answer. See Question with no answers, but issue solved in the comments (or extended in chat) )
The OP wrote:
Actually I got that on my own. The idea is to mutuate the ricursive idea, creating one TreeNode from the collection (IList) of TreeViewNodes. Problem 1: recursion Problem 2: how to mantain the DocKey custom property
private TreeNode[] GetTreeViewNodes(IList<TreeViewNode> nodes)
{
IList<TreeNode> returnedNodes = new List<TreeNode>();
foreach (var item in nodes)
{
TreeNode node = new TreeNode(item.Text, this.GetTreeViewNodes(item.Children));
node.Tag = item.DocKey;
returnedNodes.Add(node);
}
return returnedNodes.ToArray();
}
And the code required for the treeview becomes this one:
this.treeView.Nodes.Clear();
this.treeView.Nodes.AddRange(this.GetTreeViewNodes(documentStructure));