Jack Harmon554497

I'm considering learning C# for my next project.

Is there a method to handle drawing lines between points that are clicked on a dialog For example, you could click 5 times to draw a box, or click 9 times to draw an octagon

Are there any examples of this

Thanks much!



Re: Windows Forms General Considering C#

Matthew Watson

To get you started, check out this Microsoft sample application.

It doesn't do exactly what you want, but it's a complete application, and it shows you how to write code to let the user interact and draw things.





Re: Windows Forms General Considering C#

TilakGopi

Hi,

Hope the following code will do what u wanted.



List<Point> lstPoints;
int nPoints = 0;

private void Form1_Load(object sender, EventArgs e)
{
lstPoints =
new List<Point>();
}

private void Form1_MouseClick(object sender, MouseEventArgs e)
{
lstPoints.Add(e.Location);
Pen pen = new Pen(Color.Blue, 2);
if (nPoints < lstPoints.Count)
{
Graphics g = Graphics.FromHwnd(this.Handle);
for (int index = 0; index < lstPoints.Count - 1; index++)
{
g.DrawLine(pen , lstPoints[index], lstPoints[index+1]);
}
nPoints++;
}

}


Thans & Best Regards,

Ch.T.Gopi Kumar.






Re: Windows Forms General Considering C#

Jack Harmon

That looks like it'd be EXACTLY what I was looking for!

Thanks a TON! Guess I'll be learning C# afterall! =)





Re: Windows Forms General Considering C#

nobugz

Tilak's code is not technically correct. The drawing won't properly repaint itself when you minimize, then restore the form. Check this thread for code that shows how to use the form's Paint event.





Re: Windows Forms General Considering C#

TilakGopi

welcome jack,

Best Regards,

Ch.T.Gopi Kumar.






Re: Windows Forms General Considering C#

TilakGopi

Yes Nobugz,

You are right.

I didn't consider the situation.Now i added the support.

Please check this one also and help me in filling the holes in my brain.

Thanks pal


List<Point> lstPoints;
int nPoints = 0;
Pen pen;

public Form3()
{

lstPoints = new List<Point>();
}

private void Form3_Load(object sender, EventArgs e)
{
pen =
new Pen(Color.Blue, 2);
}

private void Form3_MouseClick(object sender, MouseEventArgs e)
{
lstPoints.Add(e.Location);

if (nPoints < lstPoints.Count)
{
Graphics g = Graphics.FromHwnd(this.Handle);
for (int index = 0; index < lstPoints.Count - 1; index++)
{
g.DrawLine(pen, lstPoints[index], lstPoints[index + 1]);
}
}
nPoints++;

}

private void Form3_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
for (int index = 0; index < lstPoints.Count - 1; index++)
{
g.DrawLine(pen, lstPoints[index], lstPoints[index + 1]);
}
}



Thanks & Best Regards,
Ch.T.Gopi Kumar.






Re: Windows Forms General Considering C#

TilakGopi

Hi Jack,

the Updated code is(previous code has some flaws in it).


List<Point> lstPoints;
int nPoints = 0;
Pen pen;

public Form1()
{

lstPoints = new List<Point>();
}

private void Form1_Load(object sender, EventArgs e)
{
pen =
new Pen(Color.Blue, 2);
}

private void Form1_MouseClick(object sender, MouseEventArgs e)
{
lstPoints.Add(e.Location);

if (nPoints < lstPoints.Count)
{
Graphics g = Graphics.FromHwnd(this.Handle);
for (int index = 0; index < lstPoints.Count - 1; index++)
{
g.DrawLine(pen, lstPoints[index], lstPoints[index + 1]);
}
}
nPoints++;

}
private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
if(lstPoints != null)
{
for (int index = 0; index < lstPoints.Count - 1; index++)
{
g.DrawLine(pen, lstPoints[index], lstPoints[index + 1]);
}
}
}


Thans & Best Regards,

Ch.T.Gopi Kumar.






Re: Windows Forms General Considering C#

Jack Harmon

Been getting the framework going on my application, so I hadn't used the code yet. Hopefully tonight I'll get to use it and can let ya know how it goes.

Thanks again!





Re: Windows Forms General Considering C#

Jack Harmon

The code example gave me everything I needed to make it happen!

Thanks again!





Re: Windows Forms General Considering C#

nobugz

We don't know what code sample you ended up using. Please mark the answer you used with the Answer button.