How do I capture the paint event on a treeview?

  • Thread starter Thread starter Benny Raymond
  • Start date Start date
B

Benny Raymond

I'm trying to adjust the length of the strings being drawn, as well as
some effects depending on what item is actually being drawn. I tried
doing the following, but the paint event never runs:


#region TView
public class tView : TreeView
{
#region Constructor
public tView()
{
Paint += new PaintEventHandler(tView_Paint);
/*
also tried:
this.Paint += ...
base.Paint += ...
*/
}

private void tView_Paint(object sender,
System.Windows.Forms.PaintEventArgs e)
{
/*
the following line has a break on it,
but the program never breaks.. so this
isn't running?
*/
Debug.WriteLine("blah");
}
#endregion
 
nevermind... Turns out that windows handles the drawing of this control
- so in order to change how it's drawn, you have to draw it yourself by
setting userpaint to true in the constructor, and then using OnPaint and
draw everything yourself:

in constructor:
SetStyle(ControlStyles.UserPaint, true);

then use this:
protected override void OnPaint(PaintEventArgs pe)
{
/*
place all of your custom paint code here... note, you have to draw
everything yourself!
*/
}
 
Back
Top