Subject Re: Closing an image on a form
From Mervyn Bick <invalid@invalid.invalid>
Date Sat, 10 Apr 2021 10:24:53 +0200
Newsgroups dbase.getting-started

On 2021/04/10 08:10, roy price wrote:

> Hello Again Mervyn / Akshat,
> After setting up a test area, with three records on the main table, each with a field "Photoyesfile" "01", "02", "03", and three image files (with one image each), and trying (very trying) the code below in various combinations, I get nowhere with selecting the second and third files images.
> The If's or Cases statements work to get to the correct line, but the statements, "this.dataSource =", or "form.testimagedatamodule1.dataSource =" do not cause the first image  to be replaced with the second, or third.
>
>   this.IMAGEPHOTO1 = new IMAGE(this)
>     with (this.IMAGEPHOTO1)
>        onOpen = class::IMAGEPHOTO1_ONOPEN

Each image control needs an onOpen event handler.  As the code for each
will be identical you can actually assign the IMAGEPHOTO1_ONOPEN to each
of the other two image controls instead of coding separate onOpen event
handlers.

>        height = 19.0
>        left = 4.0
>        top = 8.5
>        width = 78.0
>        dataSource = form.testimagedatamodule1.photos01_dbf1.rowset.fields["photo"]
>        enabled = true
>     endwith
>
>     this.rowset = this.testimagedatamodule1.cactusdb_dbf1.rowset
>
>   function IMAGEPHOTO1_onOpen()
>         mphoto = form.testimagedatamodule1.cactusdb_dbf1.rowset.fields["photoyesfile"].value
>                 
>       If mPhoto='01'
>            this.dataSource =  form.testimagedatamodule1.photos01_dbf1.rowset.fields["photo"]
>       elseif mPhoto='02'
>        this.dataSource =  form.testimagedatamodule1.photos02_dbf1.rowset.fields["photo"]
>       elseif mPhoto='03'
>            this.dataSource =  form.testimagedatamodule1.photos03_dbf1.rowset.fields["photo"]
>       endif
>     return
>        return

If you are using three image controls you have already assigned the
correct image table to the datasource property in the constructor code.
  When the form opens you need to make sure that only one displays an
image when the form opens.  This means nulling the datasource property
for each of the two unused image controls.  The main table's onNavigate
event handler will ensure that the correct image control has its
datasource property set for all records except the first record.

  function IMAGEPHOTO1_onOpen()
        mphoto =
form.testimagedatamodule1.cactusdb_dbf1.rowset.fields["photoyesfile"].value
                
      If mPhoto='01'
        form.IMAGEPHOTO2.dataSource = []
         form.IMAGEPHOTO3.datasource = []
      elseif mPhoto='02'
         form.IMAGEPHOTO1.dataSource = []
         form.IMAGEPHOTO3.datasource = []
      elseif mPhoto='03'
        form.IMAGEPHOTO1.dataSource = []
         form.IMAGEPHOTO2.datasource = []
      endif
    return



>
>     function form_onNavigate()
>            mphoto = form.testimagedatamodule1.cactusdb_dbf1.rowset.fields["photoyesfile"].value
>                 do case
>                 case mphoto = '01'
>                    //add code here to navigate to correct record in photos01_dbf1
>          form.testimagedatamodule1.dataSource =  form.testimagedatamodule1.photos01_dbf1.rowset.fields["photo"]
>        case mphoto= '02'
>                   //add code here to navigate to correct record in photos02_dbf1
>           form.testimagedatamodule1.dataSource =  form.testimagedatamodule1.photos02_dbf1.rowset.fields["photo"]
>        case mPhoto = '03'
>                   //add code here to navigate to correct record in photos03_dbf1
>           form.testimagedatamodule1.dataSource =  form.testimagedatamodule1.photos03_dbf1.rowset.fields["photo"]
>        endcase
>        return

No event handler executed by an event of a query, a rowset or a field
object understands the concept of 'form'.  You ALWAYS need to use
this.parent.parent. (or this.parent.parent.parent.  if using a
datamodule) in place of form. in the code.  If this is not giving you
errors it means the event handler is not correctly assigned to the
rowset's onNavigate event.  I gave you an example in a previous post.

To make sure the form opens with an image in only one of the three image
controls you need to use an on_open event handler.  The form's
form_onOpen event handler is triggered before the IMAGEPHOTO1_onOpen
event handler so you can actually use either.  My preference would be to
use IMAGEPHOTO1_onOpen for all three image controls.

>     function form_onOpen1()
>         mphoto = form.testimagedatamodule1.cactusdb_dbf1.rowset.fields["photoyesfile"].value
>                 
>       If mPhoto='01'
>            this.dataSource =  form.testimagedatamodule1.photos01_dbf1.rowset.fields["photo"]
>       elseif mPhoto='02'
>        this.dataSource =  form.testimagedatamodule1.photos02_dbf1.rowset.fields["photo"]
>       elseif mPhoto='03'
>            this.dataSource =  form.testimagedatamodule1.photos03_dbf1.rowset.fields["photo"]
>       endif
>     return
>        return

The code in the form_onOpen1 event handler above does nothing even if it
is correctly assigned to the form's onOpen event.  'this' in an event
handler ALWAYS refers to the object which executes the event.  In this
case it is the form.  A form object does not have a datasource event so
all this code is doing is creating a user-defined property named
datasource for the form and assigning a value to it.  dBASE has no way
of knowing what to do with it.

If you really want to use the form's onOpen event handler instead of the
three image controls' onOpen event handlers then you need to use the
code in the function IMAGEPHOTO1_onOpen() above.



Mervyn.