Welcome To The Home Of The Visual FoxPro Experts  
home. signup. forum. archives. search. google. articles. downloads. faq. members. weblogs. file info. rss.
 From: Bill von Valtier
 Bill von Valtier
 To: Jamie Osborn
  Where is Jamie Osborn?
 Jamie Osborn
Subject: RE: Thanks, Jamie
Thread ID: 22293 Message ID: 22332 # Views: 9 # Ratings: 0
Version: Visual FoxPro 6 Category: Forms
Date: Tuesday, April 15, 2003 10:10:06 PM         

> > Hello friends,
> >
> > My question is how to create multiple similar objects (images, in this case) on a form at run time *while* at the same time including some necessary event code with each object.
> >
> > THE PROJECT: is to build a highly customized photo editor and organizer which will handle up to 28 photos on one screen (though in actual use it will be 3 to 10). It will provide for re-ordering the photos, naming and numbering the series after rearranged, deleting or storing individual photos when asked, toggling size, comparing any two side by side, etc.
> >
> > Since the number of image objects to be handled will be variable, the form must be built and arranged at run time. And this is definitely a giant step for me in getting from FXP2.6 to VFP6. :-o
> >
> > The overall plan is to provide user-defined methods in the ParentForm code for each special function, and then call the method with a passed parameter of the index-number of the calling image object, with something like
> >
> > PROCEDURE Image1.Click		&& or RightClick, or DoubleClick
> > 	DO THISFORM.MyClickMethod WITH [index number of the image]
> > 

> >
> > And, that is where I run into my problem.
> >
> > THE PROBLEM: I have successfully gotten as far as adding the variable number of image objects and displaying them using the AddObject method (placed in the Form's Init event). But apparently the AddObject method doesn't allow for including procedures with each added object (or does it?). So I am looking at using the Define Class ... EndDefine command which permits procedures to be included. But for this route, I don't see how to make the new image objects members of the parent Form Class.
> >
> > This is probably pretty basic to you guys, but it's clear as mud to me at this point. Any points in the right direct will be appreciated.
> >
> > TIA
> If I understand you - you want each image that you add to have a custom method that is called when the image is clicked ? If so you should create your own image subclass, add your custom method to that class and be instantiating your subclass objects on the form and not from the base image class.
> I'm not sure where you are at with creating your own classes and for the sake of this example will assume you have no class libraries in your project. A step by step for getting your class created is:
> 1. Select the classes tab in your project and click the New button. The new class dialog is displayed.
> 2. In the class name textbox type : MyImage
> 3. In the based on combo select Image
> 4. In the store in textbox type MyClasses and click OK. The class designer will open.
> 5. Select Class/New Method from the menu. The New Method dialog is displayed.
> 6. Type MyClick and click Add and then Close.
> 7. Edit the Click event of the class and put a call into your new method. ie. this.MyClick()
> 8. Edit the MyClick() method and put whatever code you want to go in there. This will happen when the image is clicked.
> 9. Now in your code that dynamically adds the image controls to the form, instead of adding an image object add a myImage object
> NB. You need to ensure that you have issued a SET CLASSLIB MyClasses somewhere in your app before you start adding MyImage objects.
> I hope that this helps.
> Cheers,
> Jamie
> jamie.osborn@foxite.com

Thanks a lot Jamie. That's exactly what I needed. Not knowing it, I was looking squarely into the face of my next step in OOP, and you saw that. As I read your answer, the lights went on and it all fell into place. You were quite right, I had not yet gotten into sub-classing my own objects. And so, today I have been working merrily away, experimenting with the power of inheritance. It's great, and really does feel like a giant step. Your spelling out the steps with the Class Designer (which I had never even opened yet) were really very helpful, and I'm sailing now. So thanks again.

Now a fine tuning question about a general principle in doing stuff like this.

Example: I want this program, when left-clicked on any image, to toggle the image between fullsize and thumbnail size. I can do this either by placing a Click method in each of all the images added to the form, like this...
     IF .fullsize                                     && a custom property (logical) that reports the size status
          .WIDTH = .WIDTH/2
          .HEIGHT = .HEIGHT/2
          .WIDTH = .stdwidth                  && also custom properties
          .HEIGHT = .stdheight
     .fullsize = NOT .fullsize

... or I could write a single similar method in the parent Form's code. Then, when any image object is clicked, the parent method could be called with a passed parameter (a number from 1-28) to do the same thing, (using proper hierarchy referencing, I realize).

The former method (code in each instance of the sub-class) has to generate a lot of code at run time (especially because I plan to have about 6-8 different manipulations actuated from a shortcut right-click menu), whereas the latter does it once, and seems a little more streamlined to me. Would it be better to do it that way?

Thanks again. You guys helping us newbies are a real blessing.

Bill vV


Adding objects and event code programmatically Posted by Bill von Valtier @ 4/14/2003 10:39:54 PM
RE: Adding objects and event code programmatically Posted by Jamie Osborn @ 4/15/2003 11:52:58 AM
RE: Thanks, Jamie Posted by Bill von Valtier @ 4/15/2003 10:10:06 PM
RE: Thanks, Jamie Posted by Andy Rice @ 4/16/2003 1:02:28 AM
RE: Thanks, Jamie Posted by Jamie Osborn @ 4/16/2003 1:31:54 AM