Results 1 to 5 of 5

Thread: Cursor in textbox keeps moving to begining during key-in

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Apr 2017
    Posts
    22

    Cursor in textbox keeps moving to begining during key-in

    I am keying in data into a text box and as I am keying it in, the cursor keeps moving back to it's initial position. So, if I key in "154", it displays "541". The "1" shows, then the cursor moves back to the beginning spot and then the "54" shows.

    Any help would be appreciated....

    Code:
    Private Sub tbHeight_TextChanged(sender As Object, e As EventArgs) Handles tbHeight.TextChanged
            Dim foot As Decimal
    
            If IsNumeric(tbHeight.Text) Then
                tbStride.Text = Val(tbHeight.Text) * My.Settings.Height
                foot = Val(tbHeight.Text) * My.Settings.Foot
    
                tbLength.Text = foot.ToString(".00")
    
            Else
                tbStep.Text = ""
                tbStride.Text = ""
                tbHeight.Text = ""
                tbLength.Text = ""
            End If
    
        End Sub

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,943

    Re: Cursor in textbox keeps moving to begining during key-in

    That's not normal behavior, so I'd have to say that you are doing it by some means. You must have code that is setting the cursor location, and that code is getting triggered when you don't want it to. It may be something a bit obscure, such as setting focus.
    My usual boring signature: Nothing

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Apr 2017
    Posts
    22

    Re: Cursor in textbox keeps moving to begining during key-in

    I agree with that being unusual behavior.

    I believe it has to do with me having two events, one for the height and one for the length. When you update the textbox for height, it updates the textbox for length, and vice versa. They are both TextChanged events, which is probably whats causing all my issues. I am not sure how to correct this issue. I will include the code for both events.

    Code:
    Private Sub tbHeight_TextChanged(sender As Object, e As EventArgs) Handles tbHeight.TextChanged
            Dim foot As Decimal
    
            If IsNumeric(tbHeight.Text) Then
    
                foot = Val(tbHeight.Text) * My.Settings.Foot
    
                tbLength.Text = foot.ToString(".00")
    
            Else
                tbStep.Text = ""
                tbStride.Text = ""
                tbHeight.Text = ""
                tbLength.Text = ""
            End If
    
        End Sub
    
        Private Sub tbLength_TextChanged(sender As Object, e As EventArgs) Handles tbLength.TextChanged
            Dim height As Decimal
    
    
            If IsNumeric(tbLength.Text) Then
    
                height = Val(tbLength.Text) / My.Settings.Foot
    
                tbHeight.Text = height.ToString(".00")
    
    
            Else
                tbStep.Text = ""
                tbStride.Text = ""
                tbHeight.Text = ""
                tbHeight.Text = ""
            End If
        End Sub

  4. #4
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: Cursor in textbox keeps moving to begining during key-in

    Yes, that is the problem.
    If you change the text in one, it updates the other, which causes a text change event, which then updates the one you're typing in. When you update the text you're replacing the text so the old position of the cursor is no longer applicable so the cursor is placed at the beginning of the new text.

    There are many ways to fix this. The first that comes to mind is to have a variable that you set to indicate which textbox you're typing into, e.g. in the KeyDown event of tbHeight, set an integer to 1 and in the KeyDown event of tbLength set it to 2.
    Then in the TextChanged event you can just protect the code by checking the value and ignoring the TextChanged code if the value doesn't match, e.g. :
    Code:
        Private Sub tbLength_TextChanged(sender As Object, e As EventArgs) Handles tbLength.TextChanged
            Dim height As Decimal
    
            If ActiveTextBox = 2 Then
                If IsNumeric(tbLength.Text) Then
    
                    height = Val(tbLength.Text) / My.Settings.Foot
    
                    tbHeight.Text = height.ToString(".00")
    
    
                Else
                    tbStep.Text = ""
                    tbStride.Text = ""
                    tbHeight.Text = ""
                    tbHeight.Text = ""
                End If
            End If
        End Sub

  5. #5
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,181

    Re: Cursor in textbox keeps moving to begining during key-in

    The problem is caused by the (".00") If you remove it the problem goes away.

    Passel has given you a very nice solution. The textbox your entering data into will not be adding the .00 format as your typing so I would add that function to the Validating Event
    Code:
        Private Sub tbHeight_Validating(sender As Object, e As CancelEventArgs) Handles tbHeight.Validating
            Dim decNum As Decimal
            If Decimal.TryParse(tbHeight.Text, decNum) Then
                tbHeight.Text = String.Format("{0:n2}", decNum)
            End If
        End Sub

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width