using foreach and arraylist
hi, i have a arraylist which contains a list of objects to be drawn using DrawRectangle,DrawPolygon, etc. I want to use COntains method in 'foreach' to check whether the mouseclick position is within the
rectangle of the shape. Below is my code:
<code>
private void panel1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
int i=0;
Point p=new Point(e.X,e.Y);
foreach(ArrayList d in alDrawingObjects)
{
if(alDrawingObjects.Contains(p))
{
isCanvasShape=true;
ShapeNo=d.IndexOf(alDrawingObjects);
}
}
}
</code>
The error generated is "An unhandled exception of type 'System.InvalidCastException' occurred in DrawShape.exe
Additional information: Specified cast is not valid."
Anybody can help to explain what i should modify?
Thanks in advance
Re: using foreach and arraylist
Your code will never work, ever.
Ok, here we go:
1) foreach(ArrayList d in alDrawingObjects)
Right idea, but wrong usage. Assuming that the shapes given to you by DrawRectangle, DrawPolygon etc all derive from some base class called "Shape", the loop should be written as:
foreach(Shape d in alDrawingObjects)
The reason for this i that alDrawingObjects is an arraylist *containing* shapes. What your code did was assume alDrawingObjects was an arraylist containing other arraylists.
2) if(alDrawingObjects.Contains(p))
What you're asking here is if the arraylist contains a 'Point' object with the same X and Y component as 'p'. This will always return false, as your arraylist only contains 'Shape' objects (as described above).
3) ShapeNo=d.IndexOf(alDrawingObjects);
I'm not quite sure what you're trying to do here, but it's wrong. When you fix the above mistakes you won't actually be able to call d.IndexOf(), because 'd' is supposed to be a Shape not an ArrayList.
Having just implemented all that logic myself for a Silverlight designer, i have to say that what you're trying to accomplish is not a simple task, it is slightly more complicated than you'd think.
Re: using foreach and arraylist
Look for IsVisible method in System.Drawing.Drawing2D.GraphicsPath class, you can create new instance of this class then use method like AddEllipse to add your shape to the path then call the IsVisible method to check whether some point lies inside the path.
Just remember to dispose the path object.
Code:
//to test a point inside an ellipse
public bool Hittest(Point p){
GraphicsPath path = new GraphicsPath();
path.AddEllipse(.....)
bool result = path.IsVisible(p);
path.Dispose();
return result;
}
Re: using foreach and arraylist
Quote:
Originally Posted by Luthv
Look for IsVisible method in System.Drawing.Drawing2D.GraphicsPath class, you can create new instance of this class then use method like AddEllipse to add your shape to the path then call the IsVisible method to check whether some point lies inside the path.
**** that's handy. I'd have killed for something like that myself. I had roll my own hittesting, it isn't so bad when you treat everything as a rectangle ;)