Welcome To The Home Of The Visual FoxPro Experts  
home. signup. forum. archives. search. google. articles. downloads. faq. members. weblogs. file info. rss.
 From: Jamie Osborn
  Where is Jamie Osborn?
 Jamie Osborn
 To: Bill von Valtier
 Bill von Valtier
Subject: RE: Adding objects and event code programmatically
Thread ID: 22293 Message ID: 22303 # Views: 32 # Ratings: 0
Version: Visual FoxPro 6 Category: Forms
Date: Tuesday, April 15, 2003 11:52:58 AM         

> 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.
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.



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