Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dynamically displaying image in grid column

Status
Not open for further replies.

volfreak

Programmer
Nov 6, 2000
42
0
0
US
All,

I've got a grid that I'd like to have one image displayed if the record/field value is True while I'd like a different one displayed if the record/field value is False.

When I try to use an IIF() in the Picture property of the image object, I get errors (depending if I preceed the IIF with an equal sign (=) or not).

Does anyone have any idea how I may be able to accomplish this feat?

Thanks in advance for any and all help.
 
It depends where your pictures are stored. And if you only have two pictures to display (and always the same two). But assuming that is all true.
1. Create two container classes and put your pictures on each one of them.
and use something like this (alternating between containers that are currentcontrols of the column):
Code:
oForm=CREATEOBJECT("myForm")
oForm.ADDOBJECT('myGrid1','myGrid')
oForm.myGrid1.column1.header1.CAPTION = "Name"
oForm.myGrid1.column2.header1.CAPTION = "Color"
oForm.myGrid1.column2.sparse = .f.
oForm.myGrid1.column2.ADDOBJECT("container1","contain1")
oForm.myGrid1.column2.ADDOBJECT("container2","contain2")
oForm.SHOW(1)



DEFINE CLASS myForm AS FORM
    AUTOCENTER = .T.
    PROCEDURE LOAD()
    CREATE CURSOR myCursor (NAME C(20),TRUFLASE L))
    INSERT INTO myCursor (NAME,COLOR) VALUES ("MIKE",.t.)
    INSERT INTO myCursor (NAME,COLOR) VALUES ("JOHN",.f.)
    INSERT INTO myCursor (NAME,COLOR) VALUES ("PAUL",.t.)
    GO TOP
ENDPROC
ENDDEFINE

DEFINE CLASS myGrid AS GRID
    VISIBLE = .T.
    COLUMNCOUNT = 2
    WIDTH = 200
    TOP = 20
    LEFT = 20
    PROCEDURE INIT()
    WITH THIS
        .column2.DYNAMICCURRENTCONTROL = "dynamic()"
    ENDWITH
ENDPROC
ENDDEFINE
DEFINE CLASS TextRed AS TEXTBOX
    BACKCOLOR = RGB(255,0,0)
    VISIBLE = .T.
ENDDEFINE
DEFINE CLASS TextBlue AS TEXTBOX
    BACKCOLOR = RGB(0,128,255)
    VISIBLE = .T.
ENDDEFINE
DEFINE CLASS TextYellow AS TEXTBOX
    BACKCOLOR = RGB(255,255,0)
    VISIBLE = .T.
ENDDEFINE

FUNCTION DYNAMIC
LOCAL control1
IF  myCursor.trufalse =.t.
    control1 = "container1"
ELSE
        control1 = "container2"
ENDIF
RETURN control1
ENDFUNC

Mike Gagnon
 
Mike,

Thanks for the reply. Yes, I only have two images. I got so wrapped around trying to find a way to use a setting or something on a standard grid, it didn't even click to create my own objects. I've done that for other things in the grid and this is the right answer. Thanks much for the suggestion!

Take care and have a GR8 weekend.
 
Hey Mike:

Here's an interesting one.

I modified your code; a good one by the way; and came up with this weird little anomaly.

When the form's instantiated and you click on the second or third image cell, the first image cell shows the value in the 'TrueFalse' field. After that it works fine. Matter of fact - If you click on anything other than the second or third image cells everything works fine.

You'll notice I removed the second columns textbox thinking that would solve it. The cell just shows blank if you follow the steps above.

BTW. f:\chr_enter1.jpg and f:\chr_enter2.jpg are roughly the size and width of a standard textbox control.

oForm=createobject("myForm")
oForm.addobject('myGrid1','myGrid')
oForm.myGrid1.column1.header1.caption = "Name"
oForm.myGrid1.column2.header1.caption = "Color"
oForm.myGrid1.column2.sparse = .f.
oForm.myGrid1.column2.addobject("container1","containx","f:\chr_enter1.jpg")
oForm.myGrid1.column2.addobject("container2","containx","f:\chr_enter2.jpg")
oForm.myGrid1.column2.removeobject("Text1")
oForm.show()
read events

define class myForm as form
autocenter = .t.
procedure load()
create cursor myCursor (name C(20),TrueFalse L)
insert into myCursor (name,TrueFalse ) values ("MIKE",.t.)
insert into myCursor (name,TrueFalse ) values ("JOHN",.f.)
insert into myCursor (name,TrueFalse ) values ("PAUL",.t.)
go top
endproc
procedure destroy
clear events
endproc
enddefine

define class myGrid as grid
visible = .t.
columncount = 2
width = 200
top = 20
left = 20
procedure init()
with this
.column2.dynamiccurrentcontrol = "dynamic()"
endwith
endproc
enddefine
define class TextRed as textbox
backcolor = rgb(255,0,0)
visible = .t.
enddefine
define class TextBlue as textbox
backcolor = rgb(0,128,255)
visible = .t.
enddefine
define class TextYellow as textbox
backcolor = rgb(255,255,0)
visible = .t.
enddefine

define class containx as container
borderwidth = 0
backstyle = 0
visible = .t.

add object imgFile as image with ;
visible = .t.

procedure init
lparam lcPicture
this.imgFile.picture = lcPicture
*!* this.width = this.imgFile.width
*!* this.height = this.imgFile.height
endproc
enddefine

function dynamic
local control1
if myCursor.truefalse =.t.
control1 = "container1"
else
control1 = "container2"
endif
return control1
endfunc
'We all must do the hard bits so when we get bit we know where to bite' :)
 
darrellblackhawk

The difference I think is that by putting ".column2.dynamiccurrentcontrol = "dynamic()"" it evaluates every records in the cursor one at a time, but I notice that by using one container and alternating pictures, it gets eveluated once, not at every record. I would try to create two containers with pictures added at the container level and alternating containers.

Mike Gagnon
 
Mike:

I'll try what you say, but I believe technically I am using to different containters. I just instantiate the same base container twice and give it a different name, so there are two distinct containers. (If you run it and view the grid's controls, you will see two distinct containers in column2.

Darrell 'We all must do the hard bits so when we get bit we know where to bite' :)
 
Darrell

I'll also take another closer look today. BTW for the sake of acuraty the folowing lines should be removed (it was part of the original function):

define class TextRed as textbox
backcolor = rgb(255,0,0)
visible = .t.
enddefine
define class TextBlue as textbox
backcolor = rgb(0,128,255)
visible = .t.
enddefine
define class TextYellow as textbox
backcolor = rgb(255,255,0)
visible = .t.
enddefine
Mike Gagnon
 
darrellblackhawk

I realize this might not be what volfreak was looking for, but I was able to play around with your suggested code and I was able to get the desired results if I create two seperate container classes and seperate image classes.I don't really have an explanation as to why your code didn't work. He is your code modified (I've adjusted the rowheight of the grid for my pictures):
Code:
oForm=CREATEOBJECT("myForm")
oForm.ADDOBJECT('myGrid1','myGrid')
oForm.myGrid1.column1.header1.CAPTION = "Name"
oForm.myGrid1.column2.header1.CAPTION = "Color"
oForm.myGrid1.column2.SPARSE = .F.
oForm.myGrid1.column2.ADDOBJECT("myc1","myc")
oForm.myGrid1.column2.ADDOBJECT("myc3","myc2")
oForm.myGrid1.column2.myc3.ADDOBJECT("imgFile3","imgFile2")
oForm.myGrid1.column2.myc1.ADDOBJECT("imgFile","imgFile")
oForm.myGrid1.column2.REMOVEOBJECT("Text1")
oForm.SHOW()
READ EVENTS

DEFINE CLASS myForm AS FORM
	AUTOCENTER = .T.
	PROCEDURE LOAD()
	CREATE CURSOR myCursor (NAME C(20),TrueFalse L)
	INSERT INTO myCursor (NAME,TrueFalse ) VALUES ("MIKE",.T.)
	INSERT INTO myCursor (NAME,TrueFalse ) VALUES ("JOHN",.F.)
	INSERT INTO myCursor (NAME,TrueFalse ) VALUES ("PAUL",.T.)
	GO TOP
ENDPROC
	PROCEDURE DESTROY
	CLEAR EVENTS
ENDPROC
ENDDEFINE

DEFINE CLASS myGrid AS GRID
	VISIBLE = .T.
	COLUMNCOUNT = 2
	WIDTH = 200
	TOP = 20
	LEFT = 20
	ROWHEIGHT = 50
	PROCEDURE INIT()
	WITH THIS
		.column2.DYNAMICCURRENTCONTROL = "dynamic()"
	ENDWITH
ENDPROC
ENDDEFINE
DEFINE CLASS myc AS CONTAINER
	WIDTH = 20
	HEIGHT = 20
	VISIBLE = .T.
	NAME = "mycomnt"
ENDDEFINE
DEFINE CLASS imgFile AS IMAGE
	VISIBLE = .T.
	PICTURE = "c:\number1.jpg"
ENDDEFINE
DEFINE CLASS myc2 AS CONTAINER
	WIDTH = 20
	HEIGHT = 20
	VISIBLE = .T.
	NAME = "mycomnt"
ENDDEFINE
DEFINE CLASS imgFile2 AS IMAGE
	VISIBLE = .T.
	PICTURE = "c:\number12.jpg"
ENDDEFINE

FUNCTION DYNAMIC
LOCAL control1
IF  myCursor.TrueFalse
	control1 = "myc1"
ELSE
	control1 = "myc3"
ENDIF
RETURN control1
ENDFUNC


Mike Gagnon
 
Hey Mike:

I copied your code from the latest item in this thread. One quirk; if I click in the Color column for John or Paul, the picture in Mike's Color column disappears. If I click on Mike's row, the picture reappears. Any ideas?

Thanks,
Bill
 
It' also possible to obtain a similar effect in the form designer: you add to your column a checkbox (using, as controlsource, your logical field). Then you assign your picture to the PICTURE properties (corresponding to the "false" status), and the other (for the "true" status) to DOWNPICTURE property. Of course, if you need it, you can disable the control. It is not so powerful as Mike's method, but sometimes can be useful.

Gaetano
 
wtotten

Although that "quirk " I had not tested for, and I don't really like the work around, the work around seems to be, that in the click event of the container to recall the init event of the grid and also add a lockscreen on the form. Although the lockscreen does not seem to work as expected (perhaps an API lockscreen might work better), if you remove the lockscreen, it still shows the same problem, but if you leave it on, it seems to correct the image disappearing:
Code:
oForm=Createobject("myForm")
oForm.AddObject('myGrid1','myGrid')
oForm.myGrid1.column1.header1.Caption = "Name"
oForm.myGrid1.column2.header1.Caption = "Color"
oForm.myGrid1.column2.Sparse = .F.
oForm.myGrid1.column2.AddObject("myc1","myc")
oForm.myGrid1.column2.AddObject("myc3","myc2")
oForm.myGrid1.column2.myc3.AddObject("imgFile3","imgFile2")
oForm.myGrid1.column2.myc1.AddObject("imgFile","imgFile")
oForm.myGrid1.column2.RemoveObject("Text1")
oForm.Show()
Read Events

Define Class myForm As Form
	AutoCenter = .T.
	Procedure Load()
	Create Cursor myCursor (Name C(20),TrueFalse L)
	Insert Into myCursor (Name,TrueFalse ) Values ("MIKE",.T.)
	Insert Into myCursor (Name,TrueFalse ) Values ("JOHN",.F.)
	Insert Into myCursor (Name,TrueFalse ) Values ("PAUL",.T.)
	Go Top
Endproc
	Procedure Destroy
	Clear Events
Endproc
Enddefine

Define Class myGrid As Grid
	Visible = .T.
	ColumnCount = 2
	Width = 200
	Top = 20
	Left = 20
	RowHeight = 50
	Procedure Init()
	With This
		.column2.DynamicCurrentControl = "dynamic()"
		This.Parent.LockScreen = .F.
	Endwith
Endproc
Enddefine
Define Class myc As Container
	Width = 20
	Height = 20
	Visible = .T.
	Name = "mycomnt"
	Procedure Click
	This.Parent.Parent.Parent.LockScreen = .T.
	This.Parent.Parent.Init()
ENDPROC
visible = .t.
Enddefine
Define Class imgFile As Image
	Visible = .T.
	Picture = "c:\number1.jpg"
Enddefine
Define Class myc2 As Container
	Width = 20
	Height = 20
	Visible = .T.
	Name = "mycomnt"
	Procedure Click
	This.Parent.Parent.Parent.LockScreen = .T.
	This.Parent.Parent.Init()
ENDPROC
Enddefine
Define Class imgFile2 As Image
	Visible = .T.
	Picture = "c:\number12.jpg"
Enddefine

Function Dynamic
Local control1
If  myCursor.TrueFalse
	control1 = "myc1"
Else
	control1 = "myc3"
Endif
Return control1
Endfunc


Mike Gagnon

If you want to get the best response to a question, please check out FAQ184-2483 first.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top