3

Hi I'm trying to attach a child node to a parent node depending on if their strings match.

I'm running into some problems because I'm not sure how to identify a parent by it's name alone. For example if "string a" == "string a" then I want to add the child nodes LoanName to the parent id where the string matches the parent name.

This works if I do:

tvTodoList.Nodes[0].Nodes.Add(activityResult.ActivityName);

But obviously this will attach the child node to the first parent node in the treeview. How do I get it to match the name of the variable loanresult.LoanName?

Below is the code for my FillTodoList method

private void FillTodoList()
{
    var nol = NetworkOpsLayer.NetworkOpsLayer.CreateForDirectMongoConnection("mongodb://localhost", "test", "loans");
    //demoSave(nol);

    var loanList = nol.GetDocsWhichMatchGivenDocString("{ \"isActive\" : 1 }");

    foreach (string s in loanList)
    {
        //System.Console.WriteLine(s);
        var loanResult = JsonConvert.DeserializeObject<RootObject>(s);

        tvTodoList.Nodes.Add("Loan Name: " + loanResult.LoanName);

        //Add children to each Loan

        var con = NetworkOpsLayer.NetworkOpsLayer.CreateForDirectMongoConnection("mongodb://localhost", "test", "activity");
        //demoSave(con);
        var activityList = con.GetDocsWhichMatchGivenDocString("{ \"isActive\" : 1 }");

        foreach (string st in activityList)
        {
            var activityResult = JsonConvert.DeserializeObject<Activity>(st);
            if (loanResult.LoanName == activityResult.ParentLoanName)
            {
                tvTodoList.Nodes[loanResult.LoanName].Nodes.Add(activityResult.ActivityName);
            }
        }
    }
}
ivayle
  • 1,070
  • 10
  • 17
A1raa
  • 605
  • 1
  • 7
  • 22
  • Don't spam random tags or omit important ones. This has nothing to do with node.js or WPF. It *does* have to do with winforms. – 15ee8f99-57ff-4f92-890c-b56153 Oct 28 '16 at 16:50
  • 2
    Use `Name` property of a node to assign a string identifier if you can. Otherwise use `Tag` property to add additional information. You can find a node using its `Name` or its `Tag` as described here: [How to select a Node by Tag in Windows Forms TreeView](https://stackoverflow.com/questions/34228617/how-to-select-a-node-by-tag-in-windows-forms-treeview) – Reza Aghaei Oct 28 '16 at 20:23
  • 1
    @RezaAghaei You are right. Although this didn't entirely solve my question it did assist me in getting to the answer. I will mark it as the answer and below comment on the extra steps I took to get the solution. – A1raa Oct 31 '16 at 14:44

1 Answers1

2

The method TreeNodeCollection.Find(string, boolean) will help with this...

foreach (string st in activityList)
{
    var activityResult = JsonConvert.DeserializeObject<Activity>(st);
    if (loanResult.LoanName == activityResult.ParentLoanName)
    {
        TreeNode[] matches = tvTodoList.Nodes.Find("Loan Name: " + loanResult.LoanName, false);
        if (matches.Length > 0) matches[0].Nodes.Add(activityResult.ActivityName);
    }
}
Blake Thingstad
  • 1,639
  • 2
  • 12
  • 19
  • 1
    Using the `Name` property you don't need to search yourself in nodes collection. Just use `treeView1.Nodes.Find("something", true)`. – Reza Aghaei Oct 28 '16 at 22:05
  • 1
    Upon further inspection I've found that you're right. The parameter named "key" is a little misleading, I thought it was actually referring to a key. – Blake Thingstad Oct 29 '16 at 01:23
  • @BlakeThingstad Thanks for your answer. Unfortunately this is not adding any child nodes to the parent node. The `matches[0].Nodes.Add(activityResult.ActivityName);` never runs. I'm trying to figure out why because the strings match just fine.. – A1raa Oct 31 '16 at 10:38
  • @A1raa If the strings match then there should be at least one result in matches which would allow it to go into the if statement. When debugging, does matches ever have anything in it? – Blake Thingstad Oct 31 '16 at 16:15
  • 1
    @A1raa I just realized when you add the node to tvTodoList you concatenate loadnResult.LoanName with ""Loan Name: ". You'll need to use that to sea5rch tvTodoList.Nodes. I'll edit the answer to include this. – Blake Thingstad Oct 31 '16 at 16:17
  • 1
    @BlakeThingstad It was my bad. I had `tvTodoList.Nodes.Add(loanResult.LoanName);` which was giving me `Text: "TestOne"` and `Name: ""` after adding `tvTodoList.Nodes.Add(loanResult.LoanName, loanResult.LoanName);` it gave me the `loanResult.LoanName` as the `Text` and `Name` which is what I was after. I then used your code to match the `Name` which was "". I didn't have the overloads setup correctly. – A1raa Oct 31 '16 at 16:18
  • @A1raa I was getting Name and Text confused too. I think you should be able to figure it out decently easily from here though. Just need to make sure the correct text is being searched for on the name and that the name is being set as expected. – Blake Thingstad Oct 31 '16 at 16:20