Create a control

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have make a class derived from the System.Windows.Forms.Control class

I have write this method,

protected override void OnPaint(PaintEventArgs e)
{
Pen myPen = new Pen(Color.Black, 1);
e.Graphics.DrawEllipse(myPen, 10,10, 8,8);
SolidBrush Colore=new SolidBrush(Color.Red);
e.Graphics.FillEllipse(Colore,10,10,8,8);
}

because I wont that if I add this control to my form, it must draw a circle.
So in my form I write
MyControl myobj=new MyControl ();
Form.Controls.Add(MyControl );

The problem is that the onPaint event is never called, so I don't see tje
circle.

Someone can help me?

Thank's
Boni
 
* "=?Utf-8?B?Qm9uaQ==?= said:
I have make a class derived from the System.Windows.Forms.Control class

I have write this method,

protected override void OnPaint(PaintEventArgs e)
{
Pen myPen = new Pen(Color.Black, 1);
e.Graphics.DrawEllipse(myPen, 10,10, 8,8);
SolidBrush Colore=new SolidBrush(Color.Red);
e.Graphics.FillEllipse(Colore,10,10,8,8);

\\\
MyPen.Dispose();
Colore.Dispose();
///

Add this to your control's constructor:

\\\
this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
this.SetStyle(ControlStyles.UserPaint, true);
this.SetStyle(ControlStyles.DoubleBuffer, true);
this.UpdateStyles();
///
 
Herfried,

I don't see how can this help the control to call it's OnPaint method.
 
Hi Boni,

Check if the control is not obscured by any other control. Check also that
the control has some width and height and/or you don't draw outside the
control boundaries. You can use Spy++ tool to make sure that the control is
there and to check its size and position.
 
Back
Top