Solved

Workaround for wierd ListView behavior

Posted on 2006-11-25
7
341 Views
Last Modified: 2010-04-16
Hi,

I am looking for a workaround for a wierd behavior, possibly a bug, that I am encountering with the .Net 2.0 ListView control. For some reason, when I have a list view with MultiSelect = false and I set the selected item before adding the ListView to a container control, the selected item gets cleared and then reset when the ListView is added to the container. This causes a problem because I am using the SelectedIndexChanged event to update an underlying data item, and it is causing unnecessary updates to occur. This can easily be reproduced in a clean project by creating a form and putting the following code in it.

public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            ListView lv = new ListView();

            lv.MultiSelect = false;
            lv.Columns.Add("Test");
            lv.Items.Add("Test");
            lv.Items[0].Selected = true;

            lv.SelectedIndexChanged += ListView_SelectedIndexChanged;

            this.Controls.Add(lv);
           
        }

        private void ListView_SelectedIndexChanged(object sender, EventArgs e)
        {
            System.Diagnostics.Debug.Print("SelectedCount = {0}", ((ListView)sender).SelectedItems.Count);
        }
    }

Notice that if you run it, the event is fired 3 times, and the count is 1, then 0, then 1 again. All events are fired when the this.Controls.Add(lv) line is run.

Is there any way to work around this behavior other than to avoid setting the selected item until after the ListView is added to the container?

Thanks,
Zaphod.
0
Comment
Question by:Z_Beeblebrox
  • 4
  • 2
7 Comments
 
LVL 7

Author Comment

by:Z_Beeblebrox
ID: 18013305
I found one workaround, it seems that OnCreateControl is not called until after the listview is done messing around with the selected item, so by ignoring all selected index changed events until after that is called, I can get the behavior I am looking for. Anyone have a better way?

Thanks,
Zaphod.
0
 
LVL 11

Accepted Solution

by:
Expert1701 earned 125 total points
ID: 18013394
Why couldn't you just change the order?

i.e.:

  this.Controls.Add(lv);
  lv.Items[0].Selected = true;
  lv.SelectedIndexChanged += ListView_SelectedIndexChanged;
0
 
LVL 7

Author Comment

by:Z_Beeblebrox
ID: 18013397
Hi Expert1701,

Unfortunately, I can't really change the order, the control is being added to the container far away from the initialization code (the ListView is contained inside a UserControl), and it doesn't seem right for the code that is doing the adding to call something after it has done the adding so that the initialization can happen.

Thanks for the comment,
Zaphod.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 10

Expert Comment

by:dkloeck
ID: 18014227
So what can you change? where can we add code?
0
 
LVL 10

Assisted Solution

by:dkloeck
dkloeck earned 125 total points
ID: 18014266
you could change it to single selection after this.Controls.Add(lv);
then the event would only happen once
0
 
LVL 7

Author Comment

by:Z_Beeblebrox
ID: 18014669
Hi dkloeck,

I am inheriting from the ListView, so basically I can either change the behavior of the list view from within it, or change the design time properties, or respond to an event from the ListView.

I could change it to single selection afterwards, but I would need a place to do that. Right now, I only know of the OnCreateControl method.

Thanks,
Zaphod.
0
 
LVL 7

Author Comment

by:Z_Beeblebrox
ID: 18098951
Thanks for the input, in the end I went with the OnCreateControl method.

Zaphod.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
bulid json format 3 46
Sum Column in GridView 3 44
Performance  Html.BeginForm vs jQuery 3 50
Syntax Error 2 47
Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now