enable/disable buttons based on ListView selection

B

Brandon McCombs

Hello,

I have a form that contains a listview on the left side and a column of
buttons on the right side. Only some of the buttons do I want enabled
all the time. The other buttons should be enabled only if something is
selected in the listview. That part specifically works but not very
well. It seems that I can only get the buttons to disable if I click off
the text of the items in the listview but still within about 10-20
pixels of the text. If I click way off to the right where there is still
whitespace then I guess it isn't considered a click on the listview and
no event is generated to disable the buttons.

In Java, clicking anywhere on a row in a list would register as an event
on the list item but it seems with C# the boundary for what is
considered the list item seems to be very small (and definitely doesn't
extend across the entire width of the listview). By the way, I only have
1 column in the listview and I'm using the list view type (as opposed to
details or large icons) if that matters. Any tips?

thanks
 
N

Nicholas Paldino [.NET/C# MVP]

Brandon,

To be honest, any comparisons to Java are rather moot, as there is are
no direct analogies between the model for visual controls there, and those
in .NET.

What are the events that you are connecting to in order to determine
when to enable/disable the buttons? Also, what mode are you viewing the
listview in?
 
L

Linda Liu [MSFT]

Hi Brandon,

When a ListView's View property is set to List, LargeIcon, or SmallIcon,
the bound of an item in it limited to the item text. When you click on one
item in a ListView, you should see a dotted line around the item text, and
this is the bound of the item.

Only when you click inside the bound of an item, this item can be selected
and the corresponding events, such as SelectedIndexChanged,
ItemSelectionChanged events can be raised.

This behavior of ListView is by design. If you don't like this limited item
bound, you may use ListBox instead. You can handle the SelectedIndexChanged
event of the ListBox to get notified when different item is selected in the
ListBox.

Hope this helps.
If you have any concerns, please feel free to let me know.


Sincerely,
Linda Liu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
B

Brandon McCombs

Nicholas said:
Brandon,

To be honest, any comparisons to Java are rather moot, as there is
are no direct analogies between the model for visual controls there, and
those in .NET.

The comparisons are only for your reference to help you visualize what
I'm talking about in case I don't give good descriptions.
What are the events that you are connecting to in order to determine
when to enable/disable the buttons? Also, what mode are you viewing the
listview in?

I'm connecting to the click event. I mentioned in the previous post I'm
using the list view of the listview control.
 
B

Brandon McCombs

Linda said:
Hi Brandon,

When a ListView's View property is set to List, LargeIcon, or SmallIcon,
the bound of an item in it limited to the item text. When you click on one
item in a ListView, you should see a dotted line around the item text, and
this is the bound of the item.

I like the List view because I didn't want the items' text to wrap like
it does using Large/SmallIcon view.
Only when you click inside the bound of an item, this item can be selected
and the corresponding events, such as SelectedIndexChanged,
ItemSelectionChanged events can be raised.

Well it doesn't make sense that even though I'm not clicking on the text
(but still within the ListView bounds) that a click event is not being
generated. Why is the item and not the listview itself generating the
event (or so it seems)? If I click in the listview (with no item
previously selected) but not on an item in the list nothing gets
selected but yet if I click in the same spot after something is selected
that item stays selected: it isn't consistent in my opinion. It is
consistent from the point of view of no event being generated to change
the selection state but not from the point of view not having anything
selected if I don't click on an item.

In fact, the proximity to which I have to be to the item to get it be
selected w/o actually clicking on the item itself is not the same
proximity that I need to be within when I click to de-select the item: I
have to click further away from an item to de-select it so that's
another inconsistency.

If the ListView is generating the events then I shouldn't have to always
click on an item in order for a click event to be generated. Not
clicking on the item would show SelectedItems.Count to be = 0 and
clicking on an item would show SelectedItems.Count to be = 1, simple.

I'll try the ListBox. Maybe it will be more sane.
 
L

Linda Liu [MSFT]

Hi Brandon,

Thank you for your reply.
Why is the item and not the listview itself generating the event (or so it
seems)?

It's indeed the ListView that raises the events, such as
SelectedIndexChanged and ItemSelectionChanged.
Well it doesn't make sense that even though I'm not clicking on the text
(but still within the ListView bounds) that a click event is not being
generated.

In fact, when we click within the ListView bound and no matter whether on
the text or not, system sends the WM_LBUTTONDOWN and WM_LBUTTONUP messages
to the ListView. ListView processes these Windows messages and then
determine whether to raise the Click event depending on the clicked spot.
If I click in the listview (with no item previously selected) but not on
an item in the list nothing gets selected but yet if I click in the same
spot after something is selected that item stays selected.

I don't reproduce this in my test. If no item is currently selected and I
click somewhere in the ListView(not on an item), nothing gets selected. If
an item is selected and I click the same spot within the ListView, the
previous item gets un-selected.
I have to click further away from an item to de-select it so that's
another inconsistency.

In my test, only when the clicked spot is within the item bound, the item
will be selected. On the contrary, only when the clicked spot is out of the
item bound, this item will be un-selected.
Not clicking on the item would show SelectedItems.Count to be = 0 and
clicking on an item would show SelectedItems.Count to be = 1, simple.

Yes, it's true in my test. If there's no item selected, the
ListView.SelectedItems.Count is 0 and if there's one item selected, the
ListView.SelectedItems.Count is 1.

Hope this helps.
If you have any concerns, please feel free to let me know.

Sincerely,
Linda Liu
Microsoft Online Community Support
 
L

Linda Liu [MSFT]

Hi Brandon,

How about the problem now?

If you have any concerns, please feel free to let me know.

Thank you for using our MSDN Managed Newsgroup Support Service!

Sincerely,
Linda Liu
Microsoft Online Community Support
 

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