I use a TreeView to display some informations in two levels :
- A
- B
- 1
- D
- 1
- 2
- ...
Sometimes, the informations stored in the treeview differs with the one displayed. It seems that it's because Paint()
is not called after Invalidates()
.
I already tried answer from this question : C# Treeview doesn't refresh after moving nodes, without success.
Tree (re)Creation code :
using System.Windows.Forms.TreeNode;
using System.Windows.Forms.TreeView;
[...]
private void createTree()
{
[...]// Creation code
// Check update of the treeview
foreach (TreeNode n in viewDataTreeView.Nodes)
{
Console.WriteLine(n.Name);
foreach (TreeNode child in n.Nodes)
{
Console.WriteLine(" " + child.Name);
}
}
Console.WriteLine("done");
this.Invalidate(true);
}
Which always output the correct tree that I have in the treeview. And sometimes, newly added node are not displayed on the screen.
Working case:
callstack:
Functions of working callstack :
private void toolStripDeleteTemplateButton_Click(object sender, EventArgs e)
{
//Some confirmation stuff
[...]
// Delete the template file
GraphTemplateNode node = this.viewDataTreeView.SelectedNode as GraphTemplateNode;
File.Delete(node.GetTemplateFilePath());
createTree();
}
Not working case:
callstack:
See the Test 4
is missing.
Functions of unworking callstack :
//LineGraphUIControl.cs
private void saveTemplateToolStripButton_Click(object sender, EventArgs e)
{
base.SaveGraphTemplate(lineGraphControl1.Graph);
}
//GraphUIControl.cs
public void SaveGraphTemplate(Graph graph)
{
//Getting file name
[...]
//Creating template
ViewDataSubControl.AddNewUserTemplate(tmplt);
}
// ViewDataSubControl.cs
public void AddNewUserTemplate(GraphTemplate tmplt)
{
//Some string calculations
[...]
tmplt.SaveTemplate(fullName);
createTree();
}
I tried to use the method Refresh()
, Update()
and BeginUpdate() & EndUpdate()
with no luck. The event Invalidated
is always fired, but I can't get Paint()
to be called everytime. If I force call with InvokePaint() the TreeView is not updated either.
What can I do to make it works ?