Autor Beitrag
Big_Berny
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 26



BeitragVerfasst: Sa 20.11.04 09:48 
Hi Leute,
hab mich heute nach über einem halben Jahr wieder ans Prgorammieren (war damals schon ein Noob) gewagt und gleich stecken geblieben.
Ich will eine Prozedur programmieren, welche alle Editfelder darauf überprüft, dass eine Zahl (float) darin ist.

Sieht so aus:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
procedure FelderTest(KursMet,KursKri,KursDeut,Met,Kri,Deut,Ist,Soll,a,b,c: Single);
begin
  try
  strtofloat(Edit1.text)
except
  Edit1.Text := '0';
end;


Später werde ich dann mit einer For-Schleife arbeiten, um alle EditFelder zu checken.
Aber nun zum Problem: Delphi sagt mir 'Undefinierter Bezeichner : Edit1'. Warum funzt das nicht? Normalerweise geh's ja auch, liegt's daran, dass diesmal nicht TObject Sender ist?

Ich hoffe jemand kann helfen...
Big_Berny

Moderiert von user profile iconChristian S.: Delphi-Tags hinzugefügt.
AXMD
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 4006
Erhaltene Danke: 7

Windows 10 64 bit
C# (Visual Studio 2019 Express)
BeitragVerfasst: Sa 20.11.04 09:51 
Frage: Hast du auf deiner Form überhaupt eine Komponente mit dem Namen Edit1?
//EDIT: Sonst versuch mal Form1.Edit1....

AXMD
Big_Berny Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 26



BeitragVerfasst: Sa 20.11.04 09:55 
Danke für die superschnelle Antwort!
Wie blöd von mir, hatte TForm1.Edit... versucht! :roll:

Jetzt klappts! Thanx!
Big_Berny
Big_Berny Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 26



BeitragVerfasst: Sa 20.11.04 10:41 
Leider gibt's schon ein weiteres Problem.
Ich will jetzt also, dass meine Prozedur überprüft ob Edit1.Text eine Zahl ist und wenn nicht, soll Edit1 auf 0 gesetzt werden.

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
procedure TForm1.FelderTest(KursMet, KursKri, KursDeut, Met, Kri, Deut, Soll: Single);
begin
  try
  strtofloat(Form1.Edit1.text)
except
  Form1.Edit1.Text := '0';
end;
end;


Seltsamerweise stürzt das Programm bei der Stelle 'strtofloat(Form1.Edit1.text)' ab, wenn Edit1 kein Inhalt enthält. Sollte er dann nicht automatisch zu except springen?

Big_Berny

Moderiert von user profile iconChristian S.: Delphi-Tags hinzugefügt.
.Chef
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 1112



BeitragVerfasst: Sa 20.11.04 10:44 
Wenn das Programm im Debugger läuft, kriegst du die Exception vom Debugger trotzdem. Kann man aber abstellen.

_________________
Die Antworten auf die 5 häufigsten Fragen:
1. Copy(), Pos(), Length() --- 2. DoubleBuffered:=True; --- 3. Application.ProcessMessages bzw. TThread --- 4. ShellExecute() --- 5. Keine Vergleiche von Real-Typen mit "="!
Big_Berny Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 26



BeitragVerfasst: Sa 20.11.04 10:59 
Ach so! Muss man ja auch erst noch darauf kommen! :)
Danke!

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
procedure TForm1.FelderTest(KursMet, KursKri, KursDeut, Met, Kri, Deut, Soll: Single);
var i:integer;
begin
for i := 1 to 4 do
 begin
  try
  strtofloat(Form1.Edit[i].text) //Fehler!
  except
  Form1.Edit[i].Text := inttostr(0); //Fehler!
  end;
 end;
end;


Und warum klappt 'Form1.Edit[i].text' nicht? Er sollte jetzt Edit1 bis Edit4 durchprobieren.

Big_Berny

Moderiert von user profile iconChristian S.: Code- durch Delphi-Tags ersetzt.
.Chef
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 1112



BeitragVerfasst: Sa 20.11.04 11:02 
Achso, das seh ich jetzt erst. So geht das überhaupt nicht, weil deine Edits nicht in einem Array stehen. Die musst du anders ansprechen:
ausblenden Delphi-Quelltext
1:
TEdit(FindComponent('Edit'+InttoStr(i))).Text:='0'					

Und in der Zeile mit StrToFloat genauso.

_________________
Die Antworten auf die 5 häufigsten Fragen:
1. Copy(), Pos(), Length() --- 2. DoubleBuffered:=True; --- 3. Application.ProcessMessages bzw. TThread --- 4. ShellExecute() --- 5. Keine Vergleiche von Real-Typen mit "="!
F34r0fTh3D4rk
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 5285
Erhaltene Danke: 29

Win Vista (32), Win 7 (64)
Eclipse, SciTE, Lazarus
BeitragVerfasst: Sa 20.11.04 11:21 
oder verhinder, dass man überhaupt etwas anderes als Zahlen eingeben kann ! :D
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
procedure TForm1.Edit1KeyPress(Sender: TObject;
  var Key: Char);
begin
  if not (key in ['0'..'9',#8]) then
    key:=#0;
end;
.Chef
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 1112



BeitragVerfasst: Sa 20.11.04 11:30 
Da reicht das bisschen Code aber nicht, das du da grad aus einem anderen Beitrag herkopiert hast. :twisted: Es handelt sich schließlich um Realzahlen, und deren unterschiedliche Eingabeformen (Vorzeichen, Komma, Exponent, Vorzeichen vom Exponenten, ...) zu begrenzen, ist schon aufwendiger.

_________________
Die Antworten auf die 5 häufigsten Fragen:
1. Copy(), Pos(), Length() --- 2. DoubleBuffered:=True; --- 3. Application.ProcessMessages bzw. TThread --- 4. ShellExecute() --- 5. Keine Vergleiche von Real-Typen mit "="!
Big_Berny Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 26



BeitragVerfasst: Sa 20.11.04 11:46 
F34r0fTh3D4rk hat folgendes geschrieben:
oder verhinder, dass man überhaupt etwas anderes als Zahlen eingeben kann ! :D
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
procedure TForm1.Edit1KeyPress(Sender: TObject;
  var Key: Char);
begin
  if not (key in ['0'..'9',#8]) then
    key:=#0;
end;


Diesen Code verwende ich auch, ausser das '.' auch erlaubt ist. In die Felder muss man nämlich auch Komma-Zahlen (oder ebsser gesagt Punkt-Zahlen ;)) eingeben können.
Wenn man ein Feld leer hat oder wenn es aus mehreren Punkte besteht (z.B. '...) dann stürzts ab!


Gruss
Big_Berny
F34r0fTh3D4rk
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 5285
Erhaltene Danke: 29

Win Vista (32), Win 7 (64)
Eclipse, SciTE, Lazarus
BeitragVerfasst: Sa 20.11.04 11:49 
stimmt :D

Aber für meine Programme brauche ich meistens eh nur integer, und
deshalb hab ich garnicht dran gedacht !

Dann muss das programm aber auch noch prüfen, dass nur ein "." bzw "," da ist,
und dass da auch was vor und hinter steht !

:wink:
Big_Berny Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 26



BeitragVerfasst: Sa 20.11.04 12:01 
F34r0fTh3D4rk hat folgendes geschrieben:
Dann muss das programm aber auch noch prüfen, dass nur ein "." bzw "," da ist,
und dass da auch was vor und hinter steht !


Das macht er ja jetzt indem er schaut, ob man die Eingabe in eine Zahl umwandeln kann! Die Eingabe '.' ist übrigens gleih '0'...
F34r0fTh3D4rk
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 5285
Erhaltene Danke: 29

Win Vista (32), Win 7 (64)
Eclipse, SciTE, Lazarus
BeitragVerfasst: Sa 20.11.04 12:16 
@chef: aus welchem anderen Beitrag ?