0

I want to disable few item in a contextmenu or menustrip when an item in listbox is selected, I manage to do it in a listview control but I can't convert it to listbox, there is no ListItem in listbox control, can i do it using this code, or perhaps an entirely different code, thanks for helping..

Private Sub cIconList_Opening(sender As System.Object, e As System.ComponentModel.CancelEventArgs) Handles cIconList.Opening
    Dim item As ListViewItem = Nothing
    If lvIcon.SelectedIndices.Count = 1 Then
        item = TryCast(lvIcon.SelectedItems(0), ListViewItem)
    End If
    mnuExtractIcon.Enabled = (item IsNot Nothing)
    mnuIconProperties.Enabled = (item IsNot Nothing)
End Sub
mercenary
  • 69
  • 2
  • 11

1 Answers1

1

If you are trying to disable another form object when any ListBox item is selected, just add this to the ListBox's SelectedIndexChanged event.

AnyFormObject.Enabled = (ListBoxObject.SelectedIndex = -1)
Daniel
  • 1,920
  • 4
  • 17
  • 35
  • 1
    I think you meant `> -1` :-) – LarsTech May 14 '14 at 15:51
  • oh, that easy, I will try it – mercenary May 14 '14 at 15:53
  • @LarsTech I realize I originally said "if you wish to enable/disable" which didn't make clear what my code was doing. I changed my description to match the OPs, i.e. when a ListBox Item is selected (index > -1), the object should be disabled. Or, in the case of my code, when a ListBox Item is *not* selected (i.e. = -1), the object should be *enabled*. – Daniel May 14 '14 at 15:54
  • It reads the other way around (to me): `mnuExtractIcon.Enabled = (if item is something)`. – LarsTech May 14 '14 at 16:13
  • @LarsTech As per the OP `I want to disable few item ... when an item in listbox is selected`. So if in the case of your code, you would change it to `mnuExtractIcon.Enabled = Not (if item is something)` – Daniel May 14 '14 at 16:29