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 pastebin.com):
- 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).
- 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.
- 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.
- 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.

10 comments:
I had the same problem. The workaround I used was to add
listView.CreateGraphics();
before populating the ListView and ticking the items.
Don't know why it works, but it seems to!
listView.CreateGraphics();
works gr8.............
Thanks.
-Abhijeet
Same problem with ListView_ItemChecked ..
though the difference was ...
ListView_ItemChecked fires everytime I added a new item on ListView
CreateGraphics solved my day...thanx a lot....
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.
Nice advices, thanks for sharing.
I usually use the alt + tab or the windows key + tab to watch the completly window, in linux that doesn't works like this, it just pas.
Thanks
Thanks for posting this, it is sometimes difficult to find all the right functions.
So, I don't really believe it may have success.
A great deal of effective information for me!
That was an interesting piece of information on handwriting analysis. Please post more about graphology. Thank you!
Post a Comment