none
Manipulatig keystrokes as you type

    Question

  • Dear friends,

    I want to capitalise each words first letter as user types in a textbox.

    I have creates a class (myTxtBox) that has an additional property 'capitalise'. This property is set to .t. in mytxtbox.gotfocus if it is empty.

    I don't know how to manipulate the keystrokes as the user types I tried it in my way by using following code in its keypress event.

    LPARAMETERS nKeyCode, nShiftAltCtrl
    IF ISALPHA(CHR(nKeyCode)) and this.capitalise
    	nkeycode=nkeycode-IIF(nkeycode>96,32,0)
    	this.capitalise=.f.
    ELSE 
    	this.capitalise=INLIST(CHR(nKeyCode),' ','.','/','(',')','[',']')
    ENDIF
    	 

    Thursday, November 08, 2012 8:41 AM
    Answerer

Answers

  • Dear Pavel sir, I tried your code. But the cursor returns to the starting position after typing one of the following characters ' ','.','/','(',')','[',']'. So, if I type 'visual foxpro 9.0' in it, it looks like '09. Foxpro Visual'. - Ravi

    I still don't understand why are you mixing my code with ' ','.','/','(',')','[',']' characters which are nowhere in my code so they cannot cause the behavior you are describing. Start with a new empty form and one editbox and you'll see...

    Or you may test the following code:

    RELEASE oForm
    PUBLIC oForm
    oForm = CREATEOBJECT('Form')
    oForm.AddObject('Edt','ProperEditBox')
    oForm.Edt.Width = oForm.Width
    oForm.Edt.Height = oForm.Height
    oForm.Edt.Anchor = 15
    oForm.Edt.Visible = .T.
    oForm.Caption = "Type any text and check the cursor position"
    oForm.Visible = .T.
    DEFINE CLASS ProperEditBox AS EditBox 
    PROCEDURE InteractiveCHange
    THIS.Value = PROPER(THIS.Value)
    ENDDEFINE

    Friday, November 09, 2012 5:56 PM
    Moderator

All replies

  • I would rather do it in InteractiveChange event:

    IF THIS.Capitalise
    THIS.Value = PROPER(THIS.Value)
    ENDIF

    Thursday, November 08, 2012 9:13 AM
    Moderator
  • Thanks for reply. But I got it some other way as;
    LPARAMETERS nKeyCode, nShiftAltCtrl
    IF ISALPHA(CHR(nKeyCode)) and this.capitalise
    	nkeycode=nkeycode-IIF(nkeycode>96,32,0)
            keyboard chr(nkeycode)
    	this.capitalise=.f.
            nodefault
    ELSE 
    	this.capitalise=INLIST(CHR(nKeyCode),' ','.','/','(',')','[',']')
    ENDIF
    	 
    It does not disturb the user while typing.
    Thursday, November 08, 2012 9:22 AM
    Answerer
  • Dear Pavel sir, I tried your code. But the cursor returns to the starting position after typing one of the following characters ' ','.','/','(',')','[',']'. So, if I type 'visual foxpro 9.0' in it, it looks like '09. Foxpro Visual'. - Ravi
    Thursday, November 08, 2012 3:09 PM
    Answerer
  • Hi Ravindra,

    why don't you change the input value from within the valid event of your textbox?

    I might be enough, to automatically correct the value when the User finished the input.And in that case, Pavel reply would do.


    Gruss / Best regards
    -Tom
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible,
    you are, by definition, not smart enough to debug it. 010101100100011001010000011110000101001001101111011000110110101101110011


    Thursday, November 08, 2012 3:20 PM
    Answerer
  • Dear Pavel sir, I tried your code. But the cursor returns to the starting position after typing one of the following characters ' ','.','/','(',')','[',']'. So, if I type 'visual foxpro 9.0' in it, it looks like '09. Foxpro Visual'. - Ravi

    It just means you are not executing my code because my code works correctly on all my computers...

    BTW, foxpro should be converted to FoxPro :-)


    Thursday, November 08, 2012 3:23 PM
    Moderator
  • Dear Tom and Pavel sirs,

    Please dont missunderstand me. I am not saying that my code is superior.  

    I came accros a problem that I posted earlier while using InterActiveChange event.

    As Tom sir pointed out, Valid event will be come into action only after the user tabbing out of the field.

    I want to get it done when the user is typing.

    In KeyPress it is achived. The characters are changed silently and moreover cursor also remains at its position. So the user can continue his typing without stumbling. 

    Shall I change type of this conversation from Question to General discussion?

    -Ravi

    Friday, November 09, 2012 8:36 AM
    Answerer
  • Hi Ravindra,

    then simply rap an IF statement around Pavel's code sample like this:

    LPARAMETERS nKeyCode, nShiftAltCtrl
    IF BETWEEN( nKeyCode , 98 , 122 )
    	THIS.Value = PROPER(THIS.Value)
    	This.SelStart = LEN(ALLTRIM(This.Value))
    ENDIF 

    This may not be the optimal solution, especially if you can enter more than one word, as the second word will set properly too. But you get enough space to add further conditions i.e. by reading and writing the first char only...

    LPARAMETERS nKeyCode, nShiftAltCtrl
    IF BETWEEN( nKeyCode , 98 , 122 )
    	liPos = This.SelStart 
    	THIS.Value = PROPER(LEFT(This.Value,1)) + SUBSTR(This.Value,2)
    	This.SelStart = liPos
    ENDIF 

    so...lots of possibilities ;-)


    Gruss / Best regards
    -Tom
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible,
    you are, by definition, not smart enough to debug it. 010101100100011001010000011110000101001001101111011000110110101101110011

    Friday, November 09, 2012 9:01 AM
    Answerer
  • If I remove everything from Keypress method and leave just the PROPER() function in InteractiveChange method then it works without any side effect. (VFP 9 SP1)

    Does the PROPER() function fit to your needs?

    The question/discussion switch is not important here.

    Friday, November 09, 2012 9:02 AM
    Moderator
  • DEAR PAVEL,

    I TRIED YOUR SUGGESSTION, BUT THE PROBLEM PERSISTS. I AM USING VFP SP2. IS THIS A CAUSE? 

     

    Dear Tom,

    I tried your solution also. It is working for a single word only. But because of alltrim it is not accepting spaces.

     

    Thanks to both of you. 

    -Ravi

    Friday, November 09, 2012 9:21 AM
    Answerer
  • Install SP1 and you'll see. I would guess the problem is somewhere else.
    Friday, November 09, 2012 9:23 AM
    Moderator
  • SP2 is 1 step forward. Isn't it?

    Then why to go 1 step backward to sp1?

    Friday, November 09, 2012 4:44 PM
    Answerer
  • SP2 is 1 step forward. Isn't it?

    Then why to go 1 step backward to sp1?

    It is on your decision... I am not saying stay with SP1. You were asking if SP2 is the cause so my answer was "Install SP1 to see if the error is new to SP2".

    And even the SP2 has several releases... so you may try all of them. (Some of them are not so perfect.)

    Friday, November 09, 2012 4:54 PM
    Moderator
  • Dear Pavel sir, I tried your code. But the cursor returns to the starting position after typing one of the following characters ' ','.','/','(',')','[',']'. So, if I type 'visual foxpro 9.0' in it, it looks like '09. Foxpro Visual'. - Ravi

    Well, also on VFP9 SP2 (5815), using Pavel's code I get "Ekhcsod Falo" when typing olaf doschke, the cursor goes back to position one after each change of the value.

    I don't know wha't causing this, maybe it's not the SP version but a simple setting, eg SET CONFIRM.

    What works is

    lnSelStart = This.SelStart
    THIS.Value = PROPER(THIS.Value)
    This.SelStart = lnSelStart



    Friday, November 09, 2012 5:36 PM
  • Dear Pavel sir, I tried your code. But the cursor returns to the starting position after typing one of the following characters ' ','.','/','(',')','[',']'. So, if I type 'visual foxpro 9.0' in it, it looks like '09. Foxpro Visual'. - Ravi

    I still don't understand why are you mixing my code with ' ','.','/','(',')','[',']' characters which are nowhere in my code so they cannot cause the behavior you are describing. Start with a new empty form and one editbox and you'll see...

    Or you may test the following code:

    RELEASE oForm
    PUBLIC oForm
    oForm = CREATEOBJECT('Form')
    oForm.AddObject('Edt','ProperEditBox')
    oForm.Edt.Width = oForm.Width
    oForm.Edt.Height = oForm.Height
    oForm.Edt.Anchor = 15
    oForm.Edt.Visible = .T.
    oForm.Caption = "Type any text and check the cursor position"
    oForm.Visible = .T.
    DEFINE CLASS ProperEditBox AS EditBox 
    PROCEDURE InteractiveCHange
    THIS.Value = PROPER(THIS.Value)
    ENDDEFINE

    Friday, November 09, 2012 5:56 PM
    Moderator