CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Sep 2004
    Location
    South Africa
    Posts
    135

    Question Disable some entries in Combo box

    I am loading a combo box with entries from a data base and based on certain conditions, I would like to disable some of the entries in the combo box so that the user may not click on these options. How do I disable some of the entries in the combo box??
    Last edited by nthaby; May 24th, 2007 at 07:55 AM. Reason: remove bold in title

  2. #2
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,725

    Re: [bold]Disable some entries in Combo box[/bold]

    I think, you can't.
    Entries in a combo or a listbox are accessed as cbo.List(n) which retrieves the item string. The only property of these items is if they are selected or not. This property is accessed as cbo.Selected(n).
    An 'Enabled' property does not exist. You have to deal with that by software.

  3. #3
    Join Date
    Sep 2004
    Location
    South Africa
    Posts
    135

    Re: Disable some entries in Combo box

    Thank you for your response. In other words, I can't even change colours of certain entries in the combo box?? This is because I thought that if its not possible to disable some entries, then I might change the colour to white so that it may not be visible to the user.. Would you or anyone have other suggestions of how to work around this problem??

    Quote Originally Posted by WoF
    I think, you can't.
    Entries in a combo or a listbox are accessed as cbo.List(n) which retrieves the item string. The only property of these items is if they are selected or not. This property is accessed as cbo.Selected(n).
    An 'Enabled' property does not exist. You have to deal with that by software.

  4. #4
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,725

    Re: [bold]Disable some entries in Combo box[/bold]

    The problem is, you require a combo-box.
    The ListView control from 'Microsoft Windows Common Controls 6.0' provides extensive item properties, but has no dropdown feature...

    There are a couple of custom listbox controls available freely in the internet. Try a search with "custom combobox control"

  5. #5
    Join Date
    Jun 2005
    Location
    JHB South Africa
    Posts
    3,772

    Re: Disable some entries in Combo box

    Just a quick simple thought...

    Why not remove the items from the list...
    Code:
    Combo1.RemoveItem ([ItemIndex])
    Just remember to go through the list backwards..

    Gremmy..
    Articles VB6 : Break the 2G limit - Animation 1, 2 VB.NET : 2005/8 : Moving Images , Animation 1 , 2 , 3 , User Controls
    WPF Articles : 3D Animation 1 , 2 , 3
    Code snips: VB6 Hex Edit, IP Chat, Copy Prot., Crop, Zoom : .NET IP Chat (V4), Adv. ContextMenus, click Hotspot, Scroll Controls
    Find me in ASP.NET., VB6., VB.NET , Writing Articles, My Genealogy, Forum
    All VS.NET: posts refer to VS.NET 2008 (Pro) unless otherwise stated.

  6. #6
    Join Date
    Sep 2006
    Posts
    635

    Re: Disable some entries in Combo box

    Quote Originally Posted by nthaby
    I am loading a combo box with entries from a data base and based on certain conditions, I would like to disable some of the entries in the combo box so that the user may not click on these options. How do I disable some of the entries in the combo box??

    this may help little bit more
    Code:
    'where c is ComboBox
    
    option explicit
    Dim CllLocked As New Collection
    Const IsLocked  As Boolean = True
    
    Private Sub c_Click()
    Dim indexCombo As Integer
    Dim keyColl As String
    indexCombo = c.ListIndex
    keyColl = "key" & CStr(indexCombo)
    If CllLocked.Item(keyColl) = IsLocked Then
       MsgBox "Index is Locked"
    End If
    End Sub
    
    Private Sub Form_Load()
    dim i as integer
       For i = 1 To 10
    	  c.AddItem i
    	  CllLocked.Add IIf(i Mod 2 = 0, IsLocked, Not IsLocked), "key" & CStr(i - 1)
       Next
    End Sub

  7. #7
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: [bold]Disable some entries in Combo box[/bold]

    Just use a FlexGrid. You can change the color of each cell!
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  8. #8
    Join Date
    Jun 2004
    Location
    Kashmir, India
    Posts
    6,808

    Re: [bold]Disable some entries in Combo box[/bold]

    Or you will have to write your own control (owner drawn control).

    Why don't you just remove the items from the combobox if they are not required. You can add them again when you need them.

  9. #9
    Join Date
    Sep 2004
    Location
    South Africa
    Posts
    135

    Smile Re: [b]Disable some entries in Combo box[/b]

    Thanks to everybody who have contributed, I will try out all your suggestions and let you know how if it worked..

  10. #10
    Join Date
    Sep 2005
    Location
    Delhi, INDIA
    Posts
    237

    Wink Re: [b]Disable some entries in Combo box[/b]

    Dear nthaby,

    here i have make some changes in the code given by hensa22, so that the user can't access the disabled selection and index reset to the previous valid seletion, so that c.text propperty also not usable by the user in case of disable selection!

    Code:
    Option Explicit
    Dim CllLocked As New Collection
    Const IsLocked  As Boolean = True
    Dim lastIndex As Integer
    Private Sub c_Click()
    Dim indexCombo As Integer
    Dim keyColl As String
    indexCombo = c.ListIndex
    keyColl = "key" & CStr(indexCombo)
    If CllLocked.Item(keyColl) = IsLocked Then
       MsgBox "Index is Locked"
       c.ListIndex = lastIndex
    Else
    	lastIndex = c.ListIndex
    End If
    End Sub
    Private Sub Form_Load()
    Dim i As Integer
       For i = 1 To 10
    	  c.AddItem i
    	  CllLocked.Add IIf(i Mod 2 = 0, IsLocked, Not IsLocked), "key" & CStr(i - 1)
       Next
    End Sub
    Quote Originally Posted by nthaby
    Thanks to everybody who have contributed, I will try out all your suggestions and let you know how if it worked..
    And hensa22 , ur code was so good...thanks for sharing with us!
    I'M BACK AGAIN !!
    -------------------------------------------------------------------------
    enjoy the VB !
    If any post helps you, please rate that.
    Always try to findout the Solutions, instead just discussing the problem and its scope!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured