//当产生 KeyPress 事件时, 滤掉无效的字符, 也可以在发现无效字符时提示用户, 本程序里未做提示, 只是按键无效(滤掉了)
void __fastcall TYbCustomInplaceEdit::KeyPress(char &Key) //相当于 OnKeyPress 事件
{
if(fProcessKey(Key))
{
TCustomEdit::KeyPress(Key);
}
}
//---------------------------------------------------------------------------
//不同的检查要求采用不同的方法, 例如整数、浮点数、16进制等
//---------------------------------------------------------------------------
char __fastcall TYbCustomInplaceEdit::fProcessKey(char &Key)
{
switch(_vtType)
{
case vtInteger : fProcessKeyInteger(Key); break;
case vtIntegerPs: fProcessKeyIntegerPs(Key); break;
case vtFloat : fProcessKeyFloat(Key); break;
case vtFloatPs : fProcessKeyFloatPs(Key); break;
case vtHex : fProcessKeyHex(Key); break;
case vtHexUp : fProcessKeyHexUp(Key); break;
case vtHexLo : fProcessKeyHexLo(Key); break;
case vtCustom : fProcessKeyCustom(Key); break;
}
return Key;
}
//---------------------------------------------------------------------------
//整数的方法 (其它略)
//---------------------------------------------------------------------------
void __fastcall TYbCustomInplaceEdit::fProcessKeyInteger(char &Key)
{
if((Key>=0x20)||(Key<0)) //printable character
{
if((Key<'0') || (Key>'9'))
{
if(Key=='-')
{
if((SelStart!=0) || ((SelLength<=0) && (Text.SubString(1,1)=="-")))
Key = 0;
}
else
{
Key = 0;
}
}
else //'0' - '9'
{
if((SelStart==0)&&(SelLength<1)&&(Text.Trim().SubString(1,1)=="-"))
Key=0;
}
}
}
//---------------------------------------------------------------------------
//要拦截“粘贴”消息, 不让无效字符“粘”进来
//---------------------------------------------------------------------------
void __fastcall TYbCustomInplaceEdit::WMPaste(TMessage &Message)
{
if((ReadOnly) || (_vtType==vtNone))
{
TCustomEdit::Dispatch(&Message);
}
else
{
AnsiString TxtInPst, Txt2Pst;
if(Clipboard()->HasFormat(CF_TEXT))
TxtInPst = Clipboard()->AsText;
char Key;
int n=TxtInPst.Length();
for(int i=1; i<=n; i++)
{
Key = TxtInPst[i];
if(fProcessKey(Key))
Txt2Pst += Key;
}
if(!Txt2Pst.IsEmpty())
{
SelText = Txt2Pst;
}
}
}
//---------------------------------------------------------------------------
--------------------next---------------------
阅读(1294) | 评论(0) | 转发(0) |