Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# - Code in form constructor not executed

Tags:

c#

winforms

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!

like image 597
Conduit Avatar asked Oct 09 '12 20:10

Conduit


1 Answers

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.

like image 110
xxbbcc Avatar answered Sep 27 '22 22:09

xxbbcc