Associating Windows Forms TreeView items to actual data - c#

So - working with C# and Windows Forms, I have a bunch of "Task" classes. Every class has a List<Task>, so that they can have any number of children (and the children can obviously have more children...)
These tasks are stored in a "Project" class (in a List<Task>, obviously), which is then serialized to XML. I have a function to recurse through the tasks and put them all in a TreeView. It works just like it should.
But the thing is - if I select a task from the TreeView, how will I find it from the Project? I thought of some kind of foreign key (which would be the TreeNode's key?), but since I'm using XML for serialization, that's not possible.
So, what should I do?

I would put a reference to the "Task" object in the Tag member of each TreeNode. It then becomes very easy to cast the Tag to a Task and use the Task when handling any Tree event.

You have to give each Task a unique key (store them in a Dictionary), then set that int value to the TreeNode's tag.

Every tree node have FullPath property (which represent path from root to node), you can create dictionary, and fill it with your tasks, using FullPath as the key. Then, when node clicked, you take node FullPath and extract task from dictionary.

Related

What is the best way to associate a Nested Content node to an Umbraco Member

My initial thought was to create an Umbraco Relation and associate the Umbraco Member to the Nested Content node. Sadly, I found this form post asking a similar question and as you can see in Matt Bailsford's first response:
Unfortunately nested content can't have an ID value as they don't truely exist
I did find the issue/feature that was discussed in the forum post; however, it just adds parent information to the DetachedPublishedContent object and doesn't solve my issue. After reading the form post and the conversations of Hendy Racher, Matt Bailsford and Lee Kelleher in the github pull request, I still don't understand why Nested Content doesn't create a node in Umbraco.
So basically I need the Nested Content nodes to be created as Umbraco nodes and then stored as a JSON string in the property field. There are a few ways that I see this could be accomplished:
Create a Custom Property Editor for Umbraco Backoffice - I would start with a copy of Nested Content and add code to create the node and attach it before saving the node as a JSON string.
Use the Umbraco Multinode Treepicker control - This control was suggested by Hendy and Jeavon in this forum post as a way to allow a user to select multiple content nodes. Unfortunately going this route would require the user to create the "nested content" nodes first. Then they could associate those "nested content" nodes with the original node. We really like the user experience of the Nested Content control where it allows you to create nodes dynamically in the property editor.
Find a way to associate the Member to the "Nested Content" node - This option would require that I store an association between the top node and it's respective "nested content" node to a Member in Umbraco. There are two issues that come to mind when trying to go this route:
How should I associate the "nested content" node to the Member in a standard Umbraco way? - I immediately think of creating a link table in the database but, in my understanding, that is not the standard Umbraco way. I am still fuzzy about the best way to do this inside Umbraco.
Is there a way to uniquely identify the "nested content" node? - I realize there is a sort order value being set according to the pull request I found above but if the user reorders the nested content items will it change the "nested content" node to member association?
At this point, I am leaning towards going with option 1, but I wonder if option 3 is a better direction. In reality, I don't believe this is a new problem that someone hasn't already solved, and I hate to create another custom property editor if there is one just like it out there already.
So if you know of a better way to solve this problem please let me know.
The problem is - as you mention it - that Nested Content nodes aren't really real nodes. I don't think the right way to solve your problem is to try hacking Nested Content into doing something it really wasn't created to do.
The problem about creating nodes and also having references to them on the Nested Content node is that essentially every node in Umbraco needs to "live" somewhere.
You could choose to say that a node lives under the parent it is nested into, but how would you then differentiate between nested nodes and actual child nodes - this would require another hack as it is really working around how nodes are meant to be structured and handled in the Umbraco core.
Even if you did manage to get this working, I suspect you would have a lot to deal with to actually make it work as good as Nested Content currently does:
You would have somehow wrap every single node in the Nested Content editor into a object to be able to store meta data like the node ID it is connected to and the sortOrder when reordering all of the nested content nodes you have on there.
(edit: I think it actually already stores some sort of wrapper object here, but you would have to change the logic in here to actually handle a reference to another node instead of just deserializing json stored here, as a node)
You would also have to manually hook into events making sure the actual edits you do while on the parent node actually ends up being persisted to the nested nodes.
Deleting a nested content node, or a parent that has any nested nodes - you would have to handle deleting any orphaned nodes.
There's most likely a lot of stuff I've missed but my point is - you will have a lot of trouble trying to do this.
I think you should consider another approach if you really need to do this:
It would be possible to create a picker similar to a normal node picker, that simply allows you to browse through nodes as a normal picker would do. When you pick a node, instead of just selecting it, it should then fetch the nested content nodes and show those in the UI.
There's however the little quirk that you could essentially be having multiple properties storing each their set of nested content nodes on one single content item - so you would need some nice way of handling this in the UI.
When you select one or multiple of the nested nodes, what your picker would store would be something similar to [guid-of-the-real-node]_[propertyAlias]_[guid-of-nested-content-item].
I am not certain if Nested Content ever got the GUID unique ID/key feature implemented - Matt and I discussed it some time last year and we tried adding it in, in a custom build I needed for a project. If it isn't there I would suggest you ask Matt if he can get that in. It was essentially just giving each nested content item a "fake" unique ID (GUID) that you could use to identify it from other nested content items stored in the property. (You would have to ask Matt about the status of this)
Doing this would allow you to (on your member) have a reference that you lets you find the actual content node, then the property where the content is stored, and lastly the actual nested content node you have picked.
You should however note that this is very prone to breaking and needs a lot of null handling:
If you change the property alias of the property you are storing nested content in on the parent, it will lose the reference.
If you delete the content item storing the nested items, the picked items no longer exist and you have a missing reference in the picker on your member (needs null handling)
If you delete a nested content item - same as above. You have a missing reference in your picker.
Apart from the solution above, I don't really see another way of doing this currently with the requirements you have.

How to get all the top level htmlelements from C# WebBrowser?

I want to show the DOM as it is on the web browser with all comments and html, head, body, etc.. preserve its structure. Currently, I can only start from node html. Document.All didnt help.
The only way I can see is webBrowser1.Document.Body but I would miss the commentss, head etc.. Then if I go with Document.All then that gives me all the nodes.
I think the only choice with the WebBrowser control to get what you want is to use Document.All. Although this gives all elements not just top-level, each element has a .Parent element property so you can loop through them (or use Linq) and get only the ones that have <body> or <head> as the parent element.
Try using HTMLAgilityPack, it support Xpath so you can get any node as you want.
As suggested by hienvd_csuit, I think HTML Agility Pack is your best option. If you still want to use the WebBrowser, a possible solution is to access the unmanaged DOM directly, using dynamic (requires .NET 4+). For instance you can do something like this:
dynamic dom = wb.Document.DomDocument;
foreach (dynamic node in dom.childNodes)
{
Console.WriteLine ("{0} - {1} - {2}", node.nodeType, node.nodeName, node.nodeValue);
}
Of course, you need to know the structure of the DOM, since intellisense doesn't work on dynamic objects; you can find some information about it here.
You should be able to query (there is a property somewhere) if a particular item has a child node or not, also, you can query if it is a parent node or if a particular item has a parent or not, and if it does, discard, and you can keep querying for parent such as item.parent.parent (pls check intellisense for exact object/property names) and if it returns nothing, it means there is only one parent (assuming item.parent doesnt return nothing), and you can organize how many levels deep the nodes can/must be. So based on the child or parent checking method (or both) you can choose to either include it in your collection or discard it.
Of course, you might get many "P" tags or DIV/SPAN tag's as your top level nodes/items. So, i'm assuming there is a chance you will not want these, so feel free to discard them and query their children.

Dynamically mapping xml fields to a static object

I would like to provide the user a visual DOM like representation of an XML structure (here a completed infopath form) and allow them to specify which elements of the xml data they want to map to a statically compiled object.
As an example, the user has an infopath form that allows them to enter a sales deal, they fill it out and submit, the app should allow them to see the structure of the data in the infopath form (in a friendly, treeview kind of way) and specify how it should map to a static representation of the sales deal (think of a row that might go in the "Deals" table).
The Infopath forms (xml source) are not controlled by me.
I'm looking for suggestions on how to display the treeview of the XML and allow interaction with it to specify the mapping (possibly drag and drop?).
This will be in a wpf application (I know I'll have to host the infopath control in a forms host since it is not wpf) written in c#, and we would prefer to use .Net elements provided by Microsoft or open source software.
Edit: As a more thorough example, let's say there is an infopath form that results in xml that looks something like:
<Deal id="1" dateBooked="2011-01-01" term="24" language="en-us">
<Salesman>Jim Flowers</Salesman>
<FinancedAmount>55000.00</FinancedAmount>
<Items>
<Item id="1" quantity="10" unitPrice="10000.00">Tractor</Item>
<Item id="2" quantity="1" unitPrice="5000.00">Spare Blade</Item>
</Items>
<Notes>
<Note dateAdded="2010-09-20">Customer needs a spare blade</Note>
<Note dateAdded="2010-12-31">Customer wants to sign the deal on new year's day, I find this odd...</Note>
</Deal>
I want the user to, at run time and in an intuitive manner (the user will not know xpath...) map specific nodes of a treeview of this document to a field on my "Deal" object. So, the Deal object might have an id, salesman, amount, items collection, etc. that need to be populated but the notes and some other data are ignored, and the forms will not be the same (info path is providing configurable forms for the user to get data into the system however they want) or named in any consistent matter. The user is specifying the mapping.
Andreas's solution looks like it would be a good first step. Looks like for your input it would need to recurse into attributes of the nodes as well tho.
My description of your plan would be:
Build a control that shows all the data (Andreas uses a TreeView)
You'd probably want to show the Name and the Value for each
Build a similar control for the Object you are loading into
Setup events to build the mapping
You mentioned dragging between them
Or if they are both checkbox trees maybe have them check one in the Xml tree, then one in the Object tree and reset both, building a list of the mappings created
Finally use that mapping list to fill the object
I don't see any simple way to do it, its a lot of work and a lot of it depends on the specifics of the implementation so I can't just give you code that can do it.
How I think I might do it to get it working quickly:
Recurse through the xml generating xpaths as you go for each and every value
Load all of those xpaths and the names and values they represent into a table to be displayed to the user
With columns for "Name", "Value", "xpath"-hidden, "Load Into Property"-dropdown
Build a drop down listing every property in your object, have that in a column in the table displayed to the user
Basically I believe you need to break this down into smaller steps, and search for help on each specific step.
I'd go the simpliest way possible - create a treeview from the given xml and add a checkbox to each node. When the user clicks the "OK" (or whatever) button, you iterate all checkboxes that are checked and build your object.
Creating a treeview from a xml is rather simple - this should work (i did not test it however!)
public static class TreeViewExtensions
{
public static void LoadXml(this TreeView treeview, XmlDocument doc)
{
treeview.Nodes.Clear();
RecursiveImport(treeview.Nodes, doc.ChildNodes);
}
private static void RecursiveImport(TreeNodeCollection tvNodes, XmlNodeList xmlNodes)
{
TreeNode tvNode;
foreach (XmlNode xmlNode in xmlNodes)
{
tvNode = new TreeNode(xmlNode.Name);
if (xmlNode.ChildNodes.Count > 0)
RecursiveImport(tvNode.Nodes, xmlNode.ChildNodes);
tvNodes.Add(tvNode);
}
}
}
Edit: Well basically, you could list all xml fields in a listbox, and all fields of your object in another. When a user drag & drop's 1 field to your object's listbox, you'd need to save this relationship in another object.
I'm not familiar with wpf but this article seems pretty good - http://www.codeproject.com/KB/dotnet/csdragndrop01.aspx
The "link" Enumeration type looks pretty promising for what you try to achieve.

TreeView, Access DB & Windows Form (C#)

hello friends I am in trouble, i have a school project in which I have to use tree view control to display fullname from DB in the following form.
suppose A is super memebr & under A there are others, and under other there could be other memeber and so on. its like chain system which will show referred member in hierarchical view.
Thank You
I would first create a class to represent a node in the tree: "Node".
That class should have as properties:
1. a List of child nodes
2. a string for full name
Then create instances of the Node class, drawing the data from your database.
Now you will have an in-memory representation of your data.
Now write a recursive function that takes an instance of Node class and returns an instance of TreeNode for use in the TreeView control. The function should recurse through all the child nodes of each Node instance.
Run that function on the root Node and you should have your TreeNode.

Equivalent (functionality) of ObjPtr from VB6 in C#?

Does any one know if C# has an equivalent of ObjPtr from VB6, or equivalent functionality (see more info below)? Here are a couple of links to info on ObjPtr devx , thevbzone.
Basically I have a third party treeview that I need to walk thru to get specific nodes but the only (relevant) info the nodes have is name ... but the node names don't need to be unique. So I need to get a unique value for each node as I walk thru it the first time so when I walk thru it again I know which is which. In the old school VB6 days I would use ObjPtr.
The closest direct equivalent I can think of would be to use a GCHandle to get an IntPtr for your object reference.
You would need to allocate a GCHandle for your object (GCHandle.Alloc), then use GCHandle.ToIntPtr to convert to an IntPtr. The linked documentation shows the process.
If they're objects, why not just store the object references directly? These will be unique.
You can use Object.ReferenceEquals(x, y) to determine if a reference you have stored is referring to the same object you just retrieved from the tree.
If the treenode has FullPath property, you can use it to uniquely identify a node in the treeview (Winforms Treeview has the FullPath property). This won't be unique if 2 siblings have same text in it.
OR
You could use Handle property of the TreeNode.
GetHashCode should work well for testing unique values unless the third-party has overriden the Object implementation with something that doesn't make sense in your scenario.
I would assume that nodes in the tree would define equality/hashcode by more than just the value string, but you would need to check.

Categories

Resources