Monday, November 06, 2006

TabControl, ListView and checking list items

Today I found an odd feature with the ListView GUI control. The list view must be the king of Windows controls. I mean, what can't it do? It can display lists in 5 different ways, which is increasing as Windows evolves. It can even display a list with check boxes for each item, very similar to the CheckedListBox. But the ListView behaves very differently when it comes to checking and unchecking list items if it is placed in a TabControl.

The event ItemCheck should be called when the user or code checks an item in the list. But the oddity I found was that the ItemCheck event method would NOT be called when code/user checks a list item, but the event is first called when the user clicked on the tab that contained the control.

This is what I did (
Full source code available at
  • Created a Form with a ListBox, TabControl and a ListView
  • The TabControl contains 3 tab pages, the initial tab with a button, the second tab with a second ListView and the third tab page contained a CheckedListBox.
  • Added event listeners for all three lists that would add text into the ListBox to show what happens and when.
  • When pressing the button, it will check the first item in all lists (the two ListViews and the CheckedListBox).
This is what I found:
  • When the button was clicked, it would call the ItemCheck method on the CheckedListBox and the ListView outside the tab control. It would NOT call the ItemCheck method on the ListView inside tab page 2. (Form screen shot)
  • When I clicked on tab page 2, then it would call the ItemCheck method on the ListView inside that tab page. (Form screen shot). Somehow the ListView remembered that I wanted some of the items checked.
Now we have two differently behaviours depending if a ListView is inside a tab control or not.
  1. If a ListView is outside a tab control it will call the ItemCheck method when the property is changed. But if the ListView is inside a tab control (that isn't visible) it will not call the ItemCheck method when the property is changed.
  2. A CheckedListBox inside a tab control (not visible) will call the ItemCheck method when the property is changed; and the ListView will not call the ItemCheck method.
In our case I solved it by setting a boolean to false in the TabControl_SelectedIndexChanged() event, and then setting it to true in the TabControl_Selected() event. This way I can check from the boolean if the ItemCheck() comes from the user or is cached by the ListView. If the boolean is true, then I know the user checked a list item.

C'mon, which is the correct behaviour?
Why is there a need for a different behaviour?
Why do I have to bother?

Perhaps I didn't know that it is common GUI-OO design to have one implementation if a control is in a TabControl and one if it isn't.


Anonymous said...

I had the same problem. The workaround I used was to add


before populating the ListView and ticking the items.

Don't know why it works, but it seems to!

abhijeet said...

works gr8.............

nullstring said...

Same problem with ListView_ItemChecked ..

though the difference was ...

ListView_ItemChecked fires everytime I added a new item on ListView

tinu said...

CreateGraphics solved my day...thanx a lot....

Anonymous said...

RedSolo's approach is better than calling ListView.CreateGraphics().

The behaviour is what it is to increase performance, i.e. reduce the time required to load the form or control containg the tab page hosting the listview.

Generic Cialis said...

Thanks for posting this, it is sometimes difficult to find all the right functions.

escort madrid said...

So, I don't really believe it may have success. said...

A great deal of effective information for me!

cialis for women said...

That was an interesting piece of information on handwriting analysis. Please post more about graphology. Thank you!

Agen Bola Sbobet said...

Great post! I?m just starting out in community management/marketing media and trying to learn how to do it well - resources like this article are incredibly helpful. As our company is based in the US, it?s all a bit new to us. The example above is something that I worry about as well, how to show your own genuine enthusiasm and share the fact that your product is useful in that case. Agen Sbobet Agen Ibcbet

Casino Online said...

the internet is with a doubt growing into the most important medium of communication across the globe and its due to sites like this that ideas are spreading so quickly. 338A Bola Tangkas

Agen Bola Online said...

I'm now not sure the place you are getting your information, but good topic. I must spend a while finding out more or understanding more. Thank you for fantastic information I used to be searching for this info for my mission. Judi Bola Online

Atul Chandola said...

Packers and Movers in Pulikeshi Nagar
Packers and Movers in Queens Road
Packers and Movers in Raghavendra Nagar
Packers and Movers in Rajaji Nagar
Packers and Movers in Raja Rajeshwari Nagar
Packers and Movers in Richmond Town
Packers and Movers in Sadashiva Nagar
Packers and Movers in Sahakara Nagar
Packers and Movers in Sampangiram Nagar
Packers and Movers in Sanjay Nagar
Packers and Movers in Saraswathipuram
Packers and Movers in SM Road
Packers and Movers in Srinivas Nagar