TStringGrid输入验证只允许C ++ Builder XE8

时间:2017-05-31 23:42:37

标签: delphi c++builder c++builder-xe8

这个问题是另一个问题的延伸:

TEdit Input Validation on C++ Builder XE8

我有一个可编辑的TStringGrid。我只希望用户为网格中的每个单元格键入数字和最多一个小数点或逗号。

从上面的链接中,我了解了如何仅允许某些键,但不知道如何跟踪给定单元格中已存在某个键值的次数。

从上面的链接,我有这个:

void __fastcall TSetDataForm::ProbabilityGridKeyPress(TObject *Sender, System::WideChar &Key)
{
    if( Key == VK_BACK )
        return;

    if( (Key < L'0') || (Key > L'9') )
    {
        ShowMessage("Please enter numerals only");
        Key = 0;
    }
}

如何允许'.'','但只允许一次?

2 个答案:

答案 0 :(得分:3)

我建议使用TryStrToFloat()验证输入,然后毫无疑问用户是否输入了有效的十进制字符串。您只需要处理以下情况:

  • 用户正在键入编辑字段末尾以外的某个位置,无论是否选择了文字。

  • 用户将文本复制/粘贴到编辑器中。

例如:

class TStringGridAccess : public TStringGrid
{
public:
    __property InplaceEditor;
};

void __fastcall TSetDataForm::ProbabilityGridKeyPress(TObject *Sender, System::WideChar &Key)
{
    switch (Key)
    {
        case 3: // Ctrl-C
        case 8: // Backspace
            return;

        case 22: // Ctrl-V
        {
            Key = 0;

            TInplaceEdit *Editor = ((TStringGridAccess*)ProbabilityGrid)->InplaceEditor;

            String SaveCellText = ProbabilityGrid->Cells[ProbabilityGrid->Col][ProbabilityGrid->Row];

            String SaveEditText = Editor->Text;
            int SaveSelStart = Editor->SelStart;
            int SaveSelLen = Editor->SelLength;

            Editor->Perform(WM_PASTE, 0, 0);

            TFormatSettings fmt = TFormatSettings::Create();
            fmt.DecimalSeparator = _D('.');

            double value;
            if (TryStrToFloat(Editor->Text, value, fmt))
                return;

            ProbabilityGrid->Cells[ProbabilityGrid->Col][ProbabilityGrid->Row] = SaveCellText;

            Editor->Text = SaveEditText;
            Editor->SelStart = SaveSelStart;
            Editor->SelLength = SaveSelLen;

            break;
        }

        case _D('0'):
        case _D('1'):
        case _D('2'):
        case _D('3'):
        case _D('4'):
        case _D('5'):
        case _D('6'):
        case _D('7'):
        case _D('8'):
        case _D('9'):
        case _D('.'):
        {
            TInplaceEdit *Editor = ((TStringGridAccess*)ProbabilityGrid)->InplaceEditor;

            String str = Editor->Text;
            int idx = Editor->SelStart;
            int len = Editor->SelLength;

            String str2 = str.SubString(1, idx) + Key + str.SubString(1+idx+len, MaxInt);

            TFormatSettings fmt = TFormatSettings::Create();
            fmt.DecimalSeparator = _D('.');

            double value;
            if (TryStrToFloat(str2, value, fmt))
                return;

            break;
        }
    }

    ShowMessage(_D("Please enter decimals only"));
    Key = 0;
}

答案 1 :(得分:1)

我的问题源于这样一个事实,即我不知道如何阅读用户正在写的单元格的文本。

以下是我感兴趣的人的解决方案:

void __fastcall TSetDataForm::ProbabilityGridKeyPress(TObject *Sender, System::WideChar &Key)
{
    if( Key == VK_BACK )
        return;

    if( !((Key >= L'0') && (Key <= L'9') || (Key == L'.')))
    {
        ShowMessage("Please enter numerals only");
        Key = 0;
    }
    else if ((Key == L'.') &&
        (Pos(Key, ProbabilityGrid->Cells[ProbabilityGrid->Col][ProbabilityGrid->Row]) > 0))
    {
        ShowMessage("Two dots!");
        Key = 0;
    }
}