Conduit
Conduit

Reputation: 2735

C# - Code in form constructor not executed

Relatively new to C#; hopefully I'm just overlooking something simple.

I have a form named 'Exercise1' which contains a picture box called 'drawingArea' and a few buttons. The code for the constructor of Exercise1 is as follows:

public Exercise1()
{
    InitializeComponent();
    paper = drawingArea.CreateGraphics();
    balloon = new Balloon("redBalloon", Color.Red, drawingArea.Width / 2, 
        drawingArea.Height / 2, 30);
    paper.Clear(Color.White);
    balloon.Display(paper);   
}
...

'paper' and 'balloon' are created as globals above the constructor for use in the other methods on the form. Both 'paper' and 'balloon' work as initialized in the constructor in the other methods defined on the form.

For whatever reason, the commands

paper.Clear(Color.White);

and

balloon.Display(paper);

Which should clear the picture box and show a red ellipse, don't execute (at least visibly). What gives?

UPDATE: Think I'm going to like this website... You guys are quick!
@Nitesh: The constructor for Exercise1 is called from another form. Code is as follows:

private void button1_Click(object sender, EventArgs e)
        {
            int exSelector = (int)numericUpDown1.Value;
            switch (exSelector)
            {
                case 1:
                    Exercise1 form1 = new Exercise1();
                    form1.Show();
                    break;
...

@Sean Dunford: Yes and yes it is.
@RBarryYoung: Was playing around with that a bit, but had no luck. What command triggers a Form_Load event for Exercise1?

UPDATE: This altered code works as expected:

public Exercise1()
        {
            InitializeComponent();
            paper = drawingArea.CreateGraphics();
            drawingArea.BackColor = Color.White;
            drawingArea.Paint += new PaintEventHandler(this.drawingArea_Paint);
            balloon = new Balloon("redBalloon", Color.Red, drawingArea.Width / 2, drawingArea.Height / 2, 30); 
        }
        private void drawingArea_Paint(object sender, PaintEventArgs e)
        {
            e.Graphics.Clear(Color.White);
            balloon.Display(e.Graphics);
        } 
...

Thanks for all the help!

Upvotes: 6

Views: 1972

Answers (3)

xxbbcc
xxbbcc

Reputation: 17327

You cannot do drawing in the constructor. To do proper drawing, you need to have the form shown on the screen. You can try using the Shown event to do your rendering (this may get lost when the form is redrawn, though).

Usually the best way is to set whatever flags you need in the constructor and then use the Paint event of the form to do all painting. Later on, when you need to repaint something, set up whatever state needs to be rendered, invalidate your form (this results in a Paint event) and then you can repaint the new state.

If you try to do customized drawing (outside your Paint event) you'll run the risk of things randomly going blank or your drawing may disapper when you resize/minimize your form.

Upvotes: 5

Nikola Davidovic
Nikola Davidovic

Reputation: 8656

You use Graphics in a constructor, that means that you draw on the paper only once, any redraw for whatever reason that happens after constructor will draw the drawingArea in its original way. Try to add PaintEventHandler to drawingArea and then call inside balloon.Display(e.Graphics);

    public Exercise1()
    {
        InitializeComponent();

        balloon = new Balloon("redBalloon", Color.Red, drawingArea.Width / 2, 
        drawingArea.Height / 2, 30);


        drawingArea.Paint += new PaintEventHandler(drawingArea_Paint);
    }

    void drawingArea_Paint(object sender, PaintEventArgs e)
    {
        e.Graphics.Clear(Color.White);
        baloon.Display(e.Graphics);
    }

Upvotes: 1

IAbstract
IAbstract

Reputation: 19881

You should be overriding the forms OnPaint event handler. In doing so, you are able to get the graphics context which will redraw your paper and balloon areas.

Upvotes: 0

Related Questions