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.
Simple question about where to put code
Page 1 of 15 Replies - 221 Views - Last Post: 28 December 2012 - 02:44 PM
Replies To: Simple question about where to put code
#2
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.
BTW We are advised NOT to modify the InitializeComponent method.
This post has been edited by andrewsw: 28 December 2012 - 02:28 PM
#3
Re: Simple question about where to put code
Posted 28 December 2012 - 02:29 PM
#4
Re: Simple question about where to put code
Posted 28 December 2012 - 02:32 PM
I tried that but here the code
Is it something I'm forgetting then?
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?
#5
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..
Paint may run more than once..
This post has been edited by andrewsw: 28 December 2012 - 02:43 PM
#6
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
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.
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.
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote





|