none
remove seleleted items in combobox RRS feed

  • Question

  • I want to remove from a combobox a selected item. for example if I choose the number 3 in the combobox that this number is deleted immediately. here is my code. i need help

    If ComboBox1.Text IsNot "" Then
                    ComboBox1.Items.Remove(ComboBox1.SelectedItem)
                End If
    


    Wednesday, May 30, 2018 1:46 AM

All replies

  • I want to remove from a combobox a selected item. for example if I choose the number 3 in the combobox that this number is deleted immediately. here is my code.

    You haven't indicated what the problem is.  Is there an error?  If so, the message usually indicates the source of the problem.  Or does the code do something other than what you expected?   What does it do, and what did you expect?  See:
    https://docs.microsoft.com/en-us/dotnet/framework/winforms/controls/add-and-remove-items-from-a-wf-combobox

    Wednesday, May 30, 2018 1:59 AM
  • I want to remove from a combobox a selected item. for example if I choose the number 3 in the combobox that this number is deleted immediately. here is my code. i need help

    If ComboBox1.Text IsNot "" Then
                    ComboBox1.Items.Remove(ComboBox1.SelectedItem)
                End If



    Hi Jacob,

    You said that you want to remove the select item in combobox immediately when you select item, you can take a look this code,

    Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
            Dim item As String = ComboBox1.SelectedItem
            If item IsNot Nothing Then
                Dim index As Integer = ComboBox1.SelectedIndex
                ComboBox1.Items.RemoveAt(index)
            End If
    
        End Sub

    But I don't know why you want to selected item from combobox, you can modify the code according to your situation.

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Proposed as answer by Stanly Fan Thursday, May 31, 2018 8:22 AM
    Wednesday, May 30, 2018 6:36 AM
    Moderator
  • Jacob,

    Put in top of your code Option Strict On, then your problem is shown. 

    It is not possible to compare 2 strings on its values with IsNot (a reference compare), this can be

    If ComboBox1.Text <> "" 


    Success
    Cor


    Wednesday, May 30, 2018 9:36 AM