ListView - SelectedIndexChange event

B

Bob Dankert

I have a form which has a listview and some controls. The controls on the
form are only enabled if there is an item selected in the listview. My
problem is that there is some flicker when changing items in the listview.
I found this is because when I change items, first the item is deselected
(controls are disabled) and the next item is selected (controls are
re-enabled). Is there any way to determine in the selectedindexchanged
event if the selecteditem is in the process of "switching" selected items so
I can ignore the event when it is called to deselect the item?

If this is confusing, I can post more details or a sample.

Thanks,

Bob Dankert
 
S

Sajjad

do you allow user not to select any thing at all,
if thats not the case then you can ignore the even in which you get
SelectedIndexChange event while control dont have any selection.

Sajjad
 
B

Bob Dankert

Unfortunately, the user needs to be able to select nothing at all. It would
be nice if it could be that simple, though :)

Bob
 
J

Jeffrey Tan[MSFT]

Hi Bob,

Thanks for your post.

I suspect that you change other controls' enable state in
ListView.SelectedIndexChanged event. That is you determine if
this.listView1.SelectedIndices.Count >0, if not then there is no selected
items in the ListView and disable all the controls.

I think your program logic is correct. When the user click from one item to
another item, the SelectedIndexChanged will fire 2 times: deselect the
original item and select the new item. However, in your scenario, it seems
that this causes some UI painting performance hit, so some flicker occurs
in your application.

For this issue, I think we can handle ListView.MouseDown event, this event
will fire before SelectedIndexChanged event. In this event, we can use
listView1.GetItemAt to determine if the mouse click position falls into
certain item. If not, we can just disable the controls, else, we can enable
them again. Like this:
private void listView1_MouseDown(object sender,
System.Windows.Forms.MouseEventArgs e)
{
ListViewItem lvi=this.listView1.GetItemAt(e.X, e.Y);
if(lvi==null)
{
this.textBox1.Enabled=false;
this.button1.Enabled=false;
}
else
{
this.textBox1.Enabled=true;
this.button1.Enabled=true;
}

}

Note: Control.Enable property internally will check if the new set value is
different from its original state, if the same value is assigned, it will
do nothing and eliminate the extra work. Like this:
public void set_Enabled(bool value)
{
bool flag1 = this.Enabled;
this.SetState(4, value);
if (flag1 != value)
{
if (!value)
{
this.SelectNextIfFocused();
}
this.OnEnabledChanged(EventArgs.Empty);
}
}

Hope this helps
================================================
Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
B

Bob Dankert

Jeffrey,

Thanks for the help - I was thinking this might be the only method but I was
hoping there might be something simpler. This seems to be working fine
though - thanks!

Bob
 
J

Jeffrey Tan[MSFT]

Hi Bob,

Thanks for your feedback.

Yes, I was hoping either, however, because our program logic need to avoid
redundant state changing, we have to check the mouse position destination
ourselves, then we can have the enough information to only perform
necessary state changing.

Anyway, if you need further help, or have any other concern, please feel
free to tell me, I will work with you. Thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
M

Michael Jackson

What about when the user used the up/down arrow keys to select items?
Michael Jackson
 
G

Guest

Jeffery,

Your example was an excellent starting point for me but I'm still having
trouble. I have a listview that can have anywhere from a few to very many
items, my problem arises when a user clicks and there is no item there. I've
followed your example for the test but during the MouseDown Event I get the
SelectedItems.Count, which is 1 and multi select = false, and my lvi is null.
Next the SelectedIndexChanged event goes off, I do the same
SelectedItems.Count and this time my count is 0. Is this part of the
deselection? Then I'm assuming the SelectedIndexChanged event fires again for
the select portion, which is happening. Then I get one more additional
SelectedIndexChanged event, and the item that I selected originally in the
MouseDown event is not selected. I'm truly at a loss here, any suggestions
would be greatly appreciated.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top