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

    Pass Data onclick event

    Hi,

    I have a form with a flow layout panel which I'm loading multiple images into

    Code:
    ...
      Image = new Bitmap(Picture);
      AlbumImage = new PictureBox();
      AlbumImage.Height = 75;
      AlbumImage.Width = 100;
      AlbumImage.SizeMode = PictureBoxSizeMode.StretchImage;
      AlbumImage.Image = Image;
    
      AlbumImage.Click += new EventHandler<string>(ImageClicked);
    
       BackPanel.Controls.Add(AlbumImage);
    ...
    now when i click an AlbumImage it fires ImageClicked ok but how would I pass an Image ID so i know which image has been clicked

    Thanks

  2. #2
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Pass Data onclick event

    First, your code will not compile. This line:

    Code:
    AlbumImage.Click += new EventHandler<string>(ImageClicked);
    ...is not valid. The argument to the generic EventHandler must be derived from EventArgs.

    Now, what is "AlbumImage"? Is this your own class? If so, then you just need to create your own class derived from EventArgs which contains the information you need.

  3. #3
    Join Date
    Oct 2004
    Posts
    97

    Re: Pass Data onclick event

    Ah yeah i have taken the string out

    AlbumImage is a standard PictureBox

    Code:
    AlbumImage = new PictureBox();

  4. #4
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Pass Data onclick event

    Quote Originally Posted by Theone2k View Post
    Ah yeah i have taken the string out

    AlbumImage is a standard PictureBox

    Code:
    AlbumImage = new PictureBox();
    If it is a standard picture box then you can only display one image at a time. Even if you have sub images inside of that image, figuring out which was clicked in this way will be messy. I would suggest creating a UserControl which contains multiple pictureboxes. You can then pass the click event from any child control through the UserControl with some extra information in your custom EventArgs class.

  5. #5
    Join Date
    Oct 2004
    Posts
    97

    Re: Pass Data onclick event

    The Full load Code


    Code:
    Bitmap Image;
                PictureBox AlbumImage = new PictureBox();
                Label AlbumDescription;
                FlowLayoutPanel BackPanel;
    
                // Process the list of files found in the directory. 
                string[] Thumbnails = Directory.GetFiles("C:\\Users\\David\\Desktop\\Wedding Photos\\Thumbnails");
                foreach (string Picture in Thumbnails)
                {
                    // do something with fileName
                    BackPanel = new FlowLayoutPanel();
                    BackPanel.Height = 110;
                    BackPanel.Width = 110;
                    BackPanel.BackColor = Color.White;
                    BackPanel.Margin = new System.Windows.Forms.Padding(0);
    
                    Image = new Bitmap(Picture);
                    AlbumImage = new PictureBox();
                    AlbumImage.Height = 75;
                    AlbumImage.Width = 100;
                    AlbumImage.SizeMode = PictureBoxSizeMode.StretchImage;
                    AlbumImage.Image = Image;
    
                    AlbumImage.Click += new EventHandler(ImageClicked);
    
                    BackPanel.Controls.Add(AlbumImage);
                    AlbumDescription = new Label();
                    AlbumDescription.Text = "Album Description";
                    BackPanel.Controls.Add(AlbumDescription);
                    
    
                    this.AlbumFlow.Controls.Add(BackPanel);
                }
    
                    this.AlbumFlow.AutoSize = true;
                    this.MaskPanel.Controls.Add(AlbumFlow);
                    this.AlbumFlow.Location = this.MaskPanel.Location;
    
                    this.AlbumFlow.Left = this.AlbumFlow.Left += 17;
    
                    this.btn_Albumright.Location = this.MaskPanel.Location;
                    this.btn_AlbumLeft.Location = this.MaskPanel.Location;
                    this.btn_AlbumLeft.Top -= 1;
                    this.btn_Albumright.Top -= 1;
                    this.btn_AlbumLeft.Left = this.MaskPanel.Width - 10;
    
            }
    What happens is the program looks in a folder for all the images and loads then loops through loading each image into a new Picturebox this then gets added to a flowlayoutcontrol so i get a bar with all the images loaded.

    what I'm trying to do is find out which image has been clicked So i can then load that albums images into the next part of the program...

  6. #6
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Pass Data onclick event

    Yes, I know. You could so this by casting the 'sender' object to a picturebox and then saving a reference to its "Image" property. However, if you want to do this in a cleaner fashion I would still recommend the UserControl approach.

  7. #7
    Join Date
    Oct 2004
    Posts
    97

    Re: Pass Data onclick event

    Wouldn't I still have the same problem with a user control that i would have to cast the sender as the new user control?

  8. #8
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Pass Data onclick event

    Quote Originally Posted by Theone2k View Post
    Wouldn't I still have the same problem with a user control that i would have to cast the sender as the new user control?
    Nope. Your user control would contain some number of PictureBox controls. You would handle the click event for each PictureBox internally and then fire your own, custom event, something like "ImageClicked". Now you create your own event handler class. This class may contain a field named "Image" that contains a reference to the clicked image. So now you just handle the "ImageClicked" event in your form class and you automatically get information on the clicked image, you don't care about which control was clicked. This is a far more extensible and maintainable solution. You can easily modify your UserControl class without having to change all of the code in your form class. It is just a separation of concerns.

  9. #9
    Join Date
    Oct 2004
    Posts
    97

    Re: Pass Data onclick event

    Hmm I was thinking of making a user control for the thumbnail views (These Pics).

    you also suggest it should contain a number of picture boxes and handle the clicks internally but I have a variable number of images...would i not get the same issue in the user control...

    I have never made a user control before

  10. #10
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Pass Data onclick event

    You would have to dynamically create and load the images then. Your UserControl would serve as a container, and for each child control that you add you would subscribe to its click event. Yes, you would still be casting sender to get the image, but that logic is now where it belongs instead of in your Form class.

    Of course, if you wanted to avoid that as well you could also create your own class derived from PictureBox and add an "ImageClicked" event which passed the image along in the EventArgs parameter. Then you don't need to cast anything, but the cast isn't bad really.
    Last edited by BigEd781; November 20th, 2009 at 07:43 PM.

  11. #11
    Join Date
    Oct 2004
    Posts
    97

    Re: Pass Data onclick event

    Thanks for the help

    also found this site

    http://ondotnet.com/pub/a/dotnet/200...ts.html?page=2

    was relay helpful

    I have made a Thumbnail User Control which I was planning to do eventually this now lets me send info via a custom events args that will give me the data i need and I don't need to cast anything

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