TreeView, Access DB & Windows Form (C#) - 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.

Related

Traversing/comparing tree structures with only parent references

Consider a tree structure with nodes containing references to only their parent. So, the root node's parent will be null. This tree represents a class hierarchy. The goal is to search such a tree for a particular subtree, and if it doesn't exist, add it to the tree. Is there an algorithm that can be used to achieve this?
In this particular case, I have a C# WinForms application which uses this kind of structure. At the moment, an iterative breadth-first traversal is being used to traverse through such a tree and display it as a treeView. How this is being done is as follows: To represent the nodes of the tree itself, there is a class Data_Node. To perform the traversal, the nodes are placed into a queue. The queue contains instances of a data structure, QueueNode:
private class QueueNode
{
public Model.Data_node dataNode; // actual tree node
public QueueNode parent; // reference to the parent
public TreeNode treeNode; // node for treeView in the UI
}
The next step is to implement another such tree (assume it is already, then check whether it is a subtree of the first one, and add it if it is not.
I have researched examples of what I am trying to achieve, but they were only for binary search trees, with nodes having references to the child nodes. In my case, the nodes only have references to the parent node.

C# - How to Store Multiple Informations (mapped)

i have a huge Problem and i donĀ“t know with which methode i should solve it...
The current state of the Program: The User is currently able to create new Items of a ToolMenuStrips during runtime. The MenuStrip ALWAYS has 3 Layers. for example: Parent - Child 1 - Child of Child1. There can be multiple Parents, multiple Childs and multiple Child of Childs, only Rule is they cant have the same name. If i click on the 3. Layer it opens an Event which gives me the Name of the 3. Layer i clicked on. (Note: The ID/Names of the Items are like this : Parent= ParentName ; Child= ParentName+ChildName ; Child of Child = ParentName+ChildName+Child of ChildName) This all works great!
The "Problem" : Now the 3rd Layer (child of child) is created along with some Informations(including a Path and Filename), which are stored in an array. This array need to be bound to the 3rd layer.
My goal: When adding a new 3rd Layer the Filename and Path of it should be add to a list of an Observer who is waiting for new Files with the FIlename to be created in the Folder. (So i have multiple Paths im observing at the same time) Furthermore when the User clicks on a 3rd Layer it opens an instance of a Form, to create the istance of this Form i need to pass the Informations bound to this 3. Layer. Which then will be displayed.
The next steps of the Program:
I need the dynamic created MenuStripItems with the Informations to be stored in settings or XML so the program can load the whole datas again upon different Runtime (closing and restarting the program)
My Question: What is the best way to accomplish this? My ideas:
a.) using tuples -> (its hard to understand and probably garbage coding)
<string Parent, multidimensional array [childs,child of childs], map <string Filename, string Filepath>>
b.) using Model Classes -> Creating dynamic Classes *(I have no clue how to do that, i mean yes 1. dynamic Class = "Parents" Which gets the informations about all the Childs and Child of Childs and the other Informations as parameters on initialization, but how to deal with deleting some elements / adding some to it)
c.) I read something about "treeview" where you can initialize .Node with some Key Values, but i do not know if its the right thing for this?
d.)...?
Maybe this is easy to solve but i am lacking of knowledge... I know this is a farily open question but i have absolutely NO CLUE how this should be dealt with... I had an idea of storing all of it in just 1 Multidimensional Array but this is so messy that i dropped it... Since its hard to distinguish between Parent and its Childs and its Child of CHilds and so on... (a 10 Dimensional array would do though xD)
THANK YOU in advance!
Best regards,
Christian
Edit: Example of the Informations:
Example with 1 Parent (there can be Multiple of them named different than A1 -> A2 for example with the same Name of childs)
A1 // Parent
B1 // child of HG1
B2 // other child of HG1
C1 // Child of B1
C2 // another Child of B1
C3 // child of B2
Now when C1,C2,C3 are created each of them has a List like this:
List ListC1= {StringnameC1,StringpathC1,PictureC1;}
List ListC2= {StringnameC2,StringpathC2,PictureC2;}
List ListC3= {StringnameC3,StringpathC3,PictureC3;}
The Problem is currently i dont save the List i get from a 2nd Form, i just overwrite it when i create a new ToolStripItems ( i need a way to store all Lists and acces them cleanly) And when i Click on the MenuStripItem C3 i need to access the ListC3 with its Items. On Top of that i need a simple way to Store all these Informations during runtimes.

Syntax rewriting

I need to transform all of the properties of a certain class using Roslyn.
What is the recommended way to apply more than one transformation to a syntax tree without invalidating references into it?
Here is what I've tried and where I'm stuck:
In the first pass a descendant of CSharpSyntaxWalker is visiting all PropertyDeclarationSyntax nodes, and storing them in a list.
In the second pass a CSharpSyntaxRewriter is transforming the nodes while checking that each visited node equals one in the list before transforming it.
The problem with my attempt is: When I transform a property I add new fields to the class which causes the class to mutate. All the references in the list to the other properties become invalid in the new tree for the class.
It seems to be inefficient to revisit the whole class, and either way I cannot detect the property nodes already handled due to the reference difference.
I would not recommend to reference nodes from a SyntaxTree you want to modify. In your case just using a CSharpSyntaxRewriter in a single pass (without keeping references from a pre-processing pass) would be sufficient, because its VisitPropertyDeclaration method will only be called once per property, so there is no need to keep track of the nodes you've already modified.
The CSharpSyntaxRewriter also visits the nodes bottom-up, so the overrides should always be called with a node from the original tree. Most likely you have modified the node through the call to base.VisitPropertyDeclaration() before comparing its reference to the stored one. So you could still keep and compare references if you really wanted to.
public class PropertyRewriter : CSharpSyntaxRewriter
{
public override SyntaxNode VisitPropertyDeclaration(PropertyDeclarationSyntax node)
{
// 'node' should be still from the original syntax tree here
node = (PropertyDeclarationSyntax)base.VisitPropertyDeclaration(node);
// 'node' might be replaced here
return node;
}
}
Instead of keeping references to the nodes you want to modify, you could annotate them. Annotations to a node will survive modifications of the containing SyntraxTree as long as the node itself isn't replaced. You can add them like this:
node = node.WithAdditionalAnnotations(
new SyntaxAnnotation("propertyToChange", "todo"));
To retrieve the annotation again either use node.GetAnnotations("propertyToChange") or use GetAnnotatedNodesOrTokens("propertyToChange") to retrieve all nodes or tokens with an annotation of the given kind ("propertyToChange").

Recursive tuple

I am trying to create a recursive Tuple in C#. One side is a reference a class, and the other side goes into recursion to a likewise datastructure. The right side can be null, and this is when the recursion ends.
At the moment I am using an alias, but this does not seem to work.
using Relation = System.Tuple<Node, Relation>;
Directly typing this is not possible, as this goes infinite.
System.Tuple<Node, System.Tuple<Node, System.Tuple<Node, ...>>>
1.
Is this possible in C#?
2.
Can I do this with aliases?
Extra info
I am doing this because I need to keep track the parent of each node. A node can have multiple children. I cannot edit the node class.
I only need to go up the hierarchy. i.e. I don't need to know a nodes children. I use this for an algorithm where I calculate alot of nodes, and when I find one, I want to find the path to it from bottom to root.
You can create a Class for this.
class Relation
{
Node Node; // Node is another class
Tuple<Node, Relation> Relation;
}

Associating Windows Forms TreeView items to actual data

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.

Categories

Resources