Click here to Skip to main content
15,887,812 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
This is probably one of the most stupid questions ever asked. Okay, so I have a form:
C#
using System;
using System.Drawing;
using System.Windows.Forms;

namespace Whatever 
{
    public class Form1 : Form
    {
        public Form1
        {
            InitializeComponent();
            Graphics g = CreateGraphics();
            g.Clear(Color.Black);
            g.FillRectangle(Brushes.Tan, 0, 0, 20, 50);
        }
    }
}

No matter what I do nothing will be drawn on the screen

As I said this is probably a stupid question but hey it will give you some extra points.
Posted
Updated 1-Jul-13 14:58pm
v2

1 solution

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.

C#
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.
 
Share this answer
 
v5
Comments
Curtdawg99 1-Jul-13 18:40pm    
can u go in2 more detail
Ron Beyer 1-Jul-13 18:45pm    
I updated the solution.
Sergey Alexandrovich Kryukov 1-Jul-13 19:30pm    
Of course, a 5.
—SA
Ron Beyer 1-Jul-13 19:39pm    
Thanks Sergey.
Curtdawg99 1-Jul-13 20:58pm    
Thank you so much!!!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900