Reputation: 153
I have this structure of classes:
public class L3Message
{
public int Number { get; set; }
public string MessageName { get; set; }
public string Device { get; set; }
public string Time { get; set; }
public string ScramblingCode { get; set; }
public List<Parameter> Parameters { get; set; }
public L3Message()
{
Parameters = new List<Parameter>();
}
}
public class Parameter
{
public int numOfWhitespaces { get; set; }
public string ParameterName { get; set; }
public string ParameterValue { get; set; }
public Parameter Parent { get; set; }
public List<Parameter> SubParameters { get; set; }
public Parameter()
{
SubParameters = new List<Parameter>();
}
}
So, as return type from one of my Methods I have List
of L3Messages
(List<L3Message>
), and I need to map that to TreeView
in WinForms
(populate TreeView
from that List
).
EDIT: Please notice that tree of my objects can be deeper than one level (becouse class Parameter have prop List < Parmaeter > (List of Parameter object, same structure as root parameter object)), so that means recursion or something like.
EDIT2:
Here is pic of tree, but this tree is created from text file base on whitespaces, so here is all Parameters, in my tree I need only one from List of L3Message objects.
http://imageshack.us/photo/my-images/803/treeviewmessage.png/
EDIT3:
I'm sure that my TreeView need to be something like this:
L3Message.Number: L3Message.MessageName
+L3Message.Time
+L3Message.Device
+L3Message.ScramblingCode
+L3Message.Parameters[0]
++Parameter.ParamaeterName: Parameter.ParameterValue
++ (same as above)
L3Message.Number: L3Message.MessageName
+L3Message.Time
+L3Message.Device
+L3Message.ScramblingCode
+L3Message.Parameters[0]
++Parameter.ParamaeterName: Parameter.ParameterValue (in this occasion Value is null )
+++SubParameter.ParameterName: SubParameter.ParameterValue
Something like that
If possible, I would like to that in separate thread.
How can I achieve that?
Upvotes: 0
Views: 2961
Reputation: 153
I managed to solve this, but I think that is not optimize and there is no separate thread. If anyone can modify my code to perform better and add separate thread?
SOLUTION:
foreach (L3Message message in por)
{
treeViewMessages.Nodes.Add(message.Number + ": " + message.MessageName);
treeViewMessages.Nodes.Add("Time: " + message.Time);
treeViewMessages.Nodes.Add("MS: " + message.Device);
treeViewMessages.Nodes.Add("Scrambling Code: " + message.ScramblingCode);
foreach (Parameter param in message.Parameters)
{
TreeNode tnRootParam = new TreeNode();
//tnRootParam.Nodes.Add(param.ParameterName + ": " + param.ParameterValue);
if (param.SubParameters.Count != 0)
{
CreateTreeNodes(param, tnRootParam);
tnRootParam.Text = param.ParameterName;
treeViewMessages.Nodes.Add(tnRootParam);
}
else
{
tnRootParam.Text = param.ParameterName + ": " + param.ParameterValue;
treeViewMessages.Nodes.Add(tnRootParam);
}
}
treeViewMessages.Nodes.Add("---------------------------------------------------------------------");
}
private void CreateTreeNodes(Parameter parameter, TreeNode tnRootParam)
{
if (parameter.SubParameters.Count == 0)
{
tnRootParam.Nodes.Add(parameter.ParameterName + ": " + parameter.ParameterValue);
}
else
{
foreach (Parameter subparam in parameter.SubParameters)
{
CreateTreeNodes(subparam, tnRootParam);
}
}
}
Upvotes: 0
Reputation: 62439
Of course it is possible. Now it depends how you want your TreeView
to be structured. Then you just need to create TreeNode
objects and add them to the TreeView
. See this small tutorial: http://www.dotnetperls.com/treeview
If you are going to do this on a different thread, you will need to update the GUI by forwarding the updates to the GUI thread using BeginInvoke
:
TreeNode node = new TreeNode("node");
L3Message msg = new L3Message();
node.Tag = msg;
treeView.BeginInvoke(
(Action)(() =>
{
treeView.Nodes.Add(node);
}));
Notice that the TreeNode
needs to be created with a string
representing the name and then you can add the object it points to using the Tag
property.
Upvotes: 1