I am wondering how to make the text in a certain textbox of the
userform selected when the userform in shown up.
For example, I would like to do the same with the inputbox, in which
the default text in the textbox has been already selected when the
inputbox is shown.
I understand that when I set "tab order", I can place the cursol at
the initial desired point in the userform when the userform is shown.
However, in this case, if I set to place the cursol in the certain
textbox, the cursol is placed after the text in the textbox, resulting
that the text is not selected initially.
Thank you very much for your help.
Private Sub UserForm_Initialize()
With TextBox1
.Text = "sample text"
.SetFocus
.SelStart = 0
.SelLength = Len(.Text)
End With
End Sub
The .SelStart value sets the location of the cursor within the textbox, so
setting it to 0 puts the cursor at the left end. According to the help topic,
setting .SelStart also changes the value of .SelLength to 0, so you must set
.SelStart first.
The .SelLength value sets the location of the right end of the selection in the
textbox. The maximum value is the total number of characters in the box, which
is Len(.Text).
--
Regards,
Jay Freedman
Microsoft Word MVP FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the newsgroup so all
may benefit.
Thank you very much for sharing the code.
Your code is very beautiful and is simple enough for me
to understand it.
I found your code works when I use it in a simple userform with
textboxes.
However, when i use your code in a complex userform
with multiplepages, frames, textboxes, and so on,
only ".SetFocus" works but cursor disappears.
I think that focus is set in the specified textbox,
because when I press "tab" key, cursor appears and is
moved from the specified textbox to another in the order of tab-order.
Thus, without presing "tab" key, cursor remains invisible.
As a result, the text in the specified textbox are not selected
when the userform is shown.
I do not know why.
Let me tell you more specific textbox that I want to set focus on.
Userform has two multiplepages.
The multipage1 has four frames.
One of the frames has 9 textboxes.
I would like to set forcus a specific one of the 9 textboxes, and
to select the text in the specific textbox.
if the above information is not enough, please let me know.
Thank you very much for your help.
Junya Nitta
It might help to post the code of your procedure that sets focus to the textbox.
If that isn't sufficient, I'll ask you to send a copy of the template.
--
Regards,
Jay Freedman
Microsoft Word MVP FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the newsgroup so all
may benefit.
Thank you very much for your quick response.
Actually, when I tried to set up another userform as I described
above,
I found that the code worked perfectly.
However, I still cannot get what i want in my custermized userform.
In my custermized userform, i retrieve some values for textboxes
and position of the userform from .ini file.
Do you think this does any harm to .Setfocus?
Please tell me some clues for finding out what's going wrong with my
userform
so that I will figure out myself before I ask you more with my
template.
thank you for your consideration.
junya55
No, I think the source of the data is very unlikely to have any effect. However,
I don't know what effect there might be if the contents of the text box are in a
right-to-left language or in the upper range of Unicode characters.
>Please tell me some clues for finding out what's going wrong with my
>userform
>so that I will figure out myself before I ask you more with my
>template.
There really isn't much to try. You could put a breakpoint in the userform code
just before the place that has the .SetFocus, .SelStart, and .SelLength
statements. When the breakpoint occurs and the VBA editor opens, use the F8 key
to step through those statements one at time. Look at the values of .SelStart
and .SelLength in the Watch window to see if they're the expected values or
something else.
--
Thank you for your help.
> There really isn't much to try. You could put a breakpoint in the userform code
> just before the place that has the .SetFocus, .SelStart, and .SelLength
> statements. When the breakpoint occurs and the VBA editor opens, use the F8 key
> to step through those statements one at time. Look at the values of .SelStart
> and .SelLength in the Watch window to see if they're the expected values or
> something else.
I tried your advice above.
I could not find .SelStart and .SelLength in the watch window so
I place the cursor over .SelStart and .SelLength to look at the
values.
Then, I found out that the values for .SelStart and .SelLength are
correct.
Also, I comment out (delete) all the codes in the "Private Sub
UserForm_Initialize()"
other than the following code you gave me.
With TextBox1
.Text = "sample text"
.SetFocus
.SelStart = 0
.SelLength = Len(.Text)
End With
However, the cursor is invisible when the userform is shown.
Any idea?
Thank you very much for your help.
junya55
No, I don't have any more ideas for you. It has always worked for me, and you
said that it worked for you in another userform. It might work if you rebuild
your complex userform from the beginning, but I can understand if you feel
that's too much work.
> No, I don't have any more ideas for you. It has always worked for me, and you
> said that it worked for you in another userform. It might work if you rebuild
> your complex userform from the beginning, but I can understand if you feel
> that's too much work.
Yes, that's right. I feel that's too much work if i rebuild it from
scratch.
I think I will give it out this time.
Next time, I will see if it works OK before I complete the userform.
Thanks a lot for your help.
i will close this topic.
junya55