TreeView last item hidden - .NET bug?

  • Thread starter Tsviatko Yovtchev
  • Start date
T

Tsviatko Yovtchev

I am filling a treeview control from a background thread using Invoke
and AddRange in the delegate supplied as an argument to Invoke.

Now, the last of the TreeNodes added never shows on the TreeView. What
shows is the rootline that connects it to the rest of the tree. When I
click at the end of this rootline, i.e. where the node should be, then
the node gets displayed and it stays displayed further on.

Is it possible that this is a TreeeView bug?
 
D

Daniel Moth

Anything is possible. Can you post a complete small repro (not just steps,
but an actual project) so I can try it here and let you know.

Cheers
Daniel
 
D

Daniel Moth

If you get rid of the Begin/EndInvoke calls it will work as expected.

Also give your treenode a name...

Cheers
Daniel
 
T

Tsviatko Yovtchev

All right....here is a repro:

using System;
using System.Drawing;
using System.Collections;
using System.Windows.Forms;
using System.Data;
using System.Threading;

namespace Proben
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.TreeView treeView1;
private System.Windows.Forms.MainMenu mainMenu1;

private TreeNode node;
private EventHandler add, clear;

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//

add = new EventHandler(this.AddNode);
clear = new EventHandler(this.ClearColl);
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.mainMenu1 = new System.Windows.Forms.MainMenu();
this.treeView1 = new System.Windows.Forms.TreeView();
//
// treeView1
//
this.treeView1.ImageIndex = -1;
this.treeView1.Location = new System.Drawing.Point(8, 16);
this.treeView1.SelectedImageIndex = -1;
this.treeView1.Size = new System.Drawing.Size(224, 208);
//
// Form1
//
this.Controls.Add(this.treeView1);
this.Menu = this.mainMenu1;
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);

}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>

static void Main()
{
Application.Run(new Form1());
}

private void Form1_Load(object sender, System.EventArgs e)
{
Thread t = new Thread(new ThreadStart(this.Start));
t.Start();
}

private void AddNode(object sender, EventArgs e)
{
treeView1.Nodes.Add(node);
}

private void ClearColl(object sender, EventArgs e)
{
treeView1.BeginUpdate();
treeView1.SelectedNode = null;//bugfix for .net cf v1
treeView1.Nodes.Clear();
treeView1.EndUpdate();
}

void Start()
{
treeView1.Invoke(this.clear);
node = new TreeNode("");
TreeNode child = new TreeNode();
node.Nodes.Add(child);
treeView1.Invoke(add);
}
}
}

Actually, I found out that the behavior I described is caused by calling
the Nodes.Clear() method on an empty three before adding something to
the nodes collection. When I comment out the

treeView1.Invoke(this.clear);

line everything works fine.


Let me know how it works for you.

Best regards,
Tsviatko

Daniel Moth ÐÉÛÅÔ:
 
T

Tsviatko Yovtchev

Daniel Moth ÐÉÛÅÔ:
...I meant of course the Begin/EndUpdate (in ClearColl)

Cheers
Daniel

Well....why wouldn't it work if I keep the calls? Originally, I put them
there to improve the execution time(I have like couple of thousands of
nodes), and I think I was successful in that.


Is not giving a name supposed to be a problem? This is just a test app
so that's why I kept it empty. However, I guess it should be OK this
way, too.
 
T

Tsviatko Yovtchev

It looks like you got the source of the problem. It is the
Begin/EndInvoke calls that cause it.

So, is this a bug in the .NET CF? I ran the code on CF 1.0 SP3 and the
problem persisted.
 
D

Daniel Moth

You mean XxxxUpdate (and not Invoke).

Yes it seems that it occurs with CF v2.0 as well. While I don't have time to
research this now, I would guess those methods don't work in a
control.Invoked function

Cheers
Daniel
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top