locked
What's the best way to handle dynamically created controls? RRS feed

  • Question

  • Hi everyone.  I have a control that I've been working on that creates a bunch of pictureBoxes and labels dynamically.  I want to handle on click events for these pictureBoxes and, as I haven't done anything like this before, got a bit confused.  I would like to, when I click the pictureBox to return the image location as a FileName so that I can do stuff with that, and I would like to highlight the selected image.  But when I highlight the image it doesn't clear the previously selected images, and I can't seem to get it to return the FileName no matter what I try...  I was wondering if anyone had any ideas on how to do this.  I don't know if these are stupid questions or not, but I've never created a control before and am a bit confused.  I did get a good book on controls, but it doesn't seem to address this...

    Thanks Again,
    Tuesday, March 24, 2009 5:36 PM

Answers

  • 1)  If you load the PictureBox with an image, save the file/path data for the image in the PictureBox.Tag property.

    2) Your description sounds a bit self-contradictory. 
        "I would like to clear the highlighting of the pictureBoxes when a new one is clicked and just highlight the appropriate control."
        "I tried to keep track of which one was clicked and clear all the others, but that didn't work. "

    I guess you want to control which Picturebox currently has the Focus? 
    I'm still fuzzy on your strategy behind it all after reading if for a day.
    Mark the best replies as answers. "Fooling computers since 1971."
    • Proposed as answer by Harry Zhu Monday, March 30, 2009 3:29 AM
    • Marked as answer by Harry Zhu Monday, March 30, 2009 3:29 AM
    Friday, March 27, 2009 1:02 PM
  • Perhaps Step 1 is to break it up.  Create a custom control that consists of PictureBox and Label, that exhibit the behavior that you want.  Then add this PictueBoxLabel control to the larger control that uses a several of the smaller controls. 

    This is known as delegating responsiblities to other classes.
    Mark the best replies as answers. "Fooling computers since 1971."
    • Marked as answer by Harry Zhu Monday, March 30, 2009 3:29 AM
    Friday, March 27, 2009 1:07 PM

All replies

  •  The best way to tackle big problems is to break it up onto a set of smaller manageable problems.

    A little more info would help.  Descibe the layout of your custom control, and any method signatures you have come up with so far.  How does your custom control "fit" on your forms?  Is it a row of controls or an array of rows and columns?  Add anything else you think might be significant.  Even bad ideas you had and rejected.
    Mark the best replies as answers. "Fooling computers since 1971."
    Tuesday, March 24, 2009 6:38 PM
  • Thank you very much for your response.  I'm sorry for the delay, I was trying to meet a deadline unrelated to software development.

    Alright, that makes a lot of sense.  I'll try to break it down into more manageable chunks.  I did make pretty good progress with this and did get a lot of things working.  There are a few things that I have not had any luck with though.

    First, let me describe the control.  Basically it consists of a panel and an hScrollBar.  When I pass a directory to the control it goes through and adds all of the images to the panel.  Creating a pictureBox and a label for each image and placing the image in the pictureBox and the name of the file in the appropriate label.  The pictureBoxes are layed out horizontally with the labels below them.  The hScrollBar is setup to manage the scrolling of the panel in addition to the scrollBar in the panel (I've been trying to find a good way of removing the scrollBar from the panel but all attempts resulted in flickering or performance issues).  The control can be added to a form, it resizes quite nicely and everything, and the form can just add a directory and the control parses it and does all of the work.

    Now as far as the problems (And the attempts I've made at resolving them).  I'll try to give as much detail here as possible, but I tried a lot of things and I don't know that I can remember them all honestly.  I scrapped this so many times at this point that I didn't hang on to much of the failed code.  I realize this was probably a poor choice.  I became kind of frustrated.  Working with controls really requires a different way of thinking and I'm having a bit of a hard time doing that.

    1) The first problem is that I would like to return the fileName when the image or label is clicked.  The best I was able to do is return the number of the pictureBox that was clicked (Starting from 0).  This was based on an example that I found.

    2) The second problem is that I would like to clear the highlighting of the pictureBoxes when a new one is clicked and just highlight the appropriate control.  I tried to go through and just clear the highlighting for every pictureBox, but this didn't work.  I tried to keep track of which one was clicked and clear all the others, but that didn't work.  I am really having a very hard time understanding how to work with dynamically created content like this.  In the past controls have always been there at design time for me, so this project is a bit different from other things that I have done.  I'm certainly open to any ideas or anything that you might have. 

    Thanks again for your help.  If you have any ideas or suggestions, please let me know.  It's something I'm having a very hard time understanding.  I was able to fix a lot of the other things on my own, but these two issues are kind of a big deal and I can't wrap my head around them at all.

    Thanks Again,
    Thursday, March 26, 2009 5:09 AM
  • 1)  If you load the PictureBox with an image, save the file/path data for the image in the PictureBox.Tag property.

    2) Your description sounds a bit self-contradictory. 
        "I would like to clear the highlighting of the pictureBoxes when a new one is clicked and just highlight the appropriate control."
        "I tried to keep track of which one was clicked and clear all the others, but that didn't work. "

    I guess you want to control which Picturebox currently has the Focus? 
    I'm still fuzzy on your strategy behind it all after reading if for a day.
    Mark the best replies as answers. "Fooling computers since 1971."
    • Proposed as answer by Harry Zhu Monday, March 30, 2009 3:29 AM
    • Marked as answer by Harry Zhu Monday, March 30, 2009 3:29 AM
    Friday, March 27, 2009 1:02 PM
  • Perhaps Step 1 is to break it up.  Create a custom control that consists of PictureBox and Label, that exhibit the behavior that you want.  Then add this PictueBoxLabel control to the larger control that uses a several of the smaller controls. 

    This is known as delegating responsiblities to other classes.
    Mark the best replies as answers. "Fooling computers since 1971."
    • Marked as answer by Harry Zhu Monday, March 30, 2009 3:29 AM
    Friday, March 27, 2009 1:07 PM
  • Thanks Rudedog, I hadn't heard of doing it like that (I'm new to controls).  I will give that a shot and see how things go.  Hopefully I'll have some luck.  I get rather confused when trying ti figure out how to expose things so that the controls can handle events / do things...  I'm sure I'll get it at some point, but right now it really has me rather confused.

    Thanks Again,
    Monday, April 6, 2009 10:57 PM
  • 30 years ago, engineers like me called it "Black Box Design".  Break the problem up into smaller and smaller "black boxes" that performed "FM".  I cannot remember what "FM" stood for, but I think the second word was magic.  By breaking it up, you eventually reach a point where the "black boxes" can do things that you recognize instead of "magic". 

    Programmers had a name for this approach, too.  "Top Down Programmiing"
    Mark the best replies as answers. "Fooling computers since 1971."
    Monday, April 6, 2009 11:34 PM