Search code examples
firemonkeyc++builderc++builder-10.3-rio

StringReplace behavoir on iOS


I have a TEdit box and in it's KeyUp event i am replacing any occurrence of "-" with a blank, "". When i run on Win32 it works exactly as i expect - anytime i type the - key it shows up briefly and then is removed.

When i run on iOS and repeatedly press the - key i get the result of deleting the last character in the Edit box every other time i press -. The exception is the 2nd and 3rd presses in which i delete the last character both times. It should just be removing the -.

e.g. If i start out with 123456 in the Edit and start pressing just the minus key i get what you see in this gif below:

enter image description here

In tabular form the results are these:

enter image description here

void __fastcall TForm1::EditConstantKeyUp(TObject *Sender, WORD &Key, System::WideChar &KeyChar,
      TShiftState Shift)
{
 if (KeyChar == 45) {  // minus key pressed  (ascii code for that key is 45)
 EditConstant->Text = StringReplace(EditConstant->Text, "-", "", TReplaceFlags() << rfReplaceAll);
}

What fundamental thing am i missing here? I'm working in Rad Studio 10.3.2 using C++ Builder.


Solution

  • The correct way to handle this is to set the Key/KeyChar parameter to 0 to dismiss the keystroke, and not manipulate the TEdit::Text at all.

    Try something like this:

    // or, use the OnKeyDown event instead...
    void __fastcall TForm1::EditConstantKeyUp(TObject *Sender,
        WORD &Key, System::WideChar &KeyChar, TShiftState Shift)
    {
        if (Key == 0)
        {
            if (KeyChar == _D('-'))
                KeyChar = 0;
        }
        else
        {
            if ((Key == vkMinus) || (Key == vkSubtract))
                Key = 0;
        }
    }