ListView vs ListBox

J

Joe Cool

I have an app I am working on where I was using a ListBox to display a
list of items on a form. I decided I'd rather the control be a
ListView. But something that worked on the ListBox doesn't work the
same way as a ListView and I cannot find a way to overcome it.

The ListBox had a context menu and the form had a couple of command
buttons that operated on the selected item in the list. The context
menu items use the same click event as the command buttons. The
context menu items and command buttons are only enabled if an item was
selected. After clicking on one of the context menu items or the
corresponding command button, and the action in the click event is
processed, if the item still existed in the list, it was still
selected.

However, the ListView acts differently.

Remember, the context menu items link to the same click event handlers
as the buttons.

If you perform an action via the context menu, the item(s) selected
remain selected. If you perform the action via the command button, all
items are de-selected. EVEN THOUGH THE SELECTED PROPERTY IS STILL
TRUE!!!

Setting the Item's Selected property does not help.

I hope someone with more experiance than I can help!!!

TIA.
 
J

Joe Cool

I have an app I am working on where I was using a ListBox to display a
list of items on a form. I decided I'd rather the control be a
ListView. But something that worked on the ListBox doesn't work the
same way as a ListView and I cannot find a way to overcome it.

The ListBox had a context menu and the form had a couple of command
buttons that operated on the selected item in the list. The context
menu items use the same click event as the command buttons. The
context menu items and command buttons are only enabled if an item was
selected. After clicking on one of the context menu items or the
corresponding command button, and the action in the click event is
processed, if the item still existed in the list, it was still
selected.

However, the ListView acts differently.

Remember, the context menu items link to the same click event handlers
as the buttons.

If you perform an action via the context menu, the item(s) selected
remain selected. If you perform the action via the command button, all
items are de-selected. EVEN THOUGH THE SELECTED PROPERTY IS STILL
TRUE!!!

Setting the Item's Selected property does not help.

I hope someone with more experiance than I can help!!!

TIA.

Crap, I do this a lot. Ten minutes after posting a "help me" message,
I figure it out. All I had to do was select the ListView at the end of
the menu/button click event.
 
B

Ben Voigt [C++ MVP]

If you perform an action via the context menu, the item(s) selected
remain selected. If you perform the action via the command button, all
items are de-selected. EVEN THOUGH THE SELECTED PROPERTY IS STILL
TRUE!!!

Setting the Item's Selected property does not help.

I hope someone with more experiance than I can help!!!

They aren't deselected, the selection highlight just isn't painted unless
the ListView has the focus. Tabbing a few times until the ListView has
focus again will prove this to be true.

There's a AlwaysShowSelected or HideSelection or something like that sort of
property to fix this.
 

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