C # - code in form constructor not executed

Relatively new to C #; I hope I just miss something simple.

I have a form called "Exercise1" that contains a window with the drawing "drawingArea" and a few buttons. Exercise1 constructor code 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 other form methods. Both "paper" and "balloon" work as initialized in the constructor in other methods defined in the form.

For some reason teams

 paper.Clear(Color.White); 

and

 balloon.Display(paper); 

To clear the image window and show a red ellipse, do not perform (at least, apparently). What gives?

UPDATE: I think I will like this site ... You guys are fast! @Nitesh: The constructor for Exercise1 is called from another form. The 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: Played with this a bit, but no luck. Which command fires the Form_Load event for Exercise1?

UPDATE: This modified 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 the help!

+6
source share
3 answers

You cannot draw in the constructor. To make the correct drawing, you need to have the form shown on the screen. You can try to use the Shown event to render (this may get lost when redrawing the form).

Usually the best way is to set all the necessary flags in the constructor, and then use the Paint event of the form to make the whole picture. Later, when you need to repaint something, adjust any state you want to display, invalidate your form (this leads to the Paint event), and then you can redraw the new state.

If you try to make an individual drawing (outside of your Paint ), you risk random paths or the drawing may disappear when you resize / minimize the shape.

+5
source

You use Graphics in the designer, this means that you draw on paper only once, any redrawing for any reason that occurs after the designer draws the drawing in its original form. Try adding PaintEventHandler to drawingArea, then call inside the 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); } 
+1
source

You must override the OnPaint event handler form. In this case, you can get a graphic context that will redraw your paper and balloons.

0
source

Source: https://habr.com/ru/post/927291/


All Articles