Override the OnPaint method (protected override OnPaint(... )
Then put your drawing code in there, the OnPaint method has a paint event args that includes a reference to the graphics object, so you don't need to create it separately.
public class Form1 : Form
{
public Form1
{
InitializeComponent();
}
protected override void OnPaint(PaintEventArgs e)
{
Graphics g = e.Graphics;
g.Clear(Color.Black);
g.FillRectangle(Brushes.Red, 0, 0, 20, 50);
}
}
Form.OnPaint MSDN[
^]
BTW, you are clearing the graphics with "Black" then painting a black rectangle, so you would never see it. I changed the color to red and you will see the background now black with a red rectangle on it.