Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm Controls / December 2006

Tip: Looking for answers? Try searching our database.

TreeView

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Peter Larsen [] - 04 Dec 2006 11:26 GMT
Hi,

I am using a TreeView control and have found a flickering problem that occur
in some situations.

If the node is expanded using Expand() the node expanding will flicker.
If the node is expanded using Toggle() the entire control will flicker.
If the node is expanding using the pus-button, the node does NOT flicker.

I'm using the following code:

       private void treeView1_NodeMouseClick(object sender,
TreeNodeMouseClickEventArgs e)
       {
           if (e.Node.Level == 0)
           {
               if (e.Node.IsExpanded)
                   e.Node.Collapse(true);
               else
                   e.Node.Expand();
           }
       }

This cause the expanding node to flicker.

The following code will cause the entire control to flicker:

       private void treeView1_NodeMouseClick(object sender,
TreeNodeMouseClickEventArgs e)
       {
           if (e.Node.Level == 0)
           {
               e.Node.Toggle();
           }
       }

Is there a way to prevent the control from flicker ??
I have tried different set of styles ( SetStyle(ControlStyles.UserPaint |
ControlStyles.AllPaintingInWmPaint, true); ) etc.

BR
Peter
Peter Larsen [] - 04 Dec 2006 12:58 GMT
Hi again,

I have found that if i catch WM_ERASEBKGND in WndProc, the TreeView won't
flicker as much as before.
Why is that ??

/Peter
Linda Liu [MSFT] - 05 Dec 2006 03:11 GMT
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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.