HbH

this is really urgent, I need help about it..

I'm drawing a curve, and taking the coordinates of the points of the curve by clicking the mouse on the picturebox. I'm using a dynamic arraylist to get the best result about it and it works really good.

but the problem is, whenever I click the mouse, it give an additional point to that curve. What I want is, to finish adding points to a curve (i.e. by right clicking the mouse) and when I start to left click again, it should start drawing a new curve, and it must continue this way for every other curve.

I thought that it might be useful to make an object(which has its arraylist in it) array and to let the counter rise by1 when I right-click, so that it can start drawing a new curve. but I couldn't succeed. can anyone help thanks in advance.



Re: Windows Forms General more than one curve?

yasser azeem

1.Create an arraylist to store the point coordinates whenever you click mouse.
2. whenever you click mouse check it whether left mouse button is clicked or right button is clicked using the event arguments of mouse event.
3. if left mouse button is clicked then create a System.Drawing.Point Structure variable , initialize it with mouse coordinates and store it in an arraylist.
4. when right mouse button is clicked then create the curve using coordinates stored in arraylist and clear arraylist.




Re: Windows Forms General more than one curve?

HbH

I can already write acurve by applying what you've written. but the problem is, that I want to start drawing another curve when I right-click.

(for each click, it already draws the curve(the arraylist of the points.))





Re: Windows Forms General more than one curve?

nobugz

Use a list of lists. I'll give you some code if you tell me what framework version you're using.





Re: Windows Forms General more than one curve?

HbH

I'm using .net 2005, framework 2.0

a list of list you mean I'm wrong with the Arraylist (Im using the arraylist because I want my array to grow dynamically, if I make a normal array of points, then it initializes all the points to 0,0 and doesnt give me the drawing I want.)





Re: Windows Forms General more than one curve?

nobugz

Here's a sample. I used the generic List class because it is so much cleaner than ArrayList. Start a new Windows Forms project and paste this code. Draw by clicking the mouse on the form surface.

public partial class Form1 : Form {
private List<List<Point>> mCurves;
public Form1() {
InitializeComponent();
this.Paint += Form1_Paint;
this.MouseDown += Form1_MouseDown;
mCurves = new List<List<Point>>();
mCurves.Add(new List<Point>());
}
private void Form1_MouseDown(object sender, MouseEventArgs e) {
if (e.Button == MouseButtons.Left) {
// Add a new point to the current curve
mCurves[mCurves.Count - 1].Add(e.Location);
this.Invalidate();
}
else {
// Add a new curve if the current one contains points
if (mCurves[mCurves.Count-1].Count > 0) mCurves.Add(new List<Point>());
}
}

private void Form1_Paint(object sender, PaintEventArgs e) {
// Paint the curves
foreach (List<Point> curve in mCurves) {
if (curve.Count > 0) {
Point pos = curve[0];
foreach (Point pnt in curve) {
e.Graphics.DrawLine(Pens.Black, pos, pnt);
pos = pnt;
}
}
}
}
}


You can make drawing more intuitive by using ControlPaint.DrawReversibleLine.





Re: Windows Forms General more than one curve?

HbH

thank you for giving the code, it draws "lines", but I think I got the point.

I hope just changing the names of the "Arraylist"s to "List"s will be enough.

by the way, I tried to do all you wrote on a picturebox, and added the events for mouse down and picturebox paint. but couldnt succeed. Did I do sth wrong





Re: Windows Forms General more than one curve?

nobugz

Not sure, ought to work. Make sure you call Invalidate() on the picturebox, not on the form.