5 Replies - 435 Views - Last Post: 28 December 2012 - 02:44 PM Rate Topic: -----

#1 migcak  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 45
  • Joined: 15-August 12

Simple question about where to put code

Posted 28 December 2012 - 02:18 PM

Im new to C# and I can't find where to put my code that draw lines on the form immediately when I debug my program. Currently, I have to click a button to draw lines but I thought if I put my code under InitializeComponent() it would draw the lines without me clicking the button. I believe I did this before correctly but I forgot so that's why i'm asking.
Is This A Good Question/Topic? 0
  • +

Replies To: Simple question about where to put code

#2 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3515
  • View blog
  • Posts: 11,999
  • Joined: 12-December 12

Re: Simple question about where to put code

Posted 28 December 2012 - 02:26 PM

If you double-click an empty area of the form it will create an empty Load event procedure for you.

BTW We are advised NOT to modify the InitializeComponent method.

This post has been edited by andrewsw: 28 December 2012 - 02:28 PM

Was This Post Helpful? 1
  • +
  • -

#3 LarcaCode  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 16
  • View blog
  • Posts: 21
  • Joined: 30-July 12

Re: Simple question about where to put code

Posted 28 December 2012 - 02:29 PM

Maybe you are looking for this?!
You could use the OnPaint-Event of the form.

Cheers,
Larca
Was This Post Helpful? 1
  • +
  • -

#4 migcak  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 45
  • Joined: 15-August 12

Re: Simple question about where to put code

Posted 28 December 2012 - 02:32 PM

I tried that but here the code
 Graphics g = this.CreateGraphics();
       Pen pen = new Pen(Color.Black, 10);
       g.DrawLine(pen, new Point(100, 0), new Point(100, 300));
       g.DrawLine(pen, new Point(200, 0), new Point(200, 300));
       g.DrawLine(pen, new Point(0, 100), new Point(300, 100));
       g.DrawLine(pen, new Point(0, 200), new Point(300, 200));  


Is it something I'm forgetting then?
Was This Post Helpful? 0
  • +
  • -

#5 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3515
  • View blog
  • Posts: 11,999
  • Joined: 12-December 12

Re: Simple question about where to put code

Posted 28 December 2012 - 02:41 PM

There is a Shown event which does work :)/>. You can create it by clicking the form in Design View, Properties, Events (Window on the right) and double-clicking Shown - you probably know this :)/>

Paint may run more than once..

This post has been edited by andrewsw: 28 December 2012 - 02:43 PM

Was This Post Helpful? 0
  • +
  • -

#6 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5535
  • View blog
  • Posts: 11,857
  • Joined: 02-June 10

Re: Simple question about where to put code

Posted 28 December 2012 - 02:44 PM

Please re-read the page that LarcaCode linked for you.

Specifically how the OnPaint method was overridden

protected override void OnPaint(PaintEventArgs e)
{
   // If there is an image and it has a location,  
   // paint it when the Form is repainted. 
   base.OnPaint(e);
   if(this.picture != null && this.pictureLocation != Point.Empty)
   {
      e.Graphics.DrawImage(this.picture, this.pictureLocation);
   }
}


Notice that the .Graphics object comes from the PaintEventArgs e parameter - not from making a new one through this.CreateGraphics() as is shown in your snippet.

From there you just paint your lines as you have in your snippet.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1