TreeNode key issue - c#

I am using TreeView control in WinForm.
I am trying to use the following code, but getting "NullReferenceException".
I am following the syntax provided i.e. tree.Nodes[key].Nodes.Add(key,text)
I don't know whats wrong with the code.
Please have a look at the code i used -
tvTree.Nodes.Add("Subjects", "Subjects");
tvTree.Nodes["Subjects"].Nodes.Add("Physics", "Physics");
tvTree.Nodes["Physics"].Nodes.Add("PhysicsP1", "Paper1");
tvTree.Nodes["Physics"].Nodes.Add("PhysicsP2", "Paper2");
tvTree.Nodes["Physics"].Nodes.Add("PhysicsP3", "Paper3");
Thanks for sharing your time.

Your problem is that the "Physics" nodes are not direct children of tvTree but instead are children of the "Subjects" node. What should make this easier is that TreeNodeCollection.Add returns a TreeNode that you can reference later on.
var subjects = tvTree.Nodes.Add("Subjects", "Subjects");
var physics = subjects.Nodes.Add("Physics", "Physics");
physics.Nodes.Add("PhysicsP1", "Paper1");
physics.Nodes.Add("PhysicsP2", "Paper2");
physics.Nodes.Add("PhysicsP3", "Paper3");
If you only have the name, you can use Find:
var parentName = "from wherever";
var parentNodes = tvTree.Nodes.Find(parentName, true);
/* handle multiple results */
/* add children */

Also you may achieve this with
tvTree.Nodes.Add("Subjects", "Subjects");
tvTree.Nodes["Subjects"].Nodes.Add("Physics", "Physics");
var phyNode = tvTree.Nodes.Find("Physics", true).First();
phyNode.Nodes.Add("PhysicsP1", "Paper1");
phyNode.Nodes.Add("PhysicsP2", "Paper2");
phyNode.Nodes.Add("PhysicsP3", "Paper3");

You can use this
tvTree.Nodes["Subjects"].Nodes["Physics"].Add("PhysicsP1", "Paper1");

Related

Which element to use to retrieve and store a list of div tags (C#)?

This is what i currently use, but I need this to be stored as List which I can re-use in other classes.
var docbaseResults = repo.MyProcess.MainMenuExplorer.Docbase_QCMP3_Results.Find(".//div[#class~'webfx-tree-item' and #visible='true']");
EDIT:
SOLUTION:
List<WebElement> docbaseResults = element.Find<Ranorex.WebElement>( ".//div[#class~'webfx-tree-item' and #visible='true']").ToList();
As mentioned in documentation, "Find" method returns an IList<T> collection. You can use LINQ Method .ToList() and of course you should remove "as WebElement".
List<WebElement> docbaseResults = element.Find(
".//div[#class~'webfx-tree-item' and #visible='true']").ToList();
List<WebElement> docBaseResult = new List<WebElement>();
doBaseResult.Add(element.Find('') as WebElement);
I get 0 results back when using this one... #Maxime matter
List<WebElement> docBaseResults = new List<WebElement>();
docBaseResults.Add(repo.MyProcess.MainMenuExplorer.Docbase_QCMP3_Results.Find(".//div[#class~'webfx-tree-item' and #visible='true']") as WebElement);

Umbraco 4.11 get Parent Node

Should be a simple answer.
I am using Umbraco 4.11 and I need to get the parent Node of a cast node. I am a bit of a noob to C# and I am fixing a control someone else made. Should be simple but it was originally written before the DLL update from 4.7 to 4.11.
So below is my code. I need to get the parent Node. What would be the correct syntax to do this. You can see where the old code is commented out.
Thanks in advance.
//New using
using umbraco.NodeFactory;
private string GetEmailContactProperty()
{
Node node = Node.GetCurrent();
string email = null;
do
{
if (node.NodeTypeAlias == NodeTypeAlias)
{
email = node.GetProperty("emailContact").Value;
if (!String.IsNullOrEmpty(email))
break;
}
//node = node.Parent;
//***Need Parent Node here. new Node is asking for Overload.
node = new Node().Parent;
} while(node.Parent.Id > -1);
The original code should do what you are asking with regards to getting the parent node.
node = node.Parent;
Basically, from your code you want to traverse up the tree, across your ancestors, to find a property named "emailContact" that is not IsNullOrEmpty.
I think what you are looking for is a piece of code like this:
var emailContact = CurrentModel.AncestorsOrSelf().Items.Where(n => !string.IsNullOrWhiteSpace(n.GetProperty("emailContact").Value))
Another way would be to get property with the recursive flag set to true like so:
var emailContact = Model.GetProperty("emailContact ", true).Value;
(See this post: http://our.umbraco.org/forum/developers/razor/19005-Recursive-fields-using-Razor-macro?p=1)
On a side not, it looks like you are currently working with Documents and not "content nodes", is this a back office control or a front end control?
Hope this helps

Dex graph database retrieve all nodes and edges

I know how I can output my graph database to a file (like a GraphML file), but I'd rather iterate through the nodes and have them as C# objects because I need to use them elsewhere.
Something like this:
var it = graph.GetNodeIterator();
while (it.HasNext()) {
var node = new MyNodeObject();
//get attributes or whatever
nodeList.Add(node);
}
//nodeList now contains all nodes in a List
I can't find a convenient way to do this and the Dex documentation isn't very helpful. Clearly Dex has some way of doing this because I can easily export to GraphML, but I don't want to export to GraphML and then parse the GraphML into C# objects.
Here is how I do it, not sure if it's the best way though:
//find the type(s) youre looking for based on how you assigned
//them in the first place. you may already know.
var typeIt = graph.FindTypes().Iterator();
while (typeIt.HasNext()) {
//find your types if you dont know
}
//select the types you want. lets say all of your nodes have one of two types,
//which map to attributes 4 and 3.
var select = graph.Select(4);
//union them
select.Union(graph.Select(3));
//start to iterate
var it = select.Iterator();
while (it.HasNext()) {
var next = it.Next();
//map to your own objects using next
}

Umbraco: Get "Last Edits" values from content

In my umbraco project, i require values in the "last Edited" tab, inside C# code.
How can i get those values?
Please Help,
Thanks.
It's quite simple. Here's one way of doing it:
// Assure the query is done to the root node, in this case I was on the root controller.
var recentUpdatedNodes =
Model.Content.DescendantsOrSelf().OrderByDescending(x =>
x.UpdateDate).Take(50);
foreach (var node in recentUpdatedNodes)
{
#node.Name
#node.Url
#node.Id
}

Representing a chain of nodes in a human readable way

I'm trying to represent a subway map type thing that has to be drawn progressively (like its growing).
My code all works perfectly but its unreadable. Basically it's a tree structure with recursive nodes and subnodes and my test code looks like this:
Children.Add(new TrackLine(800));
Children[0].Children.Add(new TrackSpot());
Children[0].Children[0].Children.Add(new TrackSplitter());
Children[0].Children[0].Children[0].Children.Add(new TrackRotate(-45));
Children[0].Children[0].Children[0].Children[0].Children.Add(new TrackColorChange(Color.Red));
Children[0].Children[0].Children[0].Children[0].Children[0].Children.Add(new TrackLine(100));
Children[0].Children[0].Children[0].Children[0].Children[0].Children[0].Children.Add(new TrackRotate(45));
Children[0].Children[0].Children[0].Children[0].Children[0].Children[0].Children[0].Children.Add(new TrackLine(200));
Does anyone have any suggestions of how to fix that mess?
You're looking for a way to add it to the deepest child that has no children of it's own?
class Node
{
List<Node> children ;
public void addNode( Node newNode )
{
if( children.Count > 0 )
children[0].addNode( newNode ) ; // recursive call
// to ask first child to add newNode to it
else
children.Add( newNode ) ; // just add it to the children list of THIS node
}
}
To make that code more readable I'd use variables with appropriate names (since I don't know your subject domain, mine are probably not appropriate):
Children.Add(new TrackLine(800));
var lineA = Children[0];
lineA.Children.Add(new TrackSpot());
var spotA = lineA.Children[0];
spotA.Children.Add(new TrackSplitter());
var splitterA = spotA.Children[0];
splitterA.Children.Add(new TrackRotate(-45));
var rotateNeg45 = splitterA.Children[0];
rotateNeg45.Children.Add(new TrackColorChange(Color.Red));
var colorRed = rotateNeg45.Children[0];
colorRed.Add(new TrackLine(100));
var lineB = colorRed.Children[0];
lineB.Children.Add(new TrackRotate(45));
var rotatePos45 = lineB.Children[0];
rotatePos45.Children.Add(new TrackLine(200));
var lineC = rotatePos45.Children[0];
Another approach is to use strings as indexes for Children like this:
Children.Add(new TrackLine(800));
Children["lineA"].Children.Add(new TrackSpot());
Children["lineA"].Children["spotA"].Children.Add(new TrackSplitter());
However I probably would not do it because managing the string constants would be a mess on its own and it would probably require changing Children implementation.

Categories