Click here to Skip to main content
15,890,897 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
My program is made to generate latex files to one main latex file in winforms. I am using add nodes button event to add all my latex chapters(parent nodes) and section(children nodes). Main point of this project is to remove unwanted chapters or sections, by this option I can generate various types of main latex files. Also I am making a save and load buttons. I am saving everything to a JSON format file and later loading it. This is because I want to have a document configuration file for every generated main file( its like previous generated file) . So why I am writing here is that, my loaded button isnt doing anything with my append line( isnt filling my main latex file). If I use add_nodes button and generate file, everything is working and main file is filled with needed information, but when I load my JSON file, it is loaded correct in win forms window, but main file isnt filled. Maybe you can tell me why? and what should I do?
Here is my code:
First of all, I am adding all my parent and children node:
C#
private void addNodeButton_Click(object sender, EventArgs e)
           {
               treeView1.Nodes.Add(new TreeNode("ccc") { Tag = @"\include {ccc}" });

               treeView1.Nodes[0].Add(new TreeNode("bbb") { Tag = @"\input {bbb}" });


This is how I use my append line with chapters, I am using header and
footer file and with append line help I am inserting Tags to needed
place in my main file:

C#
private void button2_Click(object sender, EventArgs e)
            {
                {
                    var header = File.ReadAllText(@"C:\dir\header.tex");
                    var footer = File.ReadAllText(@"C:\dir\footer.tex");
                    var sb = new StringBuilder();
     
                    sb.AppendLine(header);
     
                    foreach (TreeNode node in treeView1.Nodes)
                    {
     
                        var tag = node.Tag as string;
                        sb.AppendLine(string.IsNullOrEmpty(Text) ? node.Text : tag);
     
                    }
     
                    sb.AppendLine(footer);
                    File.WriteAllText(@"C: \dir\final.tex", sb.ToString());
                }


Later, I am saving it and loading:

C#
private void saveButton_Click(object sender, EventArgs e)
               {
        
                    this.treeView1.Save(jsonFilePath);
                }
         private void loadButton_Click(object sender, EventArgs e)
                {
                    this.treeView1.Nodes.Clear();
                    this.treeView1.Load(jsonFilePath);
                }


And this is my class for loading:

C#
public static class TreeRepository
        {
    
            public static List<DataNode> LoadDataNodes(string jsonFilePath)
            {
                var json = System.IO.File.ReadAllText(jsonFilePath);
                var dataNodes = JsonConvert.DeserializeObject<List<DataNode>>(json);
                return dataNodes;
            }
    
            public static void Load(this TreeView treeView, string jsonFilePath)
            {
                treeView.Nodes.Load(LoadDataNodes(jsonFilePath));
                
            }
    
            public static void Load(this TreeNodeCollection nodes, List<DataNode> dataNodes)
            {
                foreach (var dataNode in dataNodes)
                {
                    var treeNode = nodes.Add(dataNode.Text);
                    treeNode.Checked = dataNode.IsChecked;
                    if (dataNode.Children != null && dataNode.Children.Count > 0)
                    {
                        Load(treeNode.Nodes, dataNode.Children);
                    }
                }
            }
    
            public static List<DataNode> GetDataNodes(this TreeView treeView)
            {
                var dataNodes = new List<DataNode>();
                AddNodesToList(treeView.Nodes, dataNodes);
                return dataNodes;
            }
    
            private static void AddNodesToList(TreeNodeCollection nodes, List<DataNode> dataNodes)
            {
                foreach (TreeNode node in nodes)
                {
                    var dataNode = new DataNode
                    {
                        Children = new List<DataNode>(),
                        IsChecked = node.Checked,
                        Text = node.Text
                        
                    };
                    dataNodes.Add(dataNode);
                    if (node.Nodes.Count > 0)
                    {
                        AddNodesToList(node.Nodes, dataNode.Children);
                    }
                }
            }
Posted
Comments
BillWoodruff 2-Dec-15 4:04am    
How about we leave this question open long enough for the OP to answer any questons we may have ? thanks, Bill
Member 12175492 2-Dec-15 4:20am    
I think main problem is that, I don't know how to use TAG's with my loading class. Maybe you could give me some tips or example?
Member 12175492 2-Dec-15 5:54am    
Np, everything working I just needed to clear my jsonfile, lack of experience :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900