Go Premium for a chance to win a PS4. Enter to Win

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

text selected in textbox when got focus

0
RSiebeling
Asked:
RSiebeling
  • 16
  • 9
  • 2
  • +2
1 Solution
 
jklmnCommented:
Try this:

Private Sub Text1_GotFocus()
Text1.SelStart = 0
Text1.SelLength = Len(Text1)
End Sub
0
 
Ryan ChongCommented:
SelectedText = Text1.SelText
0
 
Ryan ChongCommented:
or add

Sendkeys "{Home}+{End}" ?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
RSiebelingAuthor Commented:
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
 
RSiebelingAuthor Commented:
I see now that my question was not posted correctly,
Im sorry about that
0
 
Ryan ChongCommented:
Hi RSiebeling,

Basically, when you click on the textbox, no text is selected
0
 
RSiebelingAuthor Commented:
I see now that my question was not posted correctly,
Im sorry about that
0
 
RSiebelingAuthor Commented:
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
 
kavithagCommented:
Text _ Gotfocus Event:

text1.SelStart = 0
text1.SelLength = Len(txtDevCode.Text)
0
 
Ryan ChongCommented:
Hi,

the simplest way is:

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

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


..
0
 
RSiebelingAuthor Commented:
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
 
RSiebelingAuthor Commented:
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
 
Ryan ChongCommented:
Hi, are you mean the cursor move as well as the mouse move?
0
 
Ryan ChongCommented:
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
 
RSiebelingAuthor Commented:
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
 
RSiebelingAuthor Commented:
0
 
RSiebelingAuthor Commented:
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
 
RSiebelingAuthor Commented:
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
 
Ryan ChongCommented:
Hi, try this:

Private Sub NAWNaam_GotFocus()
   NAWNaam.SelStart = Len(NAWNaam)
   SendKeys "{Home}+{End}"
End Sub
0
 
RSiebelingAuthor Commented:
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
 
jklmnCommented:
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
 
RSiebelingAuthor Commented:
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
 
Ryan ChongCommented:
Hi, how?

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

Private Sub NAWNaam_GotFocus()
   NAWNaam.SelStart = Len(NAWNaam)
   SendKeys "{Home}+{End}"
End Sub
0
 
RSiebelingAuthor Commented:
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
 
RSiebelingAuthor Commented:
thats it,
now its working like i want
0
 
RSiebelingAuthor Commented:
thats it,
now its working like i want
0
 
nigelroweCommented:
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
 
Ryan ChongCommented:
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
 
RSiebelingAuthor Commented:
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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

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