Solved

text selected in textbox when got focus

Posted on 2001-07-03
29
215 Views
Last Modified: 2012-06-21
0
Comment
Question by:RSiebeling
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 16
  • 9
  • 2
  • +2
29 Comments
 
LVL 5

Expert Comment

by:jklmn
ID: 6248152
Try this:

Private Sub Text1_GotFocus()
Text1.SelStart = 0
Text1.SelLength = Len(Text1)
End Sub
0
 
LVL 52

Expert Comment

by:Ryan Chong
ID: 6248158
SelectedText = Text1.SelText
0
 
LVL 52

Expert Comment

by:Ryan Chong
ID: 6248159
or add

Sendkeys "{Home}+{End}" ?
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:RSiebeling
ID: 6248168
I have done that,
that works,
but I also want that when you click with the mouse
in the textbox no text is selected, and the cursor is at the location of the mouse,

I have tried it with the following code in the click event
text1.selstart=0
text1.sellength=0

but thats not what i want, then the user sees that the text is selected and deselected and thats not what I want
0
 

Author Comment

by:RSiebeling
ID: 6248172
I see now that my question was not posted correctly,
Im sorry about that
0
 
LVL 52

Expert Comment

by:Ryan Chong
ID: 6248182
Hi RSiebeling,

Basically, when you click on the textbox, no text is selected
0
 

Author Comment

by:RSiebeling
ID: 6248184
I see now that my question was not posted correctly,
Im sorry about that
0
 

Author Comment

by:RSiebeling
ID: 6248195
thats true,

but i want that the text is selected when the user presses tab and the textbox got the focus,

just like the standard windows textbox controls,
(every microsoft textbox behaves just like i said)
0
 
LVL 1

Expert Comment

by:kavithag
ID: 6248198
Text _ Gotfocus Event:

text1.SelStart = 0
text1.SelLength = Len(txtDevCode.Text)
0
 
LVL 52

Expert Comment

by:Ryan Chong
ID: 6248204
Hi,

the simplest way is:

Private Sub Text1_GotFocus()
    Sendkeys "{Home}+{End}"
End Sub

Private Sub Text1_Click()
    Sendkeys "{Home}+{End}"
End Sub


..
0
 

Author Comment

by:RSiebeling
ID: 6248205
I have done that,
that works,
but I also want that when you click with the mouse
in the textbox no text is selected, and the cursor is at the location of the mouse,

0
 

Author Comment

by:RSiebeling
ID: 6248213
i have the following code in my application

Private Sub NAWNaam_Click()
    NAWNaam.SelStart = 0
    NAWNaam.SelLength = 0
   
End Sub

Private Sub NAWNaam_GotFocus()
    NAWNaam.SelStart = 0
    NAWNaam.SelLength = Len(NAWNaam)
   
End Sub

it works good when the user presses tab,
but when the user clicks with the mouse in the textbox,
the text is first selected and then it is deselected,
and I dont want to see that

0
 
LVL 52

Expert Comment

by:Ryan Chong
ID: 6248221
Hi, are you mean the cursor move as well as the mouse move?
0
 
LVL 52

Expert Comment

by:Ryan Chong
ID: 6248234
Hi RSiebeling,

This not working?
_______________________

Private Sub NAWNaam_Click()
   SendKeys "{Home}+{End}"
End Sub

Private Sub NAWNaam_GotFocus()
   SendKeys "{Home}+{End}"
End Sub

or not what you want?
0
 

Author Comment

by:RSiebeling
ID: 6248239
no i want the textbox control to behave just like the standard windows textbox control (like in word / excel / access / notepad / wordpad)

for example if you look at the wordpad,
if you go to format -> paragraph,

if you use tab to set the focus to another textbox, all the text in the textbox will be selected,
and if you use the mouse to set the focus to another textbox, no text is selected and the cursor is being placed at the position of the mouse
0
 

Author Comment

by:RSiebeling
ID: 6248247
0
 

Author Comment

by:RSiebeling
ID: 6248253
no thats not what i want,

i want that when the user clicks on the the textbox ,
no text is selected and the cursor is being placed at the location of the mouse,

just look at wordpad
0
 

Author Comment

by:RSiebeling
ID: 6248254
no thats not what i want,

i want that when the user clicks on the the textbox ,
no text is selected and the cursor is being placed at the location of the mouse,

just look at wordpad
0
 
LVL 52

Expert Comment

by:Ryan Chong
ID: 6248263
Hi, try this:

Private Sub NAWNaam_GotFocus()
   NAWNaam.SelStart = Len(NAWNaam)
   SendKeys "{Home}+{End}"
End Sub
0
 

Author Comment

by:RSiebeling
ID: 6248280
no thats not working,

when i click with the mouse in the textbox,
text is selected from the position of the mouse till the end of the text in the textbox
0
 
LVL 5

Accepted Solution

by:
jklmn earned 50 total points
ID: 6248281
OK, try this instead:

Private fromMouse As Boolean

Private Sub Text1_GotFocus()
If fromMouse Then
fromMouse = False
Else
Text1.SelStart = 0
Text1.SelLength = Len(Text1)
End If
End Sub

Private Sub Text1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
fromMouse = True
End Sub
0
 

Author Comment

by:RSiebeling
ID: 6248285
no thats not working,

when i click with the mouse in the textbox,
text is selected from the position of the mouse till the end of the text in the textbox
0
 
LVL 52

Expert Comment

by:Ryan Chong
ID: 6248291
Hi, how?

Private Sub NAWNaam_Click()
    SendKeys "+{End}"
End Sub

Private Sub NAWNaam_GotFocus()
   NAWNaam.SelStart = Len(NAWNaam)
   SendKeys "{Home}+{End}"
End Sub
0
 

Author Comment

by:RSiebeling
ID: 6248296
no thats not working,

when i click with the mouse in the textbox,
text is selected from the position of the mouse till the end of the text in the textbox
0
 

Author Comment

by:RSiebeling
ID: 6248302
thats it,
now its working like i want
0
 

Author Comment

by:RSiebeling
ID: 6248304
thats it,
now its working like i want
0
 
LVL 3

Expert Comment

by:nigelrowe
ID: 6248313
how about...

Private Sub Text1_Click()
    Text1.SelStart = 0
    Text1.SelLength = Len(Text1.Text)
End Sub


The problem here is that you will have to deselect by using the arrow keys first, any other key will delete the whole lot
0
 
LVL 52

Expert Comment

by:Ryan Chong
ID: 6248325
Hi RSiebeling,

i wonder what you want now, actually remove all the code then try the code below will do the same thing, isn't it ?

But nvm lah :)

Private Sub NAWNaam_GotFocus()
  NAWNaam.SelStart = Len(NAWNaam)
  SendKeys "{Home}+{End}"
End Sub
0
 

Author Comment

by:RSiebeling
ID: 6248381
try this
Private fromMouse As Boolean

Private Sub Text1_GotFocus()
If fromMouse Then
fromMouse = False
Else
Text1.SelStart = 0
Text1.SelLength = Len(Text1)
End If
End Sub

Private Sub Text1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
fromMouse = True
End Sub

thats what i want
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month3 days, 15 hours left to enroll

630 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question