Results 1 to 9 of 9

Thread: [RESOLVED] Really simple ComboBox question

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Apr 2005
    Posts
    248

    Resolved [RESOLVED] Really simple ComboBox question

    Hi all,

    I've tried loads of different properties, and search around on Google, but with no luck .

    How do you select/change an item in a ComboBox?

    Say you had a combobox with the values

    1
    2
    3
    4
    5

    How would you make the box display "3" (say, by clicking a button)?

    Thanks

  2. #2
    Frenzied Member
    Join Date
    Aug 2005
    Posts
    1,042

    Re: Really simple ComboBox question

    Arby:

    I think you need to give us a little more information such as how you are populating your combobox, where is the informatiion coming from, a database, an array, a CSV file, or ???

    If you have any code you have written for this, that might also be helpful.

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Apr 2005
    Posts
    248

    Re: Really simple ComboBox question

    Ah, it's just a simple one. For example, imagine a form with one combobox "Combo1", a button "Command1":

    Code:
    Private Sub Form_Load()
    Combo1.additem(A)
    Combo1.additem(B)
    End Sub
    
    Private Sub Command1_Click()
    Combo1.[Something](1)
    End sub
    Where "[Something]" is the property that I'm looking for.
    Basically, the code is for when the form closes, just before Unload (FormName), I want to reset all of the comboboxes to their default values .

  4. #4
    Frenzied Member
    Join Date
    Aug 2005
    Posts
    1,042

    Re: Really simple ComboBox question

    Arby:

    Well, I'm still not sure I understand what you need, but you might try this code:


    VB Code:
    1. Private Sub Command1_Click()
    2.   Combo1.Text = Combo1.List(Combo1.ListIndex)
    3. End Sub

    I'm not sure if that is what you need.???

  5. #5
    Hyperactive Member binilmb's Avatar
    Join Date
    Nov 2005
    Location
    Kochi
    Posts
    472

    Re: Really simple ComboBox question

    try this..
    VB Code:
    1. Combo1.Text="3"
    ßįňįl

  6. #6
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,853

    Re: Really simple ComboBox question

    This may help you.
    VB Code:
    1. Option Explicit
    2.  
    3. Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, _
    4.                                                                         ByVal wMsg As Long, _
    5.                                                                         ByVal wParam As Long, _
    6.                                                                         lParam As Any) _
    7.                                                                         As Long
    8.  
    9. Private Const CB_ERR = (-1)
    10. Private Const CB_FINDSTRING = &H14C
    11. Private Const CB_FINDSTRINGEXACT = &H158
    12.  
    13. Private Sub Command1_Click()
    14.  Dim Strpos As Long
    15.  Strpos = FindItemByString(Combo1, "3", True)
    16.  If Strpos > -1 Then
    17.  Combo1.ListIndex = Strpos
    18.  End If
    19. End Sub
    20.  
    21. Private Function FindItemByString(cmb As ComboBox, ByVal SearchFor As String, Optional FindExact As Boolean = False) As Integer
    22.     FindItemByString = CInt(SendMessage(cmb.hwnd, IIf(FindExact, CB_FINDSTRINGEXACT, CB_FINDSTRING), _
    23.                             CB_ERR, ByVal SearchFor))
    24. End Function

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Apr 2005
    Posts
    248

    Re: Really simple ComboBox question

    AIS4U,binilmb, unfortunately both of those give me " 'Text' Property is read-only" errors, or just don't work (if a list item is selected)

    danasegarane - I think that's way more than I need ^_^;;


    I've included a code sample. I'm looking to do something really simple, so you're probably overestimating my needs.

    In the sample, I've included all three solutions provided above, and some commenting. Feel free to play around and remove bits ^_^
    Attached Files Attached Files

  8. #8
    Hyperactive Member binilmb's Avatar
    Join Date
    Nov 2005
    Location
    Kochi
    Posts
    472

    Re: Really simple ComboBox question

    Try This Code...

    VB Code:
    1. For i = 0 To Combo1.ListCount - 1
    2.     If Combo1.List(i) = "ABC" Then Combo1.ListIndex = i
    3. Next
    ßįňįl

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Apr 2005
    Posts
    248

    Re: Really simple ComboBox question

    Ah, thanks!
    I tested that, and figured out from that code that
    Combo1.ListIndex = 0
    Was what I was looking for. Thank you!

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