View Full Version : Images into ListView Checkboxes

03 March 2008, 04:46 PM
Hi guys,

I ma majorly use to this Dot Net stuff, but I am determined to crack it. I am looking to get icon images to replace the checkbox tick box (A little like the layermanager), so I can include my own custom icons for different things.

I have no idea how to get the images in the ListView Checkboxes though.... This is what Ive done so far... this is just random snippets of code...

--Create a List View
dotNetControl lv "System.Windows.Forms.ListView" width:290 height:160 pos:[20,20]
local lvItems=for x=1 to 4 collect "Item "+x as string -- Some listview Items

lv.View = (dotNetClass "System.Windows.Forms.View").Details
lv.fullRowSelect = true -- When item is clicked, all columns in that item are selected

lv.checkboxes = true -- Add checkboxes (to which im trying to add the images)
lv.Columns.add "Col1" 80 -- Add some nice columns
lv.Columns.add "Col2" 80
lv.Columns.add "Col3" 80

theRange = #()
for x=1 to lvItems.count do ( li = dotNetObject "System.Windows.Forms.ListViewItem" lvItems[x]; append theRange li )
lv.Items.AddRange theRange -- add in all the items

--From here I assume the ListViewItems need the images provided, which I thought would be an image list... this is where the real confusions starts....

--Create an Image add it to an image list...
CheckImage = dotNetClass "System.Drawing.Image"
CheckImage.FromFile "C:\SampleImage.jpg"

smallImageList = dotnetObject "System.Windows.Forms.ImageList"
smallImageList.Images.Add(CheckImage.FromFile "C:\SampleImage.jpg")

-This has successfully created the image list and added the image... but I cannot get this to the listviewitem. I tried setting it to the ImageList property, but that is read only!

How can I set this image list to be recognised by the checkboxes in the listview, so that the images are displayed? And then how can I change the image depending on the state?

Really sorry to ask, but any pointers would be ace, since I am just starting out, and to be honest am really struggling!

Thanks for your time everyone :)


03 March 2008, 05:33 PM
Use the .StateImageList property instead of the .ImageList, and also make sure that .CheckBoxes is set to true for the listview.

Using this procedure, you've essentially replaced the checkbox grapics with your own custom set. The first image in the list (index 0) is used for the unchecked state, and the second image (index 1) is used for the checked state.

The switching between the two states when you click on the icon is handled automatically by DotNet (in other words, clicking on the icon will toggle not only the graphic that's displayed, but also the .checked value of the item).

Now, if you wanted to use more than two states (ie. use more than the first two indices in the image list), you would add additional images to the image list, set the .Checkboxes property to false, and then do your own manual checking during mouse events to see if it was the graphic that was clicked, and if so, change the state. But if all you want to do is change the checkboxes to a custom two-state graphics set, you can just skip this paragraph. :)

03 March 2008, 11:39 AM
Thanks John,

That is really fantastic, and works like a charm. This Dot Net stuff is very very very slowly starting to make sense, but it is slow going. Great to have something vaguely respectable implemented though! ;)

Thanks for your time mate,


CGTalk Moderation
03 March 2008, 11:39 AM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.