Results 1 to 10 of 10

Thread: Using BackSpace in Text box

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Aug 2007
    Posts
    25

    Post Using BackSpace in Text box

    In programs I'm writing in VB5, I would like to give the User the option to remove the last character in a text box using the backspace key.

    I use this code in a _keypress event procedure :
    If ........... then
    ................
    elseif ................. then
    ................
    elseif ................. then
    .................
    elseif keyascii = 8 then
    txtABC.text = Left$(txtABC.text, len(txtABC.text) - 1)
    else
    ..................
    end if

    This does the job allright, BUT when the shortened text string is displayed in the text box, the cursor is placed at the start of the line of text, whereas I
    would prefer it was placed at the end of the string where it should be.

    Is there any code I can add to force the cursor to the end of string.

    Warren Sugden

  2. #2
    Frenzied Member
    Join Date
    Aug 2006
    Location
    India, Punjab, Bhatinda
    Posts
    1,689

    Re: Using BackSpace in Text box

    Is there any code I can add to force the cursor to the end of string
    why not?
    vb Code:
    1. Private Sub Text1_GotFocus()
    2.     Me.Text1.SelStart = Len(Me.Text1)
    3. End Sub

  3. #3
    Frenzied Member
    Join Date
    Aug 2006
    Location
    India, Punjab, Bhatinda
    Posts
    1,689

    Re: Using BackSpace in Text box

    elseif keyascii = 8 then
    txtABC.text = Left$(txtABC.text, len(txtABC.text) - 1)
    is this necessary. I dont know about vb5 in vb6 you can simply use
    vb Code:
    1. elseif keyascii = 8 then
    2. exit sub

  4. #4
    Frenzied Member
    Join Date
    Aug 2006
    Location
    India, Punjab, Bhatinda
    Posts
    1,689

    Re: Using BackSpace in Text box

    by the way if you want to restrict user input
    vb Code:
    1. Private Sub Text1_KeyPress(KeyAscii As Integer)
    2.     If KeyAscii = 8 Or KeyAscii = 9 Or KeyAscii = 10 Then Exit Sub
    3.     If InStr("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ/", Chr(KeyAscii)) = 0 Then KeyAscii = 0
    4. End Sub
    in the instr() part you can ensure what key can be input even space. It was found in this forum

  5. #5
    Fanatic Member
    Join Date
    Jul 2007
    Location
    Essex, UK.
    Posts
    579

    Re: Using BackSpace in Text box

    Warren, it is so long since I used VB5. Can the user just not hit the backspace key and the last character is erased?

    I don't think you are looking to restrict input as the other posts suggest.

  6. #6
    Frenzied Member
    Join Date
    Aug 2006
    Location
    India, Punjab, Bhatinda
    Posts
    1,689

    Re: Using BackSpace in Text box

    I don't think you are looking to restrict input as the other posts suggest.
    it has to be. if not why should he be using so many if..elseif..endif syntax, but only WS can confirm his intentions.

    better late than never...Warren Sugden Welcome to the Forums

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Aug 2007
    Posts
    25

    Re: Using BackSpace in Text box

    Steve Grant and VBFnewcomer

    Pressing the 'BackSpace' key will not remove a character from a string of text in the text box in VB5 without the code to do so, otherwise I would not have found the need to write the code.

    The other elseif lines are to 1) limit the acceptable key presses to range of ascii values eg. keyascii => 48 and keyascii =< 57 if numbers ONLY to be accepted 2) check if default$ value offered is accepted on first key press, otherwise txtABC.text is made = "" 3) if keyascii = 8 then 'exit sub' 4) 'else' recognises all other presses and makes keyascii = 0,

    I'm totally new to being able to discuss VB code with anyone. If you see some other better ways or better code I'd be grateful.

    Warren Sugden

  8. #8
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,430

    Re: Using BackSpace in Text box

    Code:
    ElseIf keyascii = 8 Then
        txtABC.text = Left$(txtABC.Text, Len(txtABC.Text) - 1)
        txtABC.SelStart = Len(txtABC.Text) 'Cursor at the end
    Else
    ..................
    End If

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Aug 2007
    Posts
    25

    Re: Using BackSpace in Text box

    JCIS

    Vbfnewcomer has suggested that I don't need those 2 lines of code. All I need is 'exit sub' which seems to work.

    Warren Sugden

  10. #10
    Frenzied Member
    Join Date
    Aug 2006
    Location
    India, Punjab, Bhatinda
    Posts
    1,689

    Re: Using BackSpace in Text box

    I'm totally new to being able to discuss VB code with anyone
    if this is the case you could start learning to code in vs 2005 or atleaset vb6. This way you will have enough help & support from various sources.

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