Autoscroll in textbox
-
I have a multiline textbox control on a form. When a user focuses on the textbox I want to check and see if a certain string (user's name and todays date) are in the textbox. If not, I want to automatically add the user's name and todays date to the end of the textbox and position the curor and scroll to the bottom. I am having trouble with the scrolling/cursor positioning part. My code will scroll down, but the cursor ends up moving to where ever the user clicked in the textbox. Does anyone know of a way to ignore where they clicked so as to override where the cursor goes? Is there another event that fires that I can grab? Am I missing something obvious here?
Private Sub txtInput\_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtInput.GotFocus Dim strAutoLabel As String = gblstrUserName.Trim & " - " & Now.ToString("M/d/yy") 'If the textbox does not contain the auto label, add it If Not txtInput.Text.Contains(strAutoLabel) Then If txtInput.Text.Trim.Length <> 0 Then txtInput.Text &= vbCrLf & vbCrLf txtInput.Text &= strAutoLabel & vbCrLf txtInput.SelectionStart = txtInput.Text.Length - 1 txtInput.ScrollToCaret() End If End Sub
I'd appreciate any help or advice you can give me.
-
I have a multiline textbox control on a form. When a user focuses on the textbox I want to check and see if a certain string (user's name and todays date) are in the textbox. If not, I want to automatically add the user's name and todays date to the end of the textbox and position the curor and scroll to the bottom. I am having trouble with the scrolling/cursor positioning part. My code will scroll down, but the cursor ends up moving to where ever the user clicked in the textbox. Does anyone know of a way to ignore where they clicked so as to override where the cursor goes? Is there another event that fires that I can grab? Am I missing something obvious here?
Private Sub txtInput\_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtInput.GotFocus Dim strAutoLabel As String = gblstrUserName.Trim & " - " & Now.ToString("M/d/yy") 'If the textbox does not contain the auto label, add it If Not txtInput.Text.Contains(strAutoLabel) Then If txtInput.Text.Trim.Length <> 0 Then txtInput.Text &= vbCrLf & vbCrLf txtInput.Text &= strAutoLabel & vbCrLf txtInput.SelectionStart = txtInput.Text.Length - 1 txtInput.ScrollToCaret() End If End Sub
I'd appreciate any help or advice you can give me.
In the mouse click event for the tetbox you could try:
if not (Me.txtNvs.SelectionStart = txtInput.text.length -1) then Me.txtNvs.SelectionStart = txtInput.text.length -1 Me.txtNvs.SelectionLength = 0 end if
'it may be something similar to determine the current selected index in the if statement good luck :) -
In the mouse click event for the tetbox you could try:
if not (Me.txtNvs.SelectionStart = txtInput.text.length -1) then Me.txtNvs.SelectionStart = txtInput.text.length -1 Me.txtNvs.SelectionLength = 0 end if
'it may be something similar to determine the current selected index in the if statement good luck :)Thanks for taking the time to help me. It's not quite what I was after, but it's a step in the right direction. I want the autoscroll to the bottom to happen only the first time they click...if they click once to get focus and then click again because they are trying to highlight a word or edit text it should not scroll. So I did this:
Dim intMouseClickCounter As Integer = 0 Private Sub txtInput\_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtInput.GotFocus Dim strAutoLabel As String = gblstrUserName.Trim & " - " & Now.ToString("M/d/yy") 'If the textbox does not contain the auto label, add it If Not txtInput.Text.Contains(strAutoLabel) Then If txtInput.Text.Trim.Length <> 0 Then txtInput.Text &= vbCrLf & vbCrLf txtInput.Text &= strAutoLabel & vbCrLf txtInput.SelectionStart = txtInput.Text.Length txtInput.ScrollToCaret() 'Set counter to one so the mouseclick event will scroll to the bottom intMouseClickCounter = 0 Else 'Set counter to one so the mouse click will not scroll to the bottom intMouseClickCounter = 1 End If End Sub Private Sub txtSpecialReport\_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles txtInput.MouseClick If intMouseClickCounter = 0 \_ AndAlso Not (txtInput.SelectionStart = txtInput.Text.Length) Then txtInput.SelectionStart = txtInput.Text.Length txtInput.SelectionLength = 0 txtInput.ScrollToCaret() End If 'Increment counter so if user clicks inside textbox after focus has already been set, 'the code to scroll to the bottom will not run again intMouseClickCounter += 1 End Sub Private Sub txtInput\_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtInput.LostFocus intMouseClickCounter = 0 'Reset counter End Sub
But this solution has flaws. It doesn't work the way I'd like when a user tabs into the textbox, and it will scroll when I don't want it to if the user activates another window/program and then returns to this form and this textbox. If anyone has any further suggestions I would appreciate it.