Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Determine Significant Trailing Spaces in controls
Message
From
02/08/2007 21:14:30
 
 
To
02/08/2007 12:30:17
General information
Forum:
Visual FoxPro
Category:
Coding, syntax & commands
Environment versions
Visual FoxPro:
VFP 9 SP1
OS:
Windows XP SP2
Miscellaneous
Thread ID:
01245320
Message ID:
01245722
Views:
22
>>>>How do you handle END key?
>>>
>>>Now it's a while since I used trick, but Iseem to remember that I had something like this in Keypress:
>>>
>>>if nKeyCode=6
>>> This.selstart=at(this.value,chr(176))
>>>endif
>>>
>>>
>>>BTW, I now see an ever better-looking solution. Some fonts show chr(255) as a blank, meaning that your users will not notice any visible difference at all. So chr(176) can be replaced with chr(255) in my sample.
>>
>>Wouldn't SHIFT+END, DEL breake it?
>
>DEL will not, unless the user holds it down for many seconds. Shift-End can be trapped, as far as I know. But as I wrote earlier, the user can probably fool the code in theory, but in practice that never was a problem for my users.
>
>If you really want to make my solution bullet proof, you can add some code to make sure that the string never gets filled with spaces at the end, something like
>
>if rat(this.value,chr(32))>20
> this.value=padr(this.value,rat(this.value,chr(32)),chr(176),1000)
>endif

I think I agree with Sergey on the END key and various combinations. It might also be confusing if the user uses the cursor key to the right and then finds "spaces" when they expected to find the end. So far I have the following solution -- so far it works with the exception of handling text that is inserted from the clipboard.

Basically I have a custom combo-dropdown class that has a custom property defined:
Space = =CHR(168)     && This is the space replacement character
Then I have the following methods defined:
Procedure KeyPress
LPARAMETERS nKeyCode, nShiftAltCtrl
LOCAL lcBegText, lcEndText
DO CASE
  CASE nKeyCode = 7     && Delete key
    DO CASE
      CASE this.SelLength >= LEN(this.Tag) .AND. this.SelStart = 0
        lcBegText = ""
        lcEndText = ""
      CASE this.SelLength > 0 .AND. this.SelLength < LEN(this.Tag)
        lcBegText = LEFT(this.Tag,this.SelStart)
        lcEndText = SUBSTR(this.Tag,this.SelStart+this.SelLength+1)
      OTHERWISE
        lcBegText = LEFT(this.Tag,this.SelStart)
        lcEndText = SUBSTR(this.Tag,this.SelStart+2)
    ENDCASE
    this.Tag = lcBegText + lcEndText

  CASE nKeyCode = 127   && Backspace key
    IF this.SelLength = 0
      lcBegText = IIF(this.SelStart>1,LEFT(this.Tag,this.SelStart-1),"")
      lcEndText = SUBSTR(this.Tag,this.SelStart+1)
    ELSE
      lcBegText = IIF(this.SelStart>1,LEFT(this.Tag,this.SelStart),"")
      lcEndText = SUBSTR(this.Tag,this.SelStart+this.SelLength+1)
    ENDIF
    this.Tag = lcBegText + lcEndText

  CASE BETWEEN(nKeyCode,32,126)   && Printable key
    DO CASE
*-*   Inserting character end of text
      CASE this.SelStart = LEN(this.Tag) .AND. this.SelLength = 0
        lcBegText = this.Tag
        lcEndText = ""

*-*   Inserting character in mid text
      CASE this.SelStart < LEN(this.Tag) .AND. this.SelLength = 0
        lcBegText = LEFT(this.Tag,this.SelStart)
        lcEndText = SUBSTR(this.Tag,this.SelStart+1)

*-*   Replacing character(s)
      CASE this.SelStart < LEN(this.Tag) .AND. this.SelLength > 0
        lcBegText = LEFT(this.Tag,this.SelStart)
        lcEndText = SUBSTR(this.Tag,this.SelStart+this.SelLength+1)
      OTHERWISE
        WAIT WINDOW "ERROR: Unhandled Keyboard Entry"
        lcBegText = this.Tag
        lcEndText = ""
    ENDCASE
    this.Tag = lcBegText + CHR(nKeyCode) + lcEndText
ENDCASE
this.Tag = STRTRAN(this.Tag,CHR(32),this.Space)
EndProc


Procedure GotFocus
this.DisplayValue = this.Tag
EndProc
I initialize the Tag property with the initial value for display. This way if the user had trailing spaces in the initial text, they will be preserved. The space is replaced in the Tag property with the Space property value. I have set the Space property to the value Ascii 168 which seemed to be the least offensive. I tried using 160 (which for the Arial font is blank) but the dropdown combo would still trim it from the display value.

Right now the above code works with the exception of the instance where the user can paste a text into the display. I am still working on that.

It might be better to instead use the textbox laid on top of a dropdown combo and then use the method from the previous thread. But I am still trying to keep it to the combobox. If this is used then I also need to handle the tab key (auto-advance past the combobox from the textbox) and always set focus back to the textbox on selection from the combobox dropdown.
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform