Hi Peter,
I performed a test based on your sample code and did see the problem. The
TreeView control is flickering a little when a tree node within it is
expanded using Expand or Toggle method.
As you have mentioned, if the tree node is expanded via plus/minus button,
the tree node does not flicker. I think TreeView achieve this by preventing
it from redrawing before the update and then restoring the redrawing after
the update.
In fact, TreeView has two methods called 'BeginUpdate' and 'EndUpdate'.
BeginUdpate method disables any redrawing of the tree view and EndUpdate
method enables the redrawing of the tree view. You could call the
'BeginUpdate' method before you call the 'Expand' or 'Toggle' method to
expand/collapse the tree node and call the 'EndUpdate' method after you
finish the update.
Your workaround of catching WM_ERASEBKGND in WndProc is another solution of
disabling the redrawing. But I think there would be a little problem. After
the tree view finishes update, it needs redrawing. However, in this case
the redrawing is 'disabled'.
Hope this helps.
Please try my suggestion and let me know the result.
Sincerely,
Linda Liu
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.
Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
Peter Larsen [] - 08 Dec 2006 12:20 GMT
Hi Linda,
Thanks for your help.
BeginUpdate/EndUpdate is a way, but its still not perfect.
I have found a way out to solve my problem - please see the following sample
which illustrate the basics of the solution.
Create a TreeView control:
public partial class TreeViewExSp : TreeView
{
}
and add the code plus 2+ root nodes and 10-20 childs to the root nodes.
protected override void OnDrawNode(DrawTreeNodeEventArgs e)
{
e.DrawDefault = true;
}
private void tv_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
{
//this.BeginUpdate();
if (e.Node.Level == 0)
{
if (e.Node.IsExpanded)
e.Node.Collapse(true);
else
e.Node.Expand();
}
//this.EndUpdate();
}
private enum winconstants
{
WM_ERASEBKGND = 0x0014
}
protected override void WndProc(ref Message m)
{
switch (m.Msg)
{
case (int)winconstants.WM_ERASEBKGND:
m.Result = (IntPtr)0; //not handled
//m.Result = (IntPtr)1; //handled
break;
default:
base.WndProc(ref m);
break;
}
}
If you want the entire project then please let me know.
BR
Peter
Linda Liu [MSFT] - 11 Dec 2006 09:47 GMT
Hi Peter,
Thank you for your reply and sample code.
I have performed a test based on your sample code and did see it prevents
the treeview control from flickering perfectly : )
I think your solution will benefit all of the readers in the newsgroup!
If you have any other questions in the future, please don't hesitate to
contact us.
Have a nice day!
Sincerely,
Linda Liu
Microsoft Online Community Support