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
 
In EDITOR.Designer.cs there is a add_task method that adds the treenode, before the return; call add Show();
 
I installed the .NET framework 4.8 SDK, as you suggested, but I can't seem to retarget the project.
It's a .NET core/5/6/7 flavored project, not a framework one. I'd recommend to put a new project (and make sure you choose "WinForms (.NET Framework)") if you want to go that route

And don't edit the *.Designer.cs file; they're programmatically generated by the designer - leave them alone. Put your own code in the normal YourFormName.cs file - the compiler merges together any classes declared as `partial` when compiling, so you can leave all the code in *.Designer,cs files as is

C#:
//in MyForm.Designer.cs

namespace Whatever
{
    partial class MyForm                                                                       //note: PARTIAL
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify                                                        //no kidding!
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.components = ....

And this is the one you edit:

C#:
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net.WebSockets;
using System.Threading.Tasks;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using Websocket.Client;
using Flurl.Http;

namespace Whatever
{
    public partial class MyForm: Form                                                                      //PARTIAL
       
        private Blah _myPrivvateClassLevelVariable;

        public MyForm()
        {
            InitializeComponent();                                                   //LEAVE ALONE

            //my code starts here
            this.Text = "Hello world";

--

I couldn't easily run your project as packaged; too many dependent projects (IVarServer etc..) are all missing, and it complains of duplicate named outputs. I note there doesn't seem to be any real organization - multiple duplicate image resources dumped all over the place in duplicate subfolders. The entire thing is quite hard to follow


If John's advice doesn't help, can you really strip it back to just a single form, with two treeview, one that works and one that doesn't? Get rid of all the icons, embedded resources, everything extraneous to give a better chance that it'll Just Work to demo the problem on someone else's machine
 
Last edited:
For example, here's a minimal code to bring up a form with two tree views in it. Just need no create a new WinForms project, and replace the Program.cs with this code.
Program.cs:
namespace TestWinForms
{
    internal class Program : Form
    {
        Program()
        {
            var tvLeft = new TreeView() { Dock = DockStyle.Fill };
            var tvRight = new TreeView() { Dock = DockStyle.Fill };

            var sc = new SplitContainer() { Dock = DockStyle.Fill };
            sc.Panel1.Controls.Add(tvLeft);
            sc.Panel2.Controls.Add(tvRight);
            Shown += (o, e) => { sc.SplitterDistance = sc.Width / 2; };

            var btnDoIt = new Button()
            {
                Dock = DockStyle.Bottom,
                Text = "Do It!"
            };
            btnDoIt.Click += (o, e) =>
            {
                var left = new TreeNode() { Text = $"Left: {DateTime.Now}" };
                tvLeft.Nodes.Add(left);

                var right = new TreeNode() { Text = $"Right: {DateTime.Now}" };
                tvRight.Nodes.Add(right);
            };

            Size = new Size(800, 600);
            Controls.Add(btnDoIt);
            Controls.Add(sc);
        }

        [STAThread]
        static void Main()
        {
            ApplicationConfiguration.Initialize();
            Application.Run(new Program());
        }
    }
}

1697226479520.png
 
A call to the Show() method on the TreeView object didn't show the entries.

The simple project that you provided code for works as expected. I'm not sure how to specifically create a TreeView that doesn't work. I would need to know what is causing it not to work.

Part of the organizational confusion is that an early go at this had two classes for each form (CEDITORWindow and CEDITOR, for example). I don't recall the reasoning behind this, but the needless complexity was maintained until today. I have stripped the intermediate class between the parent project window and the forms. The program continues to behave exactly as it did, including the failure to display the tree nodes.

I appreciate all of the time, thought and effort that members have put into this, but I need to table this project for a while. Maintenance, documentation, customer support and training on the existing program will be taking my focus for a while.

Thanks to all.

FC
 
Sorry and thanks... That worked... sorta. At least it gives me a clue as to how to proceed to find the problem. Including Show() opens a second EDITOR window that is not coupled with the main program window. (In fact, it was hidden behind the program window.) This leads to there being at least two instances of the CEDITOR class. That should be easy enough to track down.

Thanks again. I'm going to mark this "resolved".

FC
 
Back
Top Bottom