Autor Beitrag
Daniel07A
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 36

XP
D7 Enterprise
BeitragVerfasst: So 15.05.05 10:00 
Hi,

1.Ich habe folgendes Problem. Ich habe eine Fehleremldung bei einem Projekt in Delphi erstellt. Diese meldet sich, wenn man in ein Edit Feld keine Zahl sondern irgendetwas anderes eingibt. Sie meldet sich aber auch wenn ich eine Minus Zahl eingebe. Minus Zahlen sollen aber erlaubt werden.

2.Wenn ich in das Edit Feld nichts eingebe, soll auch eine Fehlermeldung kommen.

Hier der Quellcode der Fehlermeldung:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
procedure TFRM_Options.EDT_xminKeyUp(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
if EDT_xmin.text <>'' then
if (ord(EDT_xmin.text[1])>57) or (ord(EDT_xmin.text[1])<48)
    then
      begin
        showmessage('Falsche Eingabe, bitte nochmal!');
        EDT_xmin.text := '';
        EDT_xmin.setfocus;
      end;

end;


Wäre nett wenn ihr mir hlefen könntet.

Gruß Daniel
DeCodeGuru
ontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic star
Beiträge: 1333
Erhaltene Danke: 1

Arch Linux
Eclipse
BeitragVerfasst: So 15.05.05 10:33 
Schau dir mal folgenden Code an:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
  if not (Key in ['0'..'9', Chr(8), Chr(45)]) then //Chr(8) = Backspace; Chr(45) = Minus
  begin
    Key := Chr(0);
    MessageDlg('Geht nicht!',mtError,[mbOK],0);
  end;
end;


Damit kannst du schonmal verhindern, dass andere Werte als Ziffern, die "Löschtaste" und Minus eingegeben wird. Wenn du nun eine Fehlermeldung haben willst, die gezeigt wird, wenn nichts eingegeben wird, dann kannst du das nicht im OnKeyPress (oder -Up bzw. -Down) machen, da diese Events nur ausgelöst werden, wenn eine Taste gedrückt wird und somit eine Eingabe ins Editfeld stattfindet. Die Überprüfung, ob das Editfeld leer ist, musst du an der Stelle machen, an der du anfängst den Inhalt des Edits zu verarbeiten.

_________________
Viele Grüße
Jakob
Daniel07A Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 36

XP
D7 Enterprise
BeitragVerfasst: So 15.05.05 10:52 
klappt soweit... Danke !
Daniel07A Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 36

XP
D7 Enterprise
BeitragVerfasst: So 15.05.05 11:05 
sry für das doppelposting.

aber wenn ich jetzt in das Edit Feld 2mal ein komma oder minus eintippe, und das programm rechnet mit den zahlen ist der code von oben nicht so gut, weil man kann mit 2 kommatas oder 2 minus zeichen nicht rechnen. hoffe ihr wisst was ich meine.
Alstar
ontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic star
Beiträge: 827



BeitragVerfasst: So 15.05.05 22:35 
Mach es einfach wie folgt: Versuche den String in eine Zahl umzuwandeln. Wenn das klappt ist die Zahl richtig, ansonsten nicht:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
try  
  TempZahl := FloatToStr(EDT_xmin.text);
except
  MessageDlg('Fehler!', mtError, [mbOk] ,0);
end;


Alstar
Tobias1
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 454

Win 98, XP Prof
D3 Prof, D2005 Prof
BeitragVerfasst: So 15.05.05 22:41 
Muss das nicht StrtoFloat heißen?

_________________
Auch das schlechteste Buch hat seine gute Seite: die letzte. (John Osborne)
Alstar
ontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic star
Beiträge: 827



BeitragVerfasst: So 15.05.05 22:42 
Sry. Stimmt, ja ...
Tobias1
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 454

Win 98, XP Prof
D3 Prof, D2005 Prof
BeitragVerfasst: So 15.05.05 22:44 
Noch geschickter fände ich anstatt einer Fehlermeldung den Befehl rückgängig, damit wäre auch das mit den zwei Kommas abgedeckt.

_________________
Auch das schlechteste Buch hat seine gute Seite: die letzte. (John Osborne)
Daniel07A Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 36

XP
D7 Enterprise
BeitragVerfasst: Mo 16.05.05 10:56 
Dein Code funktioniert bei mir nicht. Es liegt an diesem
ausblenden Delphi-Quelltext
1:
TempZahl := ... ;					

Ich weiß aber nicht warum.

Ansonsten finde ich den Code von DeCodeGuru schon recht gut. Er soll halt nur akzeptieren, dass mann nicht 2mal das Komma oder minus eigeben kann.

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);   
begin   
  if not (Key in ['0'..'9', Chr(8), Chr(45)]) then //Chr(8) = Backspace; Chr(45) = Minus   
  begin   
    Key := Chr(0);   
    MessageDlg('Geht nicht!',mtError,[mbOK],0);   
  end;   
end;


Moderiert von user profile iconraziel: Code- durch Delphi-Tags ersetzt.