changing the selection behaviour of a double click in a text box

smig

Registered User.
Local time
Today, 05:03
Joined
Nov 25, 2009
Messages
2,209
I'm trying to change the selection behaviour of a double click in a text box to select the entire text

tried this code:
Code:
Private Sub TextBox_DblClick(Cancel As Integer)

Me.TextBox.SelStart = 0
Me.TextBox.SelLength = Len(Me.TextBox.Text)
 
End Sub
this should select the entire text in the text box. it works for a click event, but a double click will keep selecting a single word :mad:
 
I suspect that the code you have is working fine, the problem is that the native double click behaviour is firing after the VBA event and causing only the word in which the cursor is located to be selected. Are you able to use another event or button to select all the text?
 
yes, as I wrote:
it works for a click event, but a double click will keep selecting a single word :mad:
 
...and I said (in effect) that the native double click behaviour of windows is overwriting your VBA event code action.
 
I read what you said, and if there is no way to disable it I'll be very sad :(
If all fail I'll ask the user for a long click and use a timer
 
OK here's a bit of a kludge that will get the response you are looking for, in your Double click event put the following code;
Code:
    On Error GoTo Your_DblClick

    Me.Text5.SetFocus
    
Exit_Your_DblClick:
    Exit Sub
    
Err_Your_DblClick:
    Cancel = True
   Resume Exit_Your_DblClick

Now make Text5's Border style and Back Style both Transparent hide/remove it's label and make it as small as possible and put it in a position that the user is unlikely to click on. Now in it's Got Focus event put your code;
Code:
Me.TextBox.Setfocus
Me.TextBox.SelStart = 0
Me.TextBox.SelLength = Len(Me.TextBox.Text)
 
The error handling in the Double Click event is critical, as without it the moment the focus comes back to your initial text box it will cause an error saying that focus can not be set to Text5, and the whole process crashes.
 
it's going nice :)
in most cases I won't have to select the entire field, but I know how to take care for this, now that we overcome the default selection behaviour

BTW - when you go back to the TextBox it will select the enire text as this is the default behaviour of moving into a text box

also Text5 can be put on hidden Top/Bottom header of the form
 
it's going nice :)
........

BTW - when you go back to the TextBox it will select the enire text as this is the default behaviour of moving into a text box

........

That should allow you to simplify the code in Text5's got focus event in that case, down to simply setting the focus back on your text box :D
 
but in most cases I don't need to select the entire text :D

what I work now are forms to send email
I'll put it in the Code Repository after I finish it. a lot of work was spent here, so why not leting other enjoy and save some time ;)
 

Users who are viewing this thread

Back
Top Bottom