[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 :D
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.
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 :).
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:
Private Sub Command1_Click()
Combo1.Text = Combo1.List(Combo1.ListIndex)
End Sub
I'm not sure if that is what you need.???
Re: Really simple ComboBox question
Re: Really simple ComboBox question
This may help you.
VB Code:
Option Explicit
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, _
ByVal wMsg As Long, _
ByVal wParam As Long, _
lParam As Any) _
As Long
Private Const CB_ERR = (-1)
Private Const CB_FINDSTRING = &H14C
Private Const CB_FINDSTRINGEXACT = &H158
Private Sub Command1_Click()
Dim Strpos As Long
Strpos = FindItemByString(Combo1, "3", True)
If Strpos > -1 Then
Combo1.ListIndex = Strpos
End If
End Sub
Private Function FindItemByString(cmb As ComboBox, ByVal SearchFor As String, Optional FindExact As Boolean = False) As Integer
FindItemByString = CInt(SendMessage(cmb.hwnd, IIf(FindExact, CB_FINDSTRINGEXACT, CB_FINDSTRING), _
CB_ERR, ByVal SearchFor))
End Function
1 Attachment(s)
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 ^_^
Re: Really simple ComboBox question
Try This Code...
VB Code:
For i = 0 To Combo1.ListCount - 1
If Combo1.List(i) = "ABC" Then Combo1.ListIndex = i
Next
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!