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

  • Thread starter Thread starter Umut Tezduyar
  • Start date Start date
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..
 
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
 
Back
Top