For Programmers: Free Programming Magazines  


Home > Archive > Visual Studio > February 2005 > Select Text In Textbox









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Select Text In Textbox
Lemo

2005-02-10, 3:59 pm

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?

Thanks
lemo


Dave

2005-02-10, 3:59 pm

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.

"Lemo" <lemo01no@spam.charter.net> wrote in message
news:%23SWm9f4DFHA.2644@TK2MSFTNGP10.phx.gbl...
> 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?
>
> Thanks
> lemo
>
>



Steve Easton

2005-02-11, 8:59 pm

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
TextBox1.TextChanged

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)
sr.WriteLine(tbx1)
sr.Close()


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.

hth

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

"Lemo" <lemo01no@spam.charter.net> wrote in message news:%23SWm9f4DFHA.2644@TK2MSFTNGP10.phx.gbl...
> 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?
>
> Thanks
> lemo
>
>



Lemo

2005-02-12, 3:58 pm

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:

txtWhatever.SelectAll()

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.

lemo

"Steve Easton" <admin@95isalive.com> wrote in message
news:u8tfsSJEFHA.4052@TK2MSFTNGP09.phx.gbl...
> 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
> TextBox1.TextChanged
>
> 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)
> sr.WriteLine(tbx1)
> sr.Close()
>
>
> 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.
>
> hth
>
> --
> Steve Easton
> Microsoft MVP FrontPage
> 95isalive
> This site is best viewed............
> .......................with a computer
>
> "Lemo" <lemo01no@spam.charter.net> wrote in message
> news:%23SWm9f4DFHA.2644@TK2MSFTNGP10.phx.gbl...
>
>



Dave

2005-02-12, 3:58 pm

it 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" <lemo01no@spam.charter.net> wrote in message
news:eRJ6cASEFHA.3780@TK2MSFTNGP09.phx.gbl...
> 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:
>
> txtWhatever.SelectAll()
>
> 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.
>
> lemo
>
> "Steve Easton" <admin@95isalive.com> wrote in message
> news:u8tfsSJEFHA.4052@TK2MSFTNGP09.phx.gbl...
As[color=darkred]
on[color=darkred]
>
>




Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com