How do I capture the paint event on a treeview?

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
 
B

Benny Raymond

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!
*/
}
 

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