2

I am trying to draw a cross inside of a Windows Forms App.

I can do it using two lines. I have tested it using a Button. Now I do not know how I can draw it automatically when opening the application without pressing a button.

thanks for ur help :D

Andrey Korneyev
  • 26,353
  • 15
  • 70
  • 71
darude_cod3r
  • 91
  • 3
  • 11
  • 2
    [Form.Load Event](https://msdn.microsoft.com/en-us/library/system.windows.forms.form.load%28v=vs.110%29.aspx) – Der Kommissar Jun 01 '15 at 14:30
  • @EBrown that doesn't work... – darude_cod3r Jun 01 '15 at 14:33
  • 1
    I recommend against `Form.Load` because anything drawn there will be overwritten in later `Paint` events. I recommend adding a `Paint` event handler and doing your drawing there using the provided Graphics object from the `PaintEventArgs`. See more here: http://stackoverflow.com/questions/30419493/drawing-glitches-when-using-creategraphics-rather-than-paint-event-handler-for-c – adv12 Jun 01 '15 at 14:34

2 Answers2

6

Good practice is override your form OnPaint method and do all paintings you need there (using Graphics object from e.Graphics):

protected override void OnPaint(PaintEventArgs e)
{
    base.OnPaint(e);

    //your paintings here
}

Otherwise, if you will place your painting code somewhere else (Load, Shown and so on) - your paintings will not be done automatically when your form will be redrawn by system.

Andrey Korneyev
  • 26,353
  • 15
  • 70
  • 71
-2

Just invoke the method inside a InitializeComponent()

Every form has its own constructor you can see it in the project tree, {formname}.Designer.cs press F7

and there you just see the constructor

so just invoke it there

 private void InitializeComponent()
    {
        this.components = new System.ComponentModel.Container();
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.Text = "Form1";
    }
Dan Kuida
  • 1,017
  • 10
  • 18
  • I don't think the form will necessarily have a window handle by the end of the constructor, and you need a window handle to do any drawing. Also, `InitializeComponent` is not the constructor and it is "owned" by the designer, which means any changes you make to the code in `InitializeComponent` will be overwritten when you make changes in the designer. – adv12 Jun 01 '15 at 14:39
  • Even so, any drawing done here will be erased the next time the form is refreshed or another window is dragged across it. – Chris Dunaway Jun 01 '15 at 18:28