Create a control

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
 
H

Herfried K. Wagner [MVP]

* "=?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();
///
 
S

Stoitcho Goutsev \(100\) [C# MVP]

Herfried,

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

Stoitcho Goutsev \(100\) [C# MVP]

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.
 

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