SELECT method for form textbox controls

  • Thread starter Thread starter Cindy M -WordMVP-
  • Start date Start date
C

Cindy M -WordMVP-

Hi all

While working my way through a VB.NET exercise, I came
across something that didn't do what the text said it
should and I'm wondering if it's supposed to behave this
way, or not (and why).

txtNote.Text = "the text"
txtNote.Enabled = True
'txtNote.Select()
txtNote.Select(0, 0) 'deselect text

txtNote is a textbox control on a windows form. As the code
stands, the txtNote control is NOT selected, focus remains
in the control that had the selection when the code is
executed.

If I include the line that's currently commented out, first
selecting the control specifically, then setting the
selection length (doesn't matter what numbers I put in the
function), it works as I'd expect, and focus is in the text
box.

Should it always be necessary to use Select twice - once to
put the focus into the control, once to change what's
selected - or am I missing something?

TIA, Cindy


-- Cindy
 
Cindy M -WordMVP- said:
Hi all

While working my way through a VB.NET exercise, I came
across something that didn't do what the text said it
should and I'm wondering if it's supposed to behave this
way, or not (and why).

txtNote.Text = "the text"
txtNote.Enabled = True
'txtNote.Select()
txtNote.Select(0, 0) 'deselect text

txtNote is a textbox control on a windows form. As the code
stands, the txtNote control is NOT selected, focus remains
in the control that had the selection when the code is
executed.

If I include the line that's currently commented out, first
selecting the control specifically, then setting the
selection length (doesn't matter what numbers I put in the
function), it works as I'd expect, and focus is in the text
box.

Should it always be necessary to use Select twice - once to
put the focus into the control, once to change what's
selected - or am I missing something?

Do you want to select text or do you want to change the focus?

To change the focus, call the Focus function.
To select text, set the SelectionStart and SelectionLength properties, or
call SelectAll.
 
* Cindy M -WordMVP- said:
While working my way through a VB.NET exercise, I came
across something that didn't do what the text said it
should and I'm wondering if it's supposed to behave this
way, or not (and why).

txtNote.Text = "the text"
txtNote.Enabled = True
'txtNote.Select()
txtNote.Select(0, 0) 'deselect text

txtNote is a textbox control on a windows form. As the code
stands, the txtNote control is NOT selected, focus remains
in the control that had the selection when the code is
executed.

If I include the line that's currently commented out, first
selecting the control specifically, then setting the
selection length (doesn't matter what numbers I put in the
function), it works as I'd expect, and focus is in the text
box.

Should it always be necessary to use Select twice - once to
put the focus into the control, once to change what's
selected - or am I missing something?

'Select' without parameters is used to /activate/ the control (its
inherited from 'Control'), the parameterized 'Select' method is used to
select part of the text. You can call the control's 'Focus' method to
focus it and the 'SelectionStart' and 'SelectionLength' properties to
select some text.
 
Back
Top