Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Autoscroll in textbox

Autoscroll in textbox

Scheduled Pinned Locked Moved Visual Basic
helpquestion
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    Kschuler
    wrote on last edited by
    #1

    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.

    M 1 Reply Last reply
    0
    • K Kschuler

      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.

      M Offline
      M Offline
      Mr Oizo
      wrote on last edited by
      #2

      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 :)

      K 1 Reply Last reply
      0
      • M Mr Oizo

        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 :)

        K Offline
        K Offline
        Kschuler
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups