How to toggle select/deselect background colour and state on mouse click.

1 Answer 400 Views
ListView
Clint
Top achievements
Rank 1
Iron
Iron
Iron
Clint asked on 14 Sep 2021, 03:38 PM

I am currently changing the background row on my RadListView, when click selected via the visual item formatting event like this:


        private static void RadListView_VisualItemFormatting(object sender, ListViewVisualItemEventArgs e)
        {

            if (e.VisualItem.Selected)
            {
                e.VisualItem.BackColor = Color.Red;
            }
            else
            {
                e.VisualItem.ResetValue(VisualElement.BackColorProperty, ValueResetFlags.None);
            }
        }

This works great when I click on a row to select it. It does change to a red background.

If you click on another row that is not selected, the state does appear to change with the old row reverting back and the new row changing to a red background.

However if you click the same row, the one you just selected, nothing appears to happen.

How can you code up a toggle of a given row's selection state, if you click it once to select and click it again to deselect?

1 Answer, 1 is accepted

Sort by
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 16 Sep 2021, 10:16 AM

Hi, Clint,

Your question has already been answered in the support ticket (ID 1535578) you have opened on the same topic. However, I am posting the answer here as well in order the community to benefit from it.

When you click an item in RadListView for the first time, it gets selected. Then, when you again on this item, the editor is being activated. The item still remains selected. If you want the click over an already selected item to deselect it, it is necessary to handle the ItemMouseDown and ItemMouseUp events and check whether the clicked item was already selected before processing mouse down/up.

I have prepared a sample code snippet for your reference which result is illustrated in the gif file:

        public RadForm1()
        {
            InitializeComponent();
            this.radListView1.AllowEdit = false;
            this.radListView1.VisualItemFormatting += radListView1_VisualItemFormatting;
            for (int i = 0; i < 20; i++)
            {
                this.radListView1.Items.Add("Item" + i);
            }

            this.radListView1.ItemMouseDown += radListView1_ItemMouseDown;
            this.radListView1.ItemMouseUp += radListView1_ItemMouseUp;
        }

        bool IsSelected = false;

        private void radListView1_ItemMouseDown(object sender, ListViewItemMouseEventArgs e)
        {
            IsSelected = e.Item.Selected;
        }

        private void radListView1_ItemMouseUp(object sender, ListViewItemMouseEventArgs e)
        {
            if (e.Item.Selected == IsSelected)
            {
                this.radListView1.SelectedItem = null;
            }
        }

        private void radListView1_VisualItemFormatting(object sender, ListViewVisualItemEventArgs e)
        {
            if (e.VisualItem.Selected)
            {
                e.VisualItem.BackColor = Color.Red;
            }
            else
            {
                e.VisualItem.ResetValue(VisualElement.BackColorProperty, ValueResetFlags.Local);
            }
        }

We kindly ask you to use just one thread for a specific problem to contact us. Posting the same questions numerous times slows down our response time because we will need to review and address two or more tickets instead of one. Moreover, threads are handled according to license and time of posting, so if it is an urgent problem, we suggest you use a support ticket, which would be handled before a forum thread.

Thank you for your understanding.

Regards,
Dess | Tech Support Engineer, Principal
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Clint
Top achievements
Rank 1
Iron
Iron
Iron
commented on 16 Sep 2021, 02:38 PM

Nice. I see what you did there. Using the mouse down event to interrogate each item object, seeing if it's selected and saving the state of that item to a common variable, so you can use it to compare with other item click events to see what was the last saved state.

Well done, very streamlined solution. Apologies for the double post, it's been a frantic time trying to beat a crazy deadline with this winform project, when I'm actually a React/Angular web developer.
Tags
ListView
Asked by
Clint
Top achievements
Rank 1
Iron
Iron
Iron
Answers by
Dess | Tech Support Engineer, Principal
Telerik team
Share this question
or