Ver Mensaje Individual
  #5  
Antiguo 07-09-2014
Avatar de ecfisa
ecfisa ecfisa is offline
Moderador
 
Registrado: dic 2005
Ubicación: Tres Arroyos, Argentina
Posts: 10.508
Reputación: 38
ecfisa is a splendid one to beholdecfisa is a splendid one to beholdecfisa is a splendid one to beholdecfisa is a splendid one to beholdecfisa is a splendid one to beholdecfisa is a splendid one to beholdecfisa is a splendid one to behold
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
__________________
Daniel Didriksen

Guía de estilo - Uso de las etiquetas - La otra guía de estilo ....
Responder Con Cita