CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Oct 2006
    Posts
    94

    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

  2. #2
    Join Date
    May 2007
    Posts
    1,546

    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.
    www.monotorrent.com For all your .NET bittorrent needs

    NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.

  3. #3
    Join Date
    Nov 2002
    Location
    Baby Land
    Posts
    646

    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;
    }

  4. #4
    Join Date
    May 2007
    Posts
    1,546

    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
    www.monotorrent.com For all your .NET bittorrent needs

    NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured