Community discussion forum

How to disable particular combobox items

Tags: Korea
  • 1 year ago

    hi i'm using vb.net 2003 version.I have added 4 items to the list using collection property.On some condition i need to disable a particular item (say for ex. item no 2) in the combo box list items so that user can't able to select this item.

    pls suggest me the solution.

  • 1 year ago

     

    Hello,

    There is no direct method to disable a particular list item. However, the simplest thing you can try is to

     to store index of the selected item in a variable. In case  if you don't like the change you reset it to the

    previous recorded index. Look at the given code snippet:

    BEGIN CODE

    Dim index As Integer

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    Me.ComboBox1.Items.Add("First")

    Me.ComboBox1.Items.Add("Second")

    Me.ComboBox1.Items.Add("Third")

    Me.ComboBox1.Items.Add("Fourth")

    Me.ComboBox1.Items.Add("Fifth")

    Me.ComboBox1.SelectedIndex = 0

    index = Me.ComboBox1.SelectedIndex

    End Sub

    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles

    ComboBox1.SelectedIndexChanged

    If Me.ComboBox1.SelectedItem = "Fifth" Then

    Me.ComboBox1.SelectedIndex = index

    Else

    index = Me.ComboBox1.SelectedIndex

    End If

    End Sub


    Try the code in a sample application by only dropping a Combo box. I believe this should help.

    Regards,

    Allen

    Allen Smith
    ComponentOne LLC
    www.componentone.com

Post a reply

Enter your message below

Sign in or Join us (it's free).

We'd love to hear what you think! Submit ideas or give us feedback