1

I have a listview in a WinForms Form that can have 0 to 100 items and a groupbox that interacts with the currently selected item of the listview. I want to set the enabled property of the groupbox to false as soon as there is no item selected in the listview (for example if there are no items or the user clicked into a free space in the listview, unselecting the current item). I know that I could use a timer checking the Count of the selected items array but I'd like to use an event. The SelectedIndexChanged event of the listview didn't work for me. I only need an event for when there is no item selected.

Thanks for your help.

This is my SelectedIndexChanged method (that obviously won't work):

        if (lstPINs.SelectedItems.Count == 0)
            MessageBox.Show("Test"); // I'd disable the groupbox and return here, but this dosn't work!

        int index = PINMGR.GetIndex(lstPINs.SelectedItems[0].Text);
        string pin = PINMGR.GetPIN(index);

        txtEditPIN1.Text = pin.Substring(0, 4);
        txtEditPIN2.Text = pin.Substring(5, 4);
        txtEditPIN3.Text = pin.Substring(10, 4);
        txtEditPIN4.Text = pin.Substring(15, 4);

        int balance = PINMGR.GetBalance(index);

        // Unimportant math stuff

EDIT: The HideSelection Property is set to false, so the items will remain selected, even if the list doesn't have focus anymore. This is no problem.

Jan H.
  • 41
  • 8
  • Winforms or web? Post your `SelectedIndexChanged` event handler and we will fix it. – Sinatr Feb 19 '15 at 11:24
  • @Sinatr WinForms. I added the code, thanks for your help! – Jan H. Feb 19 '15 at 11:30
  • If you select an item, clearly SelectedItems.Count will be over 0 so of course the message box doesn't show – cbr Feb 19 '15 at 11:39
  • @GrawCube I know, because of this I asked the question. I just posted the code, because Sinatr wanted to see it. – Jan H. Feb 19 '15 at 11:41
  • *"for example if there are no items or the user clicked into a free space,"* This sounds like a job for the `Leave` event, which is called when the input focus leaves the control. – cbr Feb 19 '15 at 11:46
  • @GrawCube Thanks for your help! `Leave` doesn't work since it is activated as soon as you click anything else. But I have the `HideSelection` property set to false, so the selected item will remain selected while you interact with it in the groupbox. With "free space" I meant a spot without an item in the listview. So basicly I need an event, that doesn't activate when you leave but when the `listview.SelectedItems.Count` property goes from 1 to 0 – Jan H. Feb 19 '15 at 11:55
  • `SelectedItems` may not work if you are using `ListView` in virtual mode. But you can simply check `SelectedIndices.Count` (which will work in any case). Also, maybe `lstPINs` is a wrong `ListView` try to use `((ListView)sender)` instead. It's all speculation (as well as guess what maybe you forgot to subscribe `ListView` to that event), you can simply debug and tell us if even handler is ever called and what exactly is not working. Maybe you are simply getting [NullReferenceException](http://stackoverflow.com/q/4660142/1997232). – Sinatr Feb 19 '15 at 12:22
  • @Sinatr Thank you!! `SelectedIndices.Count` worked! One should note, that this doesn't activate if you do ListView.Items.Clear(), but I can work around that. – Jan H. Feb 19 '15 at 12:48

0 Answers0