Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
460
Prevent first Item in list from becoming active
posted

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 ?

 

  • 69832
    Suggested Answer
    Offline posted

    The control automatically activates the first visible item when the control is entered, in emulation of Windows Explorer. You can prevent this by overriding OnEnter, setting a flag before the base implementation is called, and clearing it thereafter. Then, in OnItemActivating, if that flag is true, set the Cancel property of the event arguments to true.

  • 460
    posted

    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;
                    }

                }
            }