Z_Beeblebrox
asked on
Workaround for wierd ListView behavior
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_SelectedIndexChan ged;
this.Controls.Add(lv);
}
private void ListView_SelectedIndexChan ged(object sender, EventArgs e)
{
System.Diagnostics.Debug.P rint("Sele ctedCount = {0}", ((ListView)sender).Selecte dItems.Cou nt);
}
}
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.
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_SelectedIndexChan
this.Controls.Add(lv);
}
private void ListView_SelectedIndexChan
{
System.Diagnostics.Debug.P
}
}
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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.
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.
So what can you change? where can we add code?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.
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.
ASKER
Thanks for the input, in the end I went with the OnCreateControl method.
Zaphod.
Zaphod.
ASKER
Thanks,
Zaphod.