Einzelnen Beitrag anzeigen

Benutzerbild von Helmi
Helmi

Registriert seit: 29. Dez 2003
Ort: Erding, Republik Bayern
3.312 Beiträge
 
Delphi XE2 Professional
 
#23

Re: nimmt meine zahlen nicht im stringgrid trotz strtoint

  Alt 1. Jan 2009, 17:16
Zitat von Helmi:
Es müsste dann so heissen:

Delphi-Quellcode:
var a,b,c,d:integer;
procedure TForm2.Button1Click(Sender: TObject);
begin
try
    a := StrToInt(StringGrid1.Cells[0, 0]) ;
    b := StrToInt(StringGrid1.Cells[1, 0]) ;
    c := StrToInt(StringGrid1.Cells[2, 0]) ;
    d := StrToInt(StringGrid1.Cells[3, 0]) ;
except on EConvertError do
      showmessage ('Ungültige Eingabe');
      end;
oder um das Except wegzubekommen:
Delphi-Quellcode:
var a,b,c,d:integer;
procedure TForm2.Button1Click(Sender: TObject);
begin
  a := StrToIntDef(StringGrid1.Cells[0, 0], -1) ;
  b := StrToIntDef(StringGrid1.Cells[1, 0], -1) ;
  c := StrToIntDef(StringGrid1.Cells[2, 0], -1) ;
  d := StrToIntDef(StringGrid1.Cells[3, 0], -1) ;

  If (a = -1) or (b = -1) or (c = -1) or (d = -1) then
    ShowMessage ('Ungültige Eingabe');
end;
So wird bei einem Fehler (keine Zahl eingegeben in der StringGrid-Zelle) -1 als Defaultwert zurückgegeben.
Wenn ein Feld -1 beinhaltet, dann kommt die Meldung.
(Da geht natürlich nur, wenn die erlaubte Eingabe > -1 ist, also ab 0 positiv werdend...
Oder:
Delphi-Quellcode:
var a,b,c,d:integer;
procedure TForm2.Button1Click(Sender: TObject);
begin
  If not TryStrToInt(StringGrid1.Cells[0, 0], a) or
     not TryStrToInt(StringGrid1.Cells[1, 0], b) or
     not TryStrToInt(StringGrid1.Cells[2, 0], c) or
     not TryStrToInt(StringGrid1.Cells[3, 0], d) then
    ShowMessage ('Ungültige Eingabe');
end;
(So wärst unabhängig von der Eingabe - aber auf die Eingabe von Nicht-Zahlen wird trotzdem geprüft...)
mfg
Helmi

>> Theorie ist Wissen, dass nicht funktioniert - Praxis ist, wenn alles funktioniert und keiner weiss warum! <<
  Mit Zitat antworten Zitat