Array Issues

This is a discussion on Array Issues within the C# Programming forums, part of the General Programming Boards category; I have an array of pictureboxs which i want to be able to check for mouse events. Can i set ...

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    85

    Array Issues

    I have an array of pictureboxs which i want to be able to check for mouse events. Can i set up an eventhandler that will check each picturebox in the array without having to set an individual event for each box ?

    I have read somewhere that it is possible to write an eventhandler for the base class ?

    also in conjuction with this, how can i attach an image to the mouse in the drag and drop mode. i.e. semitransparent image of original which will follow the mouse around until dropped ?

    please try to keep it simple or reasonably well commented if you post code thanks.

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Either write and assign a generic event handler ("object Sender" is the PictureBox), or use lambda functions:
    Code:
    foreach(var PictureBox in MyPictureBoxes)
    {
    	PictureBox.Click += (Sender, EventArgument) =>
    	{
    		//Do something here
    	};
    }
    The latter assumes you have a fairly modern version of .NET, 3.0 or 3.5 (can't remember which).
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    85
    thanks for your reply but looking at that it creates a new eventhandler for every picturebox ( am i correct ?) . i was hoping to use one handler regardless of which picturebox is clicked. Is this possible ?

  4. #4
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Like:
    Code:
    foreach(var PictureBox in MyPictureBoxes)
        PictureBox.Click += Handler;
    
    ...
    void Handler (object sender, HoweverItsCalled args)
    {
        ...
    }
    ?

    sender will be the PictureBox object as Magos guarantees

  5. #5
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507
    This is using one handler regardless of which picturebox is clicked. In accordance to typical observer patterns the sender is also sent to the handler, therefore you just need to cast sender to a PictureBox and access its public interface from there.

  6. #6
    Registered User
    Join Date
    Oct 2008
    Posts
    85
    ok so i have set up a Foreach to initialise eventhandlers for all the pictureboxes in the array and have checked that it actually goes to the handler for every box.

    no my problem is - how do i determine which picturebox fired the event?
    Code:
     public void picClick(object sender, EventArgs e)
            {
             // change border style to 3d (for example)    
            }
    i understand sender is the object that raised the event but how do i check it ?

    EDIT:


    thanks All - have cast it over and it does what i want now

    but how do i found out the array index for the picturebox from its interface ?

    any thoughts on the attaching a "ghost" image to the mouse ?
    for when i move it around ?
    Last edited by deviousdexter; 11-19-2008 at 12:49 PM.

  7. #7
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Use the Tag property. When creating the PictureBox array you should put a unique Tag to each PictureBox, typically its index in the array.

    A "ghost" image meaning? Like a different cursor picture? You can search for cursors and change the picture, should be possible. Or create a handler for the mouse changing position and move the image accordingly. Don't know if I understand correctly what you want

  8. #8
    Registered User
    Join Date
    Oct 2008
    Posts
    85
    i dont want to change the cursor but rather attach a copy of whatever image was clicked in the picturebox to the cursor during drag and drop functions. this image will stay with the mouse until another image is selected or it is placed on the screen.

    hope this makes sense.

  9. #9
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    It makes sense. That doesn't seem something hard to do. You just need a mouse movement handler. Something like:
    Code:
    this.MouseMove += mouseHandler;
    
    void mouseHandler(object sender, MouseEventArgs e)
    {
        myImage.Location = new Point(e.X, e.Y);
        ...
    }
    I assume e.X and e.Y so the current position of the mouse, you can check it up. Now you want this to happen when you click on the image. So you need another event hanlder and a Image variable. Like:
    Code:
    myImage.Clicked += clicked;
    Image selected;
    void clicked(object sender, EventArgs e)
    {
       selected = myImage
        ...
    }
    
    this.MouseMove += mouseHandler;
    
    void mouseHandler(object sender, MouseEventArgs e)
    {
        if (selected != null)
           selected.Location = new Point(e.X, e.Y);
        ...
    }
    So, when you click on an image the selected will be set. Then its location will move around according to the mouse position. If you select something else then the other picture will be moved.

    If you want one event handler for all images, then you can also use the sender parameter in the clicked method. You get my point I hope

  10. #10
    Registered User
    Join Date
    Oct 2008
    Posts
    85
    thanks will give that a try and see how i get on.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Have problems with copying my array!
    By AvaGodess in forum C Programming
    Replies: 11
    Last Post: 09-25-2008, 01:56 AM
  2. question about multidimensional arrays
    By richdb in forum C Programming
    Replies: 22
    Last Post: 02-26-2006, 09:51 AM
  3. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 08:31 AM
  4. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 05:15 AM
  5. Code: An auto expanding array (or how to use gets() safely).
    By anonytmouse in forum Windows Programming
    Replies: 0
    Last Post: 08-10-2004, 01:13 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21