[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 314
  • Last Modified:

Limit to list on a combo box

How can i make it impossible for a user to type in their own text in a combo box. I only want them to be able to click to enter a given choice. Access has a limit to list  option what do I do for VB 6.0??? Another thing, I would like to do is to allow the user to type in the first letter and the list would move to that word if possible.
0
techconcepts
Asked:
techconcepts
  • 4
  • 3
  • 2
  • +2
1 Solution
 
Éric MoreauSenior .Net ConsultantCommented:
Set the Style property to "Dropdown list". It will change the combo behavior to respect both conditions.
0
 
CraigLazarCommented:
to do a search on the list like you want use the following code

Public Function cboSelString(pComboListBox As Object) As Long
'this function works like a quick fill in Quicken. This function is called
'in a change event of a combobox and finds the item in the list based on the
'first characters that the user types. If it finds a match, the user's input
'is left unhighlighted while the remaining characters are highlighted.

   Dim nIndex As Long
   Dim cSearch As String
   Dim cFound As String
   
   ' get the contents in input box
   cSearch = LTrim(pComboListBox.Text)    

   nIndex = SendMessage(pComboListBox.hwnd, CB_FINDSTRING, -1, _
                        cSearch)

   If (nIndex <> -1) Then           ' -1 means found nothing
      pComboListBox.ListIndex = nIndex 'display the found item
      cFound = pComboListBox.List(nIndex)
      If (Len(cFound) >= Len(cSearch)) Then 'highlight remainder
         pComboListBox.SelStart = Len(cSearch)
         pComboListBox.SelLength = Len(cFound) - Len(cSearch)
      End If
   End If
   cboSelString = nIndex     'returns -1 if not found, else returns index
End Function


this piece of code i prefer and found it works better
    If CSearchSurname.SelLength = 0 Then
        findstring = CSearchSurname.Text & Chr$(KeyAscii)
    Else
        findstring = Left$(CSearchSurname.Text, CSearchSurname.SelStart) & Chr$(KeyAscii)
    End If
    CB = SendMessage(CSearchSurname.hwnd, CB_FINDSTRING, -1, ByVal findstring)
    If CB <> CB_ERR Then CSearchSurname.ListIndex = CB
        CSearchSurname.SelStart = Len(findstring)
        CSearchSurname.SelLength = Len(CSearchSurname.Text) - CSearchSurname.SelStart
       

        srchstr = CSearchSurname
        Data1.Recordset.FindFirst "Surname=" & """" & srchstr & """"
       
    KeyAscii = 0

hope it helps

:)

Craig
0
 
techconceptsAuthor Commented:
I am populating the combobox with the following code...

    Set rs = db.OpenRecordset("select Country from Country")
    Country.Clear
 
    If rs.RecordCount <> 0 Then
        Do While Not rs.EOF
        Country.AddItem rs("Country")
           rs.MoveNext
        Loop
    End If
If I use the style 2 combo list I get an error that it is a read only field. I am not sure how to use the code you provided with this type of method of populating the combo box. Could you take another look at it please?
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
Éric MoreauSenior .Net ConsultantCommented:
Which combobox do you use exactly (there are at least 3 in VB6)?
0
 
TheAnswerManCommented:
Use Style 2-Dropdownlist for combo.

If you are getting an error saying combo1.text = "Jimmy Johnson" then it is not spelled the same as in the list box.
they must be EXACT for it to find it.

no leading ... no trailing spaces.
   

No "Jim Johnson"
No "JimmyJohnson"
No " Jimmy Johnson"
No "Jimmy Johnson "

Dim ls_MyVal as string
ls_MyVal = "Jimmy Johnson "
Combo1.text = Trim$(ls_MyVal)


As to the Finding the First...
Combo will automatically find the first item if the combo has the focus and you press a key....if you have it set to Style = 2 - Dropdownlist

0
 
techconceptsAuthor Commented:
The combobox that I am using is the standard one that appears when you start a standard exe project. TheAnswerman, I tried type 2 and it does work as far as searching the list but with the way I populate and move about in the record I get numerous this is a read only text field. I get it when I move forward and backward thru the recordset. There are times when I need to set the field.clear or field.text="" . I am at a lost for the best way to do this. The combo box's data must be of dynamic data ability (ie. list edited or added to via another form at the time of running the application) such as adding more countries. I am sure your answer is not totally wrong, but it doesnt seem to work with the method of my application. Is there a better combobox that I should be using? or a better way to populate the list?
0
 
techconceptsAuthor Commented:
emoreau,

how would you set the combobox up? Including populating the list? Would you do it as I have shown in the code above? Should I be using a data control to populate instead, and if so, how should I do that?
0
 
Éric MoreauSenior .Net ConsultantCommented:
>If I use the style 2 combo list I get an error that it is a read only field.

This is one of the property that you absolutely have to set at desing time.

I have just started a new project, added a combobox to the form, changed the Style property to 2 and add the following code:
Private Sub Form_Load()
    With Combo1
        .AddItem "1 item"
        .AddItem "2 item"
        .AddItem "3 item"
    End With
End Sub

Make this test and come back with any problems.
0
 
dishaCommented:
use style 2 combobox.
make sure u dont use combo1.text anywhere to set the item since this is not possible if u r using this style. use the additems to add the new item and use list and listindex preferably to retreive the items, avoid using .text property

Regds
0
 
techconceptsAuthor Commented:
could you provide a code snipet to achieve this?
0
 
dishaCommented:
for adding in items use the code as emoreauo....
for retrieving items...use
combo2.list(combo2.listindex) or similiar
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 4
  • 3
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now