user3178317
user3178317

Reputation: 11

Paint event sequence

I run the program from Form3. Why does the program execute Picture1_Paint of Form3 first instead Picture1_Paint of Form1, how to change the sequence?

private void Form3_Load(object sender, EventArgs e)
{
    ss = new Form1();
    ss.Show();  // Here Form1 is loaded, but its Picture1_paint is not executed

    FootingWidth.Text = ss.richTextBox9.Text; 

    pictureBox1.Paint += new System.Windows.Forms.PaintEventHandler(this.pictureBox1_Paint);

    pictureBox1.Refresh();               
}

private void Form1_Load_1(object sender, EventArgs e)
{
    richTextBox1.Text = 1.0.ToString();
    richTextBox2.Text = 0.403.ToString();
    richTextBox3.Text = 0.0.ToString();

    pictureBox1.Paint += new System.Windows.Forms.PaintEventHandler(this.pictureBox1_Paint);

    pictureBox1.Refresh();
}

Upvotes: 1

Views: 428

Answers (1)

Hans Passant
Hans Passant

Reputation: 941635

The Form.Show() call only starts the process of making the window visible. You cannot actually see it until your method completes and execution re-enters the message loop. After which the Paint event is delivered if nothing else of importance needs to be done. Painting order is back-to-front.

You forced the pictureBox1's Paint event to run by calling Refresh(). That isn't necessary, it will get the Paint event automatically. But the code will certainly always get the pb's Paint event to run before any other. Just remove the Refresh() call.

If there's any reason to force a paint to occur then use the Invalidate() method to tell Windows that the window ought to be repainted. You can use the Update() method to force the Paint event to be delivered immediately.

Upvotes: 2

Related Questions