Some questions regarding ListView fundamental behaviour
-
Hi all, Whenever my ListView (let's call it ListView1) selected IDX changes, I wish to grant focus to another ListView (let's call it ListView2), but this is a bit problematic: If I write ListView2.Focus() inside the ListView1.SelectedIndexChanged event handler, ListView1 is focused. In order to explain this behaviour, I'll mention few facts: 1. Mouse click is composed of 2 events: MouseUp and MouseDown. in each of these cases the control receives focus, if he didn't have it already. 2. whenever changing the selected line at a ListView, the event SelectedIndexChanged is called twice: once for removing the selection from the previously selected line, and once for selecting the new line. So, the flow is as followed: 1. starting point: line 1 is selected in ListView1. 2. I'm clicking line 2 of ListView1. 3. Line 1 of ListView1 is unselected, which causes firing the event SelectedIndexChanged of ListView1. Now ListView2 receives focus. 4. Line 2 of ListView1 is unselected, which causes firing the event SelectedIndexChanged of ListView1. ListView2 already has focus. 5. Now ListView1 receives focus, since the MouseUp event (fired due to the MouseClick event). Does anyone have a solution for focusing a control when changing a ListView selected IDX? it also bothers (due to other reasons) that the event SelectedIndexChanged is fired twice for each line selection. Verifying this behaviour can be done by using:
private void listView1_SelectedIndexChanged(object sender, EventArgs e) { System.Threading.Thread.Sleep(500); System.Media.SystemSounds.Beep.Play(); }
When you'll change the selected line, 2 beeps will be heard. Thanks and sorry for the long question! Eyal. -
Hi all, Whenever my ListView (let's call it ListView1) selected IDX changes, I wish to grant focus to another ListView (let's call it ListView2), but this is a bit problematic: If I write ListView2.Focus() inside the ListView1.SelectedIndexChanged event handler, ListView1 is focused. In order to explain this behaviour, I'll mention few facts: 1. Mouse click is composed of 2 events: MouseUp and MouseDown. in each of these cases the control receives focus, if he didn't have it already. 2. whenever changing the selected line at a ListView, the event SelectedIndexChanged is called twice: once for removing the selection from the previously selected line, and once for selecting the new line. So, the flow is as followed: 1. starting point: line 1 is selected in ListView1. 2. I'm clicking line 2 of ListView1. 3. Line 1 of ListView1 is unselected, which causes firing the event SelectedIndexChanged of ListView1. Now ListView2 receives focus. 4. Line 2 of ListView1 is unselected, which causes firing the event SelectedIndexChanged of ListView1. ListView2 already has focus. 5. Now ListView1 receives focus, since the MouseUp event (fired due to the MouseClick event). Does anyone have a solution for focusing a control when changing a ListView selected IDX? it also bothers (due to other reasons) that the event SelectedIndexChanged is fired twice for each line selection. Verifying this behaviour can be done by using:
private void listView1_SelectedIndexChanged(object sender, EventArgs e) { System.Threading.Thread.Sleep(500); System.Media.SystemSounds.Beep.Play(); }
When you'll change the selected line, 2 beeps will be heard. Thanks and sorry for the long question! Eyal.I've always found the ListView to be a very quirky control to work with. The biggest question to answer is why are you using it? If possible, you should work around it by getting rid of the listview and go with the ListBox instead. Also you want to avoid what you do with events. You can call SuspendLayout() (I THINK) you will have to verify in the method description. Basically it suspends the events when changing things around in the control. You can also minimize issues by avoiding too many events. SelectedIndexChange is a good one to get yourself into a hung state just by having one control change another control and having both of them trigger off the change event. In my test I had the selected index change load the second listview with items. I failed to give it focus as well....but I found that I only had the event fire once!
-
I've always found the ListView to be a very quirky control to work with. The biggest question to answer is why are you using it? If possible, you should work around it by getting rid of the listview and go with the ListBox instead. Also you want to avoid what you do with events. You can call SuspendLayout() (I THINK) you will have to verify in the method description. Basically it suspends the events when changing things around in the control. You can also minimize issues by avoiding too many events. SelectedIndexChange is a good one to get yourself into a hung state just by having one control change another control and having both of them trigger off the change event. In my test I had the selected index change load the second listview with items. I failed to give it focus as well....but I found that I only had the event fire once!
You probably get only one SelectedIndexChanged event fired since no item was selected prior to your selection. If an item is selected and then you select different item, the event is fired twice: once for unselecting the first item and once for selecting the second item. I'll try using the SuspendLayout() function as you suggested and see if it can help. As for using ListBox - what makes it better? Doesn't it have less functionality than ListView? I'm using ListView since every item in my application is composed of multiple columns. Thanks a lot for your help! Eyal.
modified on Tuesday, March 11, 2008 4:28 AM
-
You probably get only one SelectedIndexChanged event fired since no item was selected prior to your selection. If an item is selected and then you select different item, the event is fired twice: once for unselecting the first item and once for selecting the second item. I'll try using the SuspendLayout() function as you suggested and see if it can help. As for using ListBox - what makes it better? Doesn't it have less functionality than ListView? I'm using ListView since every item in my application is composed of multiple columns. Thanks a lot for your help! Eyal.
modified on Tuesday, March 11, 2008 4:28 AM
WRONG. I get only one event fired because only one event firing happens when an event gets selected. There is no such thing as an event getting fired twice because the control is 'unslecting the first item' unless you have written some code that issues the event. Regardless of how many times I did the selection, I got only one event. The only time you get multiple events fired is because you've written some bad code that is causing the multiple events to fire. Are you calling an event like Select()? Are you programattically doing something to try and change a selection? Are you bound to other events such as losing focus, our MouseLeave, or anything like that? Are you manually setting focus to something when you shouldn't? No matter how frequently I select an item there is always only one event that is fired. That is the event model in Windows -- not something I've done. The field getting 'unselected'? That is not a seperate event unless you bind yourself to the lost focus event (or some such stupid thing as that) In a Windows Form application the ListBox is far more flexible. You can put any object into the list. You control what is displayed by handling the ToString event. The ListView can only have ListViewItem objects in it. So from that aspect of it the ListView is pitiful. This is the totallity of my code and I get only one event each time I select a new item no matter how many times I do it in a row. And it would be extremely amateurish of me to say I'm getting only one event if I had not done multiple selects in a row. So keep your insults to yourself.
public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void listView1\_SelectedIndexChanged(object sender, EventArgs e) { if (listView1.SelectedIndices.Count.Equals(0)) return; if (listView1.SelectedItems\[0\].Text.Equals("Fish")) LoadFish(); else LoadMammals(); } private void LoadMammals() { listView2.Items.Clear(); listView2.Items.Add(new ListViewItem("Horse")); listView2.Items.Add(new ListViewItem("Human")); listView2.Focus(); } private void LoadFish() { listView2.Items.Clear(); listView2.Items.Add(new ListViewItem("Mantaray")); listView2.Items.Add(new ListViewItem("Lion Fish")); listView2.
-
WRONG. I get only one event fired because only one event firing happens when an event gets selected. There is no such thing as an event getting fired twice because the control is 'unslecting the first item' unless you have written some code that issues the event. Regardless of how many times I did the selection, I got only one event. The only time you get multiple events fired is because you've written some bad code that is causing the multiple events to fire. Are you calling an event like Select()? Are you programattically doing something to try and change a selection? Are you bound to other events such as losing focus, our MouseLeave, or anything like that? Are you manually setting focus to something when you shouldn't? No matter how frequently I select an item there is always only one event that is fired. That is the event model in Windows -- not something I've done. The field getting 'unselected'? That is not a seperate event unless you bind yourself to the lost focus event (or some such stupid thing as that) In a Windows Form application the ListBox is far more flexible. You can put any object into the list. You control what is displayed by handling the ToString event. The ListView can only have ListViewItem objects in it. So from that aspect of it the ListView is pitiful. This is the totallity of my code and I get only one event each time I select a new item no matter how many times I do it in a row. And it would be extremely amateurish of me to say I'm getting only one event if I had not done multiple selects in a row. So keep your insults to yourself.
public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void listView1\_SelectedIndexChanged(object sender, EventArgs e) { if (listView1.SelectedIndices.Count.Equals(0)) return; if (listView1.SelectedItems\[0\].Text.Equals("Fish")) LoadFish(); else LoadMammals(); } private void LoadMammals() { listView2.Items.Clear(); listView2.Items.Add(new ListViewItem("Horse")); listView2.Items.Add(new ListViewItem("Human")); listView2.Focus(); } private void LoadFish() { listView2.Items.Clear(); listView2.Items.Add(new ListViewItem("Mantaray")); listView2.Items.Add(new ListViewItem("Lion Fish")); listView2.
OK, So I've created a new project which has only one ListView with two items, and one event hadler - for SelectedIndexChanged event. Here is the code:
namespace TestListViewSelectedIndexChanged { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void listView1_SelectedIndexChanged(object sender, EventArgs e) { System.Threading.Thread.Sleep(500); System.Media.SystemSounds.Beep.Play(); } } }
The Sleep is ment to hear both beeps. Now, whenever I select item, 2 beeps are heard. What's the explanation for this behaviour if the event only fired once? Pleae note that I do not suspect your knowledge, I'm sure it's bigger than mine. I just want to understand this phenomenon. Thanks, Eyal.