Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Highlighting string in the Combo Box when Clicking on it.

Status
Not open for further replies.

zevw

MIS
Jul 3, 2001
697
US
I have seen many different posts on a similar issue. But it does not seem to work.

How can I highlight the complete string in the combo box when I click on it so that it overwrites the previous string. When I click it might highlight one word but not all the words.

I tried Sendkeys "{HOME}+{HOME}" doesn't work.

I tried SelStart and SelLength it did not work.

Please help me with the code, and in which event procedure should I write it.

Thanks!
 
a double click should select all of the text. < normal behavior

HTH << MaZeWorX >> "I have not failed I have only found ten thousand ways that don't work
 
It will only select one of the words.

For Example

Jane Mary Smith

Will select one of the 3 words whichever you double click on but not the full name.
 
in the On Mouse Down event and the On Enter event of the combo box

add the following adjusting for your combo name

Code:
     cboLoadTypeID.SelStart = 0
     cboLoadTypeID.SelLength = Len(cboLoadTypeID.Column(1))

Let me explain; the length of the combo is not necessarily the length of the display data. The control is bound to a field not always whats displayed. in this case its bound to an ID field so if i check the Len((cboLoadTypeID.Value) then it returns 1 however I'm displaying column 2 to the user so i need to reference column 2 when I'm checking the length. since since we reference from 0 then column 2 is actually cboLoadTypeID.Column(1)

Clear as mud?? Thoughts

HTH << MaZeWorX >> "I have not failed I have only found ten thousand ways that don't work
 
Sorry my apologies after a rethink the first bit of code didnt account for null or empty string values so it should read
Code:
    If IsNull(Me.cboLoadTypeID) Or Me.cboLoadTypeID = 0 Then
        Exit Sub
    Else
        cboLoadTypeID.SelStart = 0         ' Start selection at beginning.
        cboLoadTypeID.SelLength = Len(cboLoadTypeID.Column(1))
    End If

HTH << MaZeWorX >> "I have not failed I have only found ten thousand ways that don't work
 
MaZeWorX

Beautiful Work!

Yes it highlights the complete string.

However the cursor stays at the position where the mouse was clicked. This is a problem because it does not overwrite the previous selection.

I tried adding the Sendkeys "{HOME}" at the end of the code.
Code:
    If IsNull(Me.SelectName) Or Me.SelectName = 0 Then
        Exit Sub
    Else
        SelectName.SelStart = 0 ' Start selection at beginning.
        SelectName.SelLength = Len(SelectName.Column(0))
        SendKeys "{HOME}"
    End If

It doesn't work because if I understand correctly the sendkeys happens prior to the mouse click / cursor position.

So how can we get the cursor at the begining of the line regardles of where the mouse is clicked?
 
Code:
Private Sub cboLoadTypeID_KeyDown(KeyCode As Integer, Shift As Integer)
Me.cboLoadTypeID.Value = ""
End Sub

simply sets the value of the combo to an empty string

HTH << MaZeWorX >> "I have not failed I have only found ten thousand ways that don't work
 
btw is your cbo box all text?
I used because my example uses an ID field which is numeric

If IsNull(Me.SelectName) Or Me.SelectName = 0 Then

This references the bound column of the combo box in my case is numeric if yours is alpha then it should read

If IsNull(Me.SelectName) Or Me.SelectName = "" Then



HTH << MaZeWorX >> "I have not failed I have only found ten thousand ways that don't work
 
If I may correct you on your KeyDown Event Procedure

it won't work because it keeps on blanking out as soon as you start typing in to the line. I added it to the end of the MouseDown and Enter Even Procedures.

Code:
    If IsNull(Me.SelectName) Or Me.SelectName = "" Then
        Exit Sub
    Else
        SelectName.SelStart = 0 ' Start selection at beginning.
        SelectName.SelLength = Len(SelectName.Column(0))
        Me.SelectName.Value = ""
    End If
 
i assumed thats what you wanted to clear the selection? if not ok as long as it all worked out :)

HTH << MaZeWorX >> "I have not failed I have only found ten thousand ways that don't work
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top