Results 1 to 5 of 5

Thread: [REsolved] Quick Simple Question on ComboBoxes

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2006
    Posts
    15

    [REsolved] Quick Simple Question on ComboBoxes

    If I have

    cboMyBox.ItemData(4)

    And I want to reference it by

    cboMyBox.itemData([The text of the selected item])

    How can I reference that? It should be easy, but it's giving me a hard time for some reason...

    Thanks

    Shahzad
    Last edited by raverdj; Apr 26th, 2006 at 04:01 PM.

  2. #2
    eltiT resU motsuC Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: Quick Simple Question on ComboBoxes

    u cant directly get it that way

    VB Code:
    1. Private Function GetItemData(sValue As String) As Long
    2.     For x = 0 To cboMyBox.ListCount - 1
    3.         If cboMyBox.List(x) = sValue Then
    4.             GetItemData = cboMyBox.ItemData(x)
    5.             Exit Function
    6.         End If
    7.     Next
    8. End Function
    9.  
    10. Private Sub Command1_Click()
    11.     MsgBox GetItemData("STRING")
    12.    
    13. End Sub
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  3. #3

    Thread Starter
    New Member
    Join Date
    Apr 2006
    Posts
    15

    Re: Quick Simple Question on ComboBoxes

    Thank you, that was perfect, I did make a slight adjustment though... so it could be used globally...


    Code:
    Private Function GetItemData(sValue As String, cboMyBox As ComboBox) As Long
        Dim x As Integer
        
        For x = 0 To cboMyBox.ListCount - 1
            If cboMyBox.List(x) = sValue Then
                GetItemData = cboMyBox.ItemData(x)
                Exit Function
            End If
        Next
    End Function
    
    
    sub command1_click()
            MsgBox GetItemData(cboHuntRRName.Text, cboHuntRRName)
    end sub

  4. #4
    eltiT resU motsuC Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: Quick Simple Question on ComboBoxes

    excellent
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  5. #5
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: [REsolved] Quick Simple Question on ComboBoxes

    Alternatively use API (which will be faster)

    VB Code:
    1. Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" ( _
    2.                 ByVal hWnd As Long, _
    3.                 ByVal wMsg As Long, _
    4.                 ByVal wParam As Long, _
    5.                 lParam As Any) As Long
    6.  
    7. Private Const CB_FINDSTRINGEXACT = &H158
    8.  
    9. Private Sub Command1_Click()
    10.     MsgBox GetItemData(cboHuntRRName.Text, cboHuntRRName)
    11. End Sub
    12.  
    13. Private Function GetItemData(ByVal sFind As String, ByVal cbo As ComboBox) As String
    14.     Dim lIndex As Long
    15.     lIndex = SendMessage(cbo.hWnd, CB_FINDSTRINGEXACT, -1, ByVal sFind)
    16.     If lIndex > -1 Then GetItemData = cbo.ItemData(lIndex)
    17. End Function

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