0

hi all,
I have a piece of code, I can delete the first value but fails and give notice
InvalidArgument=Value of '0' is not valid for 'index'.
Parameter name: index

Private Sub ComboBox1_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles ComboBox1.KeyDown
     If ComboBox1.Items.Count > 0 Then
         If e.KeyCode = Keys.Delete Then
             ComboBox1.Items.Remove(ComboBox1.SelectedItem)
             'or
             'ComboBox1.Items.RemoveAt(ComboBox1.SelectedIndex)
         End If
     End If
 End Sub

thank help !

2
Contributors
3
Replies
15
Views
3 Years
Discussion Span
Last Post by oussama_1
0

this one works and wont give any error if the item is not selected

ComboBox1.Items.Remove(ComboBox1.SelectedItem)

but this one will give you "InvalidArgument=Value of '-1' (not 0) is not valid for 'index'" if the item is not selected

ComboBox1.Items.RemoveAt(ComboBox1.SelectedIndex)
0

I declare If ComboBox1.SelectedIndex > 0 Then
Can not delete the first value

Edited by fugio

0

a zero index actually can contain an item you should change your code to this
If ComboBox1.SelectedIndex > -1 Then

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.