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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to put check box in List

Status
Not open for further replies.

alisaif

ISP
Apr 6, 2013
418
AE
Hi,

Is it possible to show check box in listbox like we change in grid in place of text box.

Thanks

Saif
 
You need two image files, one the checked-box, the other, the unchecked-box. You'll need to track the status of each item and change the image for each item based on the status.

Craig Berntson
MCSD, Visual C# MVP,
 
Craig gave you the workaround.

Otherwise, the listbox does not compare to the grid in being a container for controls, it just has it's list and/or listitenm array, picture array, selected array etc., it even has no column sub object, but just the columnwidths property to define column sections with or without column lines.

The more typical way to use a listbox with multiple selected and unselected items is to make it a multiselect listbox and use the selected array to select items. This works best without populating it by a table/alias, though, as a workarea has just one record pointer anyway. It works, but has it's quirks, eg in programmatically setting items as selected.

You may use a grid instead of a listbox to have a checkbox and a normal text column. That should also be easy enough to do, if you don't have much code to port filling the grid instead of the listbox, eg if you used a table or alias for the listbox, you can recycle that for the grid.

Bye, Olaf.
 
If you still want to use listbox and you want to add checkbox objects (not only pictures), you can use RowSourceType=10. But the graphic images must be managed like was already mentioned.

If I'm correct, the Picture property of the listbox can be used as an array (Picture(1), Picture(2),...) for RowSourceType=0,1,5 and 10
For RowSourceType=2,3 and 6 only Picture has effect, not Picture(1), Picture(2),...

I made a short test.
Code:
PUBLIC ofrm
DO genpict
ofrm=CREATEOBJECT("MyForm")
ofrm.show()

DEFINE CLASS MyForm as Form
	ocol=Null
	ADD OBJECT lst as Listbox WITH width=300,multiselect=1
	PROCEDURE init
		This.ocol=CREATEOBJECT("collection")
		FOR lni=1 TO 5
			This.ocol.Add(CREATEOBJECT("mycheckbox"))
			This.ocol.item(lni).Caption="Item "+TRANSFORM(lni)
			This.ocol.item(lni).nListItem=lni
			This.ocol.item(lni).oList=This.lst
			This.ocol.item(lni).value2="Col 2 item "+TRANSFORM(lni)
			This.ocol.item(lni).value3="Third col item "+TRANSFORM(lni)
		NEXT
		This.lst.RowSourceType=10
		This.lst.ColumnCount=3
		This.lst.ColumnWidths="100,75,100"
		This.lst.RowSource="ThisForm.ocol,caption,value2,value3"
		FOR lni=1 TO 5
			This.lst.Picture(lni)="uncheck.bmp"
		NEXT
	ENDPROC
	PROCEDURE lst.click
		FOR lni=1 TO This.ListCount
			ThisForm.ocol.item(lni).Value=This.selected(lni)
		NEXT
	ENDPROC
	PROCEDURE lst.keypress
		LPARAMETERS nkey,nshift
		IF INLIST(nkey,13,32)
			NODEFAULT
			DODEFAULT(nkey,nshift)
			FOR lni=1 TO This.ListCount
				ThisForm.ocol.item(lni).Value=This.selected(lni)
			NEXT
		ENDIF
	ENDPROC
	PROCEDURE destroy
		This.ocol=Null
	ENDPROC
ENDDEFINE

DEFINE CLASS MyCheckbox as checkbox
	Picture="uncheck.bmp"
	DownPicture="check.bmp"
	style=1
	value=.F.
	nListItem=0
	oList=Null
	value2=""
	value3=""
	PROCEDURE interactivechange
		IF VARTYPE(This.oList)="O" AND !ISNULL(This.oList) AND This.nListItem>0
			This.oList.Picture(This.nListItem)=IIF(This.Value,This.DownPicture,This.Picture)
		ENDIF
	ENDPROC
	PROCEDURE programmaticchange
		IF VARTYPE(This.oList)="O" AND !ISNULL(This.oList) AND This.nListItem>0
			This.oList.Picture(This.nListItem)=IIF(This.Value,This.DownPicture,This.Picture)
		ENDIF
	ENDPROC
ENDDEFINE

PROCEDURE GenPict
	LOCAL lcFile,oImage,oGr,oFillColor,oLineColor,oPen,nHeight,nBkColor,nForeColor
	nBkColor=RGB(255,255,255)
	nForeColor=0
	nHeight=14
		
	lcFile="uncheck.bmp"
	oImage = Newobject('Gpbitmap',Home(1)+'ffc/_gdiplus.vcx')
	oImage.Create(m.nHeight,m.nHeight)
	oGr = Newobject('GpGraphics',Home(1)+'ffc/_gdiplus.vcx')
	oGr.CreateFromImage(oImage)
	oFillColor = Newobject( 'GpColor',Home(1)+'ffc/_gdiplus.vcx','', nBkColor%256,FLOOR(nBkColor%(256*256)/(256)),FLOOR(nBkColor/(256*256)) ) 
	oFillColor.set(nBkColor%256,FLOOR(nBkColor%(256*256)/(256)),FLOOR(nBkColor/(256*256)))
	oGr.Clear(oFillColor)
	oLineColor = Newobject( 'GpColor',Home(1)+'ffc/_gdiplus.vcx','', nForeColor,FLOOR(nForeColor%(256*256)/(256)),FLOOR(nForeColor/(256*256)) )
	oPen = Newobject('GpPen', Home(1)+'ffc/_gdiplus.vcx' )
	oPen.Create( m.oLineColor, 1 )
	oGr.DrawRectangle(oPen, 2,2, m.nHeight-5,m.nHeight-5)
	oImage.Savetofile( lcFile, "image/bmp")
		
	lcFile="check.bmp"
	oGr.DrawLine(oPen, 2,2, m.nHeight-4,m.nHeight-4)
	oGr.DrawLine(oPen, m.nHeight-3,2,2,m.nHeight-3)
	oImage.Savetofile( lcFile, "image/bmp")
ENDPROC

Respectfully,
Vilhelm-Ion Praisach
Resita, Romania
 
Thanks Mr.Vilhelm-Ion Praisach for such a nice example I will go through it.

Thanks Mr.Mike for the idea, but I want to display the records of the delivered/undelivered items of Delivery Orders in list and wanted to show the check box (checked) which is delivered and (Unchecked) which is not delivered. Otherwise, it showing .T. and .F. respectively.
Grid is good alternate.

Thanks

Saif
 
Saif said:
Thanks Mr.Mike for the idea, but I want to display the records of the delivered/undelivered items of Delivery Orders in list and wanted to show the check box (checked) which is delivered and (Unchecked) which is not delivered.

Well, that's exactly what I had in mind when I suggested using a Listview or Treeview. With those controls, it is easy to display the checkbox and to bind it to a logical field in the way you describe.

However, I'm not suggesting it's the best solution. Personally, I would probably use a grid. If nothing else, it has the advantage of being a native VFP control.

But if you (or anyone else) is interested in seeing how to do it with a Listview / Treeview, I am happy to post step-by-step instructions.

Mike


__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
I have no experience with Listview so I am very interested. Thank you in advance.

Respectfully,
Vilhelm-Ion Praisach
Resita, Romania
 
alisaif said:
I want to display the records of the delivered/undelivered items of Delivery Orders in list and wanted to show the check box (checked) which is delivered and (Unchecked) which is not delivered

Technically speaking, this is dead wrong use of the checkbox UNLESS the user can click the checkbox to toggle it.

A checkbox is a USER control. They use it to turn something on or off. If you use it for something else then they won't have certainty about what other checkboxes in your application (and others) do.

There's an example of this in VFP itself. In the menu designer's General Options dialog there are checkboxes for Setup code and Cleanup code. They're not on/off toggles. They open a window. It's completely wrong use of a checkbox and it confuses me every time I use it (which is compounded because I tend not to use it very often).

Think long before you use a control the wrong way.
 
Dan, you are completely correct. I hadn't realised that was what Saif was asking. I thought he wanted to let the user toggle the true/false state. If he just wants to display the state, he should use a tick (checkmark) to indicate true, with the absence of a tick to show fasle. In fact, you can do that without resorting to images, provided you know your users have a font installed that includes a checkmark (Wingdings or Dingbats or something similar).

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
Vilhelm-Ion Praisach said:
I have no experience with Listview so I am very interested.

Hi Vilhelm-Ion,

Keep in mind that a Listview might not be the ideal control in this particular case, for the reasons we have discussed (especially in light of what Dan said above about using a checkbox only to let the user toggle the status). But let me give you a quick walk-through, in the hope that you or others will find it useful.

Some time ago, I created a little wrapper class for the Listview, called SimpleList. Its aim is to hide most of the details of using the Listview, and especially to make it easy to bind the control to a cursor. You might want consider using it. If so, feel free to download a copy from here.

Next, drop the SimpleList on a form, and give it a sensible name. Next, set properties, as follows (I'll assume you will do this in the Init):

Code:
WITH THIS
  .cAlias = "Orders"  && alias of table or cursor to bind to the control
  .cData = "Order_ID" && a character field in the cursor that you want to display
  
  .cCheckField = "Delivered"  && a logical field in the cursor that you want to 
                              && map to the checkbox
  .lUpdateCheckbox = .T. && says that, when the user toggles the checkbox, 
                       && this will also toggle the logical field
  .lCheckBoxes = .T.   && a master switch to enable / disable the checkbox

  .PopulateList       && go ahead and display the data
ENDWITH

That's about it. The download includes a documentation file which describes the other features of the control.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
Just test it and it seems very easy to use [smile]
Thank you again, Mr. Mike Lewis.

Respectfully,
Vilhelm-Ion Praisach
Resita, Romania
 
If you merely want to display delivered/undelivered status, also look at Listbox.disableditembackcolor and itembackcolor, Adding delivered items as "\item" instead of "item" you can make use of the disabled item colors. Disabling also means they can't be selected (due to a listbox bug the can be, if using multiselect from/to two enabled items before and after the disabled ones), which might be bad or good, in case only the undelivered items should be selectable eg for tracking.

I'd go for a dynamicbackcolor in a grid, though. That can be set without enabling/disabling items. This gives much better visualisation of the state than just a checkbox.

Bye, Olaf.
 
Olaf said:
I'd go for a dynamicbackcolor in a grid, though.

I'd go for DynamicCurrentControl. That's partly because the presence or absence of a tick in the "Delivered" column has a more obvious meanding than a change of colour. And partly because I try to avoid using colours as part of the core functionality, in case any of the users are colour blind.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
I agree with Mike here. I avoid color. I once had a situation where my two immediate bosses (!) were both color blind but in completely different ways. I *had* to learn to make things clear without color.
 
Well, I disagree, because then we could also all develop for the least common denominator, there are tools for people with disabilities in Windows (Ease of Access), you can make colors configurable but even green color blind can distinguish between clear green and red.

There are other arguments against color, eg too much color would be overwhelming and destroy coprporate identity design of an application.

Anyway you can combine dynamic colors with other dynamic properties, also dynamicfont... properties: ...name, ...size, ...bold, ...italic. Again combining too many of them will not look good and be informative.

Another thing you can of course do is sort by delivery status and fill two lists or show items left/right aligned. All of this is easier in a grid than in a listbox.

A checkbox is a good interface showing status too, especially in the form of a checklist, but then again it's also about toggling the state to "done", maybe only allowing the first unchecked box to be ticked.

Bye, Olaf.

 
Olaf, it's not always possible for these tools to change color. Take an image for example.

Also, if the application is used by the US Government, it must meet regulations of the Americans with Disabilities Act. I know of one case where a company was denied a contract because the colors on their PowerPoint slides during their sales pitch did not comply. In fact, the government cut short their presentation and told them to leave.

Craig Berntson
MCSD, Visual C# MVP,
 
We're not talking of images here, but of self defined colors of a grid.
Anyway, Saif will decide and consider anything needed for his country and laws. Everything has been said.

Bye, Olaf.

 
Thanks for this healthy discussion, I am trying alternate solution with the help of your suggestions.

Saif
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top