Question How to focus on the very last word in the TextBox ?

capedech

Well-known member
Joined
Oct 29, 2008
Messages
62
Programming Experience
Beginner
I make the TextBox1 properties become multiline and has vertical scroll.

This is my code :
VB.NET:
Button1_Click :
For I as Integer = 1 To 100
  TextBox1.Text &= I & VbCrLf
  TextBox1.SelectionStart = TextBox.TextLength
  TextBox1.ScrollToCaret()
  TextBox1.Refresh()
  Threading.Thread.Sleep(1)
Next I

I made this code accidently. And it works. But I wonder, Is there a better to do it?

Thanks in Advance.

Regards,
 
You could try

VB.NET:
  TextBox1.Select(TextBox1.Text.LastIndexOf(" "c) + 1, TextBox1.Text.Length - (TextBox1.Text.LastIndexOf(" "c) + 1))

The .Select method selects the text in the textbox from a stated index for the length of a count of characters.

VB.NET:
TextBox1.Text.LastIndexOf(" "c) + 1
Finds the last index of a blank space. Then adds one to 'skip' the space. This is our beginning point within the line of text.

VB.NET:
TextBox1.Text.Length - (TextBox1.Text.LastIndexOf(" "c) + 1)
This line takes the total length of the text and subtracts the index of the character that we want to start at.
 

Latest posts

Back
Top