java to c# how to do custom painting in a panel
Question
in java when you want to do custom painting in a panel, you usually override the paint() function.
now i am looking for the corresponding function to override in a C# panel.
also i would be thankful for a short samplecode to do some painting. like draw a circle or something.
thanks a lot!
edit: ok thanks for your answers! i have an additional question on this: what is the preferred method to manually trigger a repaint? for example i want my red circle to be green suddenly. how do i make the call to OnPaint()?
thanks!
Solution
You override the OnPaint method. Here's a quick example of drawing a circle in C#:
protected override void OnPaint(PaintEventArgs pe)
{
Graphics gfx = pe.Graphics;
using (Pen pen = new Pen(Color.Blue))
{
gfx.DrawEllipse(pen, 10,10,10,10);
}
}
OTHER TIPS
Override the OnPaint
method.
There's a simple example here, and searching for OnPaint tutorial C#
gets lots of hits.