Sign in to follow this  
Alpha Brain

Drawing with C#

Recommended Posts

I'm novice with Sharp Develop and C#. I'm trying to follow this tutorial. http://www.geekpedia.com/tutorial50_Drawing-with-Csharp.html The guy tell to override OnPaint with the following code; protected override void OnPaint(PaintEventArgs paintEvnt) { // Get the graphics object Graphics gfx = paintEvnt.Graphics; // Create a new pen that we shall use for drawing the line Pen myPen = new Pen(Color.Black); // Loop and create a new line 10 pixels below the last one for(int i = 20; i < 250; i = i + 10) { gfx.DrawLine(myPen, 20, i, 270, i); } } When I start a Windows Application with Sharp Develop, where do I insert this code exactly?

Share this post


Link to post
Share on other sites
You'll have some class somewhere that inherits from the .NET Form class. You want to override the base Form class's implementation of OnPaint. Your implementation must therefore go inside this class.

i.e. Something like this:


class Form1 : Form
{
// ...

protected override void OnPaint(PaintEventArgs paintEvnt)
{
// Get the graphics object
Graphics gfx = paintEvnt.Graphics;
// Create a new pen that we shall use for drawing the line
Pen myPen = new Pen(Color.Black);

// Loop and create a new line 10 pixels below the last one
for(int i = 20; i < 250; i = i + 10)
{
gfx.DrawLine(myPen, 20, i, 270, i);
}
}

// ...
}

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this