Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Backspace causes a lose focus.. 7

Status
Not open for further replies.

thatguy

Programmer
Aug 1, 2001
283
US
Hey there folks--

Maybe I'm searching for the wrong keywords, but I've been looking for a way to prevent backspace from causing a textbox to lose focus? I've checked the "Enter or Tab to exit fields" box on the Options->General tab and I've also included SET CONFIRM ON in my main prog (even tried it in the form's Init()), but still, when I backspace on an empty textbox, focus goes to the previous control.

I'm confused... Any thoughts?

Thanks
-- frank~
 
Frank,


You could try to use following code in your KeyPress Event of the TextBox which you donot want to get out of focus, the drawback is that one cannot use the Backspace to correct a typo, maybe you advise to use the BackArrow to do that.
Code:
* KeyPress Event
Lparameters nKeyCode, nShiftAltCtrl

BackSpaceTrue=nKeyCode
If BackSpaceTrue=127
	This.setfocus()
Endif
Return

Koen
 
Hi Frank,

I would try, also in the keypress event.
Code:
IF EMPTY(THIS.VALUE) AND nKeyCode = 127
   NODEFAULT
ENDIF
Regards

Mike
 
wow... so there's no switch or SET to control this? the only way to prevent the backspace from changing to another control is to monitor the keys? that seems really odd... maybe an option to add to VFP 10..

thanks
-- frank~
 
Frank,

so there's no switch or SET to control this? the only way to prevent the backspace from changing to another control is to monitor the keys?

I'm surprised that you are surprised. Why should there be a property to control this particular behaviour? It's not as it is something that we need to do everyday.

Mike


__________________________________
Mike Lewis (Edinburgh, Scotland)

My sites:
Visual FoxPro (www.ml-consult.demon.co.uk)
Crystal Reports (www.ml-crystal.com)
 
Really? I'm thinking about the case where there's text in a text box that you want cleared out.. most users don't know about Shift+Home then Del (and using the mouse to select all the text is time-consuming).. instead they just hold backspace until the text is gone, but when they do that, I want the cursor to stay in the textbox. Is that strange?

-- frank~
 
My preference is to use SelectOnEntry = .T.

Regards,

Mike
 
I've got that set..

this whole problem started with a "quick search" text box, which does an incremental search on a grid. so the user types in the first few letters of the search, finds the record they're looking for, then they backspace to erase the search text and start a new search. *shrug* hard to believe I'm the only one bugged by this.. oh well..

thanks anyway all
-- frank~
 
Frank,

If you don’t mind to do some dirty coding, make a property to your form BackSpaceTrue and use the following coding. This will allow your users to use the BackSpace to erase the contents but will prevent them to accidentally skip to the previous text box as well. It works in my testing.

Code:
* Valid Event 
Para KeyCode
With thisform
	If vartype(.Backspacetrue)='N'
		If .Backspacetrue=127 AND THIS.SELSTART<1
			=MESSAGEBOX( "You are about to delete the previous text box as well this should not be done"+;
			"Click OK Button and Press Spacebar", 48, "Backspace Key" )  
			Return .F.
		Endif
	Endif
Endwith

*KeyPress Event
Lparameters nKeyCode, nShiftAltCtrl
With thisform
	.BackSpaceTrue=nKeyCode
	If .BackSpaceTrue=127
		This.valid(.BackSpaceTrue)
	Endif
Endwith
Return

Koen
 
I agree with thatguy, I can easily see the value of an option for backspace not pop me out of an entry box, so I'll give mspratt and KoenPiller stars. Of course, that can open a can of worms, since ther are lots of other assorted control characters...
 
OK, I'll accept for the moment that it might be desirable to have this property (but I'm not convinced).

However, ThatGuy, if you are trying to implement an icremental search, it is much easier to put the code in the IncrementalChange rather than the KeyPress. That way, you only have to look at the current Value to the see string entered so far; you don't have to worry about trapping control keys such as backspace or del.

Mike


__________________________________
Mike Lewis (Edinburgh, Scotland)

My sites:
Visual FoxPro (www.ml-consult.demon.co.uk)
Crystal Reports (www.ml-crystal.com)
 
As another way of achieving this, I have the following code in the Valid event of my textbox base class:
Code:
*!* to prevent users backspacing off the textbox control
IF EMPTY(THIS.VALUE) AND LASTKEY() = _BackSpaceKey
    KEYBOARD "{DEL}"
    RETURN 0
ENDIF


Hope that helps,

Stewart
PS If you want to get the best response to a question, please check out FAQ184-2483 first.
 
The simplest solution that I have found goes as follows:
In the Valid of the textbox, put the following commands:

Code:
** avoid message 'Invalid Input'
** tip : store current NOTIFY-setting in a Form-property
SET NOTIFY OFF
** selstart is the cursor-position
IF this.selstart < 1 AND LASTKEY() = 127
  RETURN .F.          && that's all folks
ENDIF

Greetings from DoctorNix.
 
Note: StewartUk uses EMPTY(THIS.VALUE) in his code, but this is not what you want. If you have type 'ABCD', and then 4 times <Leftarrow>, your cursor will be at the start of the textbox. If you then type <Backspace>, your (this.value) will NOT be empty, and your textbox WILL lose focus.
Just thought I'd mention it.
 
DoctorNix,

Oh yes, I might have to think about that.

I think there is an issue with your code as it stands, in that you can't then click off the object because LASTKEY is still BackSpace. At least that's what I seem to get when I tested it.

By the way, if you RETURN 0 from a Valid event you get the same effect as RETURN .F. without the WAIT WINDOW.

Stewart
 
Keith,

After you search, set the value of your text box to "".
That way the text box is ready for the user to enter their next search.


I disagree with that -- user interface-wise. When they get the error, chances are the user will want to check their spelling, and perhaps edit the search term. I prefer to highlight the entire contents of the text box rather than clear it.

Mike


__________________________________
Mike Lewis (Edinburgh, Scotland)

My sites:
Visual FoxPro (www.ml-consult.demon.co.uk)
Crystal Reports (www.ml-crystal.com)
 
Why not the best of everyone's ideas:

Keypress()

if This.SelStart == 0 and nKeyCode = 127
nodefault
endif

Malcolm
 
Thanks for the tip about the return 0, StewartUK.
Question: when do you click outside the box, and where ?
I don't see strange behaviour in my test-environmant
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top