Hola aguml.
Si tenes razón, la función falla en el caso que comentas. Y la tuya también lo hace cuando detecta un error al salir del Edit. Luego de mostrar el mensaje y asignarle nuevamente el foco y estándo el texto selecionado, permite la introducción de un número ya que los valores de
SelStart y
SelLength no son actualizados.
Sin embargo, pude comprobar que se simplifica el tratamiento con el uso de
String:
Código:
...
String wrongChar = "|°¬!\"#$%&/()='?\\¿¡@´¨+*~{[^}]`<>,;.:-";
void __fastcall TForm1::FormCreate(TObject *Sender)
{
for(int i=0;i < ComponentCount; i++)
if(Components[i]->ClassNameIs("TEdit"))
(static_cast<TEdit*>(Components[i]))->Clear();
}
void __fastcall TForm1::EditKeyPress(TObject *Sender, char &Key)
{
TEdit *ed = static_cast<TEdit*>(Sender);
if(ed->SelStart+ed->SelLength == 0 && (char)Key>='0' && (char)Key<='9' ||
wrongChar.AnsiPos((char)Key)!=0) {
MessageBeep(MB_ICONERROR);
Key=0;
}
}
void __fastcall TForm1::EditExit(TObject *Sender)
{
TEdit *ed = static_cast<TEdit*>(Sender);
bool invalid = false;
if(ed->Text == "") return;
if(ed->Text[1]>='0'&&ed->Text[1]<='9'|| wrongChar.AnsiPos(ed->Text[1])!=0)
invalid = true;
for(int i = 2; i <= ed->Text.Length(); i++)
if(wrongChar.AnsiPos(ed->Text[i]))
invalid = true;
if (invalid) {
MessageBeep(MB_ICONERROR);
ed->SetFocus();
ed->SelStart = 0;
ed->SelLength = 0;
}
}
Saludos
