Select Text In Textbox

Select Text In Textbox

Post by Lemo » Sat, 12 Feb 2005 00:48:31

Newbie to vb .net from vb 6.0. Simple problem?

In the Enter event of a text box:

txtGroup.SelectionStart = 0
txtGroup.SelectionLength = txtGroup.Text.Length

If I tab to the textbox it behaves as expected. If, however, I click on the
text box it does not work. Putting a breakpoint in shows that the code is
running, it's just not executing. How could it be that code that looks like
it's running does not execute?


Select Text In Textbox

Post by Dave » Sat, 12 Feb 2005 02:02:05

try putting in methods for the other events associated with the text box, i
think you will find that there is probably also a click event and probably a
focus event being fired. most likely the default action for the click event
is done after the enter event and un does your select statement. try
putting it in the gotfocus (or whatever that one is called, i don't have it
open off hand to check), i think that is done later and doing text
selections work better in that one.



Select Text In Textbox

Post by Steve East » Sun, 13 Feb 2005 08:45:13

If you're trying to capture the text entered, I do it this way.
First, dimension a global variable. For example: tbx1 for textbox 1.

Dim tbx1 As String

Then in the textbox area add a line:
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles

tbx1 = TextBox1.Text

End Sub

You don't need:
txtGroup.SelectionStart = 0
txtGroup.SelectionLength = txtGroup.Text.Length

unless you need to capture a specific portion of the entry or limit the characters entered..

You then use the variable tbx1 if you want to write the entry to a file using a button click event

Dim myfile as test.txt
Dim sr As StreamWriter = File.CreateText(myfile)

Will write the contents of TextBox1 to a file named test.txt

Be advised if test.txt does not yet exist you will need to create it.


Steve Easton
Microsoft MVP FrontPage
This site is best viewed............
.......................with a computer

Select Text In Textbox

Post by Lemo » Mon, 14 Feb 2005 01:30:02

Thanks for the replies. I didn't explain it very well. I just want the
text in the textbox selected when I go to the textbox by tabbing or
clicking. If I use the new (to me) Enter event or the GotFocus event and
run the below code:

txtWhatever.SelectionStart = 0
txtWhatever.SelectionLength = txtGroup.Text.Length



Or the old VB6:

txtWhatever.SelStart = 0
txtWhatever.SelLength = Len(txtWhatever.Text)

Any of these work fine if I tab to the textbox but NOT if I click in the
textbox. If I click in the
textbox the text remains unselected. I have to repeat the code in the
Click event to get it to work.

This blows my mind because if I put a breakpoint in the Enter event (if I
have it there) or the GotFocus event (if I have it there), I can see that
the code is being run when I click in the box. It is not being executed
though. The text remains unselected.


Select Text In Textbox

Post by Dave » Mon, 14 Feb 2005 02:31:04

t is being executed, however you have to consider the order that things
happen. i haven't traced this personally because i know the focus event
works... but what is likely happening is that the text is selected in your
enter event handler but the click of the mouse has not been handled yet.
when the mouse click is actually handled the default code for it unselects
the text by putting the insertion point where ever in the string in the box
you clicked. so the trick is to find the last event that happens after the
mouse places the insertion point and select the text there. and yes, this
may result in duplicate code, though i can't say for sure in this case if
its possible to do it only in one place off hand.

"Lemo" < XXXX@XXXXX.COM > wrote in message
news: XXXX@XXXXX.COM ...