Resolved TreeView Does Not Display Nodes

FerroCoder

Member
Joined
Jul 20, 2023
Messages
18
Programming Experience
10+
Hi,

I have a TreeView object in a form that is not displaying its nodes. Repeated calls to the method noted here update editorTestDefinition properly, where editorTestDefinition is the TreeView object. Text values and image indices are correct. No exceptions are thrown and the validation loop shows that the TreeView object is being updated appropriately.

C#:
public void add_task( CTask Task )
 {

     TreeNode l_tnTreeEntry = new TreeNode();

     l_tnTreeEntry.Text = Task.get_task_name();
     l_tnTreeEntry.ImageIndex = Task.get_no_execute()? 0 : Task.get_editor_icon_list_index();
     l_tnTreeEntry.SelectedImageIndex = Task.get_no_execute()? 0 : Task.get_editor_icon_list_index();
     l_tnTreeEntry.EnsureVisible();  // Added to try to force the node to appear.

     int l_iIndex = editorTestDefinition.Nodes.Add( l_tnTreeEntry );  //  editorTestDefinition is the TreeView object.

    //  Validate TreeView status
     for( int i = 0; i <= l_iIndex; i++ )
     {

         String l_sIcon = editorTestDefinition.Nodes[ i ].Text;
         int l_iImageIndex = editorTestDefinition.Nodes[ i ].ImageIndex;

     }  //  for( int i = 0; i <= l_iIndex; i++ )

     return;

 }  //  public void add_task( CTask task )

However, after repeated calls, this remains the window view:

Empty Editor.png


The tree is docked to the parent window. There are no other elements in the EDITOR window that could mask the tree.

Essentially identical code works for other trees in other windows. Direct comparison between working windows' and tree controls' properties and these form/TreeView properties show no substantive differences.

Adding a node at design time displays the added node on execution.

image_2023-10-11_095948086.png


Note that all elements in the tree are at the root level, suggesting a list box. I have tried a list box, using only text entries, and I get the same behavior. This suggests to me that there is something about the form, and not the tree control, that is causing this behavior.

The IDE is VS 2022 and .NET 6.0 is targeted.

Any help leading me to my error would be greatly appreciated.

Thanks,

FC
 
If you run Spy++ on your form, what is the size of the tree view or list box?

If I had to guess, I suspect that you have the wrong dock style option, or that the autosizing for the control is not set correctly, or that the size of the control is not set correctly.
 
In Fill/Dock to Parent docking the TreeView does seem to extend into the parent window's frame. This is evident in Spy++ and also if I set AutoScroll true in the parent. Assigning a fixed-sized TreeView that fits entirely within the window does not resolve the issue. Nor does experimenting with AutoSizing. At this point in the prototype, the EDITOR window is very simple. I think I may try starting from scratch. I have adjusted some of my design outside the window that may, somehow, have affected behavior inside the window.
 
If you add that node at design time, and then run the code, is that design time node still visible?
 
Yes... The second image in my post was meant to convey that. I'm sorry that it wasn't clearer. The arrow actually does say "Execution", but it is black against a black background.

FC
 
That would tend to suggest that there is something wrong in the code above which adds another node. Did you set a breakpoint and verify that the code is even being called?

If it is being called, try replacing the Add(l_tnTreeEntry) with a simple Add("test").

Are you calling that method in the proper UI thread? Are you making sure that the message pump has a chance to run after you've added the new node so that UI can repaint the form and its children?

Also, have you tried calling Invalidate() on the tree view to force it to repaint?
 
Side note on your naming conventions; the vast majority of coders would write your code like:

C#:
    public void AddTask(CTask task)
    {
        var treeEntry = new TreeNode();

* method names AreLikeThis
* method argument names areLikeThis
* local variable names areLikeThatToo
* (private class level members _areLikeThis, public members AreLikeThis, so you don't need to prefix local vars with l_ as you can tell by the camelCase that they're not class level)
* don't need to put TYPE name = new TYPE; it's obvious from "new TreeNode" that the thing is a tree node, so var'ing it is less repetitive
 
As to your actual problem; can you post a reproductive example? Or take a working form and a broken one, and park them down to the absolute minimum necessary to still replicate the problem then show us the diff ?
 
I appreciate the style points. I have 30+ years of C++/MFC experience, but am just crawling out of the cave in C#. At my age, old habits die very hard, but I will work to embrace C# standards. However, as the sole developer (anyone need a job?), style concerns me less than quick prototyping. Code cited below maintains my personal style for the time being.

Here is a partial view of the program state after several calls to add_task(). The EDITOR tree does not show its nodes, while the TASK LIBRARY tree is completely and properly updated:

EDITOR and TASK LIBRARY.png


As shown, the TASK LIBRARY can be up to three levels deep, never deeper. Here is the function method for adding the root-level folders to the TASK LIBRARY. If the folder already exists it is not added. (Comments on forming the code better in C# are greatly appreciated.) Note that this does differ from add_task() in that I am adding the nodes as text and then updating the image index. In add_task(), [shown in the first post] I add the node as a TreeNode object. I did investigate adding the node as text in add_task() and then updating the image index, but this did not change the behavior. I will simplify the code shown here a bit by using TreeNode objects.

C#:
        public bool add_folder( String Folder )
        {

            if( Folder == "" )
                return ( false );

            //  If the folder is already specified, return;
            for( int i = 0; i < taskLibraryTree.Nodes.Count; i++ )
                if( taskLibraryTree.Nodes[ i ].Text == Folder )
                    return ( false );

            int l_iFolderIndex = 0;

            taskLibraryTree.Nodes.Add( Folder );

            //  Get the tree index of the just-added folder
            for( int i = 0; i < taskLibraryTree.Nodes.Count; i++ )
                if( taskLibraryTree.Nodes[ i ].Text == Folder )
                {

                    l_iFolderIndex = i;

                    break;

                }  //  if( taskLibraryTree.Nodes[ i ].Text == Folder )

            //  set the folder icon
            taskLibraryTree.Nodes[ l_iFolderIndex ].ImageIndex =
                taskLibraryTree.Nodes[ l_iFolderIndex ].SelectedImageIndex = ( int ) LIBRARY_ICON_INDEX.STANDARD_LIBRARY_FOLDER;

            return ( true );

        }  //  public bool add_folder( String Folder )

The functions for add_subfolder() and add_subsubfolder() are similar, as is the code to add the Tasks.

Thanks for your attention to this.

FC
 
Any way you can strip out (a copy of) the project to just those two tree views and the supporting code plus necessaries to make it run?(program.cs) Also remove the bin and obj folders and post the resulting project as a zip file? We'll be able to load it on our machines to reproduce it then
 
That will be involved, but, I think, doable. It will take some time, though. Today is my Friday, so I will post next Monday or Tuesday if I can. Otherwise I'll let you know.

FC
 
You've just transitioned to page 2 of the thread, maybe thats why you thought the post went missing. I can try and replicate from that posted..


Oh, btw I just noticed you said youre using .NET 6; there be dragons on .NET core derivatives for winforms - does your problem exhibit on framework 4.8?
 
I have managed to strip the project down to about 32 MB zipped. It went quicker than I thought it would. I could probably do some more-painstaking surgery to reduce the size by a few more MB. The 32 MB file is too big for the server. Here's a link:

Vision6.zip

A critic would have a field day with this project. Just building, there are all kinds of warnings that still need to be handled. That, along with the many style issues that you have pointed out. As I mentioned, the purpose here is to prototype as quickly as possible. There are major problems that need to be solved and it's painful to be stuck on this trivial matter. But I am learning by doing.

To operate, run the project. Open the "Program Control" Tab in the TASK LIBRARY. Right-click on the Pause Task and select "To EDITOR". A Task configuration dialog will appear. Clicking OK should put the Task, by user-specified name and icon, into the EDITOR window. Repeating the operation should repeatedly add the Task to the EDITOR.


TASK LIBRARY-to-EDITOR.png



I installed the .NET framework 4.8 SDK, as you suggested, but I can't seem to retarget the project. Only the targets in the image are available. Along with C#, I'm trying to learn what .NET's all about. They are not exactly one-in-the-same. Maybe you meant that I should do something else.(?)

.NET Targets.png



Hey, I really appreciate your taking the time to help me with this. I'm looking forward to the day when I can start to pay it forward.

FC
 
The reason why your .zip file is so big is because you included all the .vs folders. Those folders include temporary per user files that are not needed when sharing code with someone else.
 
Good to know. Try the attached.

It is the end of the week for me. Will look back on Monday.

FC
 

Attachments

  • IVision6 (2).zip
    396.1 KB · Views: 18

Latest posts

Back
Top Bottom