Hi,
I've created a listbox component which inherits from UltraListView (set with listbox properties).
In my class I've overrided many events such as ItemSelectionChanging, ItemCheckStateChanging etc.. in order to achieve my goals.
However I have a problem, and let's take this simple example: when a new Item is active, which means I've clicked on it, I change his color and modify its checkstate.
When my listbox gets focus and when I click on an item, the first Item of the list is processed too, because it seems to be active since the beginning my listbox has focus...After that there is no other problem of this kind, this just happens for the first selection!
Any idea to prevent the first Item of the listview from being Active ?
Or any idea of the problem if I'm wrong ?
After further researches, It seems each time the listbox is refreshed or gets focus I don't know, the OnItemActivating event is raised with the first list item as argument...
What is the event which is raised when you click on an item?
So, why is OnItemActivating everytime raised ?
I give you a snippet of my component's events I've modified:
protected override void OnItemActivating(ItemActivatingEventArgs e) { base.OnItemActivating(e); if (e.Item != null) { if (e.Item.CheckState == CheckState.Unchecked) { e.Item.CheckState = CheckState.Checked; } else if (e.Item.CheckState == CheckState.Checked) { e.Item.CheckState = CheckState.Unchecked; } } }
protected override void OnItemCheckStateChanging(ItemCheckStateChangingEventArgs e) { base.OnItemCheckStateChanging(e); if (e.Item.CheckState == CheckState.Unchecked) { e.Item.Appearance.BackColor = Color.FromArgb(188, 215, 254); e.Item.Appearance.ForeColor = Color.Black; } else if (e.Item.CheckState == CheckState.Checked) { e.Item.Appearance.BackColor = Color.Transparent; e.Item.Appearance.ForeColor = Color.Black; } }
protected override void OnItemSelectionChanging(ItemSelectionChangingEventArgs e) { base.OnItemSelectionChanging(e); if (e.SelectedItems.Count > 0) { if (e.SelectedItems.First.CheckState == CheckState.Checked) { this.ItemSettings.SelectedAppearance.BackColor = Color.FromArgb(188, 215, 254); this.ItemSettings.SelectedAppearance.ForeColor = Color.Black; } else if (e.SelectedItems.First.CheckState == CheckState.Unchecked) { this.ItemSettings.SelectedAppearance.BackColor = Color.Transparent; this.ItemSettings.SelectedAppearance.ForeColor = Color.Black; } } }
I've get arround my problem by stopping using the On ItemActivating event, which I still don't understand the goal by the way :)
I've grouped everything in both OnItemCheckStateChanged and OnItemSelectionChanged events..
I have another asking, I would like to deal with elements already selected, and I had found a post in this forum which was giving a good solution, adding a "Mark" property to the item. But impossible to retrieve it, does anywone know where this thread is ??
Thanks a lot it would be very helpful !