Select entire textbox contents

Select entire textbox contents

Post by Ivan Debon » Sun, 31 Jul 2005 23:15:33


Hi all,

Is there an easy API way to select the entire text of a textbox when the
focus moves into the textbox, instead of using SelLen and SelStart. Maybe an
API that sets some hidden property?

Thanks
Ivan
 
 
 

Select entire textbox contents

Post by mayayan » Sun, 31 Jul 2005 23:46:05

You can use SendMessage with:

TextHwnd, EM_SETSEL, 0, -1

------------------------
From the docs:
"In a rich edit control, if the selection is not entirely contained in the
first 64K, use the message EM_EXSETSEL."
---------------------

EM_EXSETSEL is basically the same thing except
that it puts the start and end values into a CHARRANGE
structure in the lParam and leaves the wParam as 0:

Private Const WM_USER = &H400
Private Const EM_EXSETSEL = (WM_USER + 55)

Private Type CHARRANGE
cpMin As Long
cpMax As Long
End Type

Private Declare Function SendMessageAny Lib "user32" Alias "SendMessageA"
(ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As
Any) As Long

Dim CR1 As CHARRANGE
CR1.cpMin = 0
CR1.cpMax = -1
SendMessageAny hRTB, EM_EXSETSEL, 0, CR1

 
 
 

Select entire textbox contents

Post by Rick Roths » Sun, 31 Jul 2005 23:53:20

> Is there an easy API way to select the entire text of a textbox when
the
Maybe an

You should be able to use the SendMessage API coupled with EM_SETSEL to
do what you want. See the example here for some ideas...

http://www.yqcomputer.com/

Rick
 
 
 

Select entire textbox contents

Post by erewho » Mon, 01 Aug 2005 00:41:32

On Sat, 30 Jul 2005 16:15:33 +0200, "Ivan Debono"



Why use an API to add 1 + 1 ?

It might not be faster (who cares in this case) than 'trusted' calls
to the same thing.
 
 
 

Select entire textbox contents

Post by Tom Es » Mon, 01 Aug 2005 04:17:29

On Sat, 30 Jul 2005 15:41:32 +0000 (UTC), XXXX@XXXXX.COM (J


I assumed the OP was asking if there was a prop or style bit that
could be flipped that would eliminate having to do it manually (Api or
otherwise) in GotFocus. IOW something like "ES_AUTOSELECT", which of
course does not exist.


-Tom
MVP - Visual Basic
(please post replies to the newsgroup)
 
 
 

Select entire textbox contents

Post by Ivan Debon » Mon, 01 Aug 2005 12:25:31

Exactly.

Pity nothing of the sort exists.

Ivan

"Tom Esh" < XXXX@XXXXX.COM > schrieb im Newsbeitrag
 
 
 

Select entire textbox contents

Post by Rick Roths » Mon, 01 Aug 2005 13:08:26

> Pity nothing of the sort exists.

I would think you could create a User Control that has whatever
automatic actions you want and use it instead of the standard TextBox.

Rick
 
 
 

Select entire textbox contents

Post by Tom Es » Mon, 01 Aug 2005 16:23:16

On Sun, 31 Jul 2005 05:25:31 +0200, "Ivan Debono"



I suppose, but doing it with the Sel props still only amounts to 2
lines of code per GotFocus event, which if you use control arrays is
only one per array. You could whittle it down to one call per event
with something like this in a bas module...

Public Sub TbAutoSelect(oTB As TextBox)
With oTB
.SelStart = 0
.SelLength = Len(.Text)
End With
End Sub


-Tom
MVP - Visual Basic
(please post replies to the newsgroup)
 
 
 

Select entire textbox contents

Post by Rick Roths » Mon, 01 Aug 2005 17:09:28

> >Pity nothing of the sort exists.

And, since you are calling it from the GotFocus event, you can eliminate
the argument altogether...

Public Sub TbAutoSelect()
With Screen.ActiveForm.ActiveControl
.SelStart = 0
.SelLength = Len(.Text)
End With
End Sub

Rick
 
 
 

Select entire textbox contents

Post by Tom Es » Tue, 02 Aug 2005 01:36:24

On Sun, 31 Jul 2005 04:09:28 -0400, "Rick Rothstein [MVP - Visual



Cool. Might want to include an error handler though. Seems I
invariably need one when dealing with ActiveForm/Control. :-)


-Tom
MVP - Visual Basic
(please post replies to the newsgroup)
 
 
 

Select entire textbox contents

Post by Rick Roths » Tue, 02 Aug 2005 05:22:53

> >And, since you are calling it from the GotFocus event, you can
eliminate

Assuming the Sub is called from any event procedure in a TextBox or
RichTextBox control, it will work fine (as would yours) and no error
protection should be needed (or at least I cannot think of any reason
why it would be needed).

Rick
 
 
 

Select entire textbox contents

Post by Ivan Debon » Tue, 02 Aug 2005 16:07:47

I did that. I actually created a UserControl based on just API, and I have
much much more properties than the implicit textbox. It was unfortunately
slow (although just pure API calls)

"Rick Rothstein [MVP - Visual Basic]" < XXXX@XXXXX.COM >
 
 
 

Select entire textbox contents

Post by Ivan Debon » Tue, 02 Aug 2005 16:08:41


"Rick Rothstein [MVP - Visual Basic]" < XXXX@XXXXX.COM >


Yep. That's exactly what I have.

Ivan