|
-
Sep 8th, 2007, 01:53 AM
#1
Thread Starter
Junior Member
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
-
Sep 8th, 2007, 02:03 AM
#2
Frenzied Member
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:
Private Sub Text1_GotFocus()
Me.Text1.SelStart = Len(Me.Text1)
End Sub
-
Sep 8th, 2007, 02:13 AM
#3
Frenzied Member
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:
elseif keyascii = 8 then
exit sub
-
Sep 8th, 2007, 02:20 AM
#4
Frenzied Member
Re: Using BackSpace in Text box
by the way if you want to restrict user input
vb Code:
Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii = 8 Or KeyAscii = 9 Or KeyAscii = 10 Then Exit Sub
If InStr("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ/", Chr(KeyAscii)) = 0 Then KeyAscii = 0
End Sub
in the instr() part you can ensure what key can be input even space. It was found in this forum
-
Sep 8th, 2007, 03:12 AM
#5
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.
-
Sep 8th, 2007, 03:24 AM
#6
Frenzied Member
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
-
Sep 9th, 2007, 06:33 PM
#7
Thread Starter
Junior Member
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
-
Sep 9th, 2007, 09:18 PM
#8
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
-
Sep 9th, 2007, 09:23 PM
#9
Thread Starter
Junior Member
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
-
Sep 11th, 2007, 02:02 AM
#10
Frenzied Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|