Why it is not painting classes that are directly inheriting from Control class.

U

Umut Tezduyar

I want to make custom control. The problem is, i want to inherit from
System.Windows.Forms.Control class not System.Windows.Forms.CustomControl.
Although i have overriden the method OnPaint in my class and draw something
inside it, it doesnt draw in the main form that i add my custom control.

public class MyCustomControl : System.Windows.Forms.Control
{
protected override void OnPaint (PaintEventArgs e)
{
base.OnPaint (e);
e.Graphics.DrawRectangle (new
System.Drawing.Pen(System.Drawing.Color.Red,2),0,0,this.Width,this.Height);
}
}

It doesnt enter the OnPaint method. But if i change the base class to
UserControl, it draws my custom control. Can anyone explain this to me,
please..
 
K

Ken Tucker [MVP]

Hi,

If you are inherting from some controls like the listbox you have to
set its ownerdrawn property to one of the ownerdrawn ones for it to run your
onpaint method. The other option is to add the userpaint style to your
control.

Ken
 

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