Einzelnen Beitrag anzeigen

Benutzerbild von Jasocul
Jasocul

Registriert seit: 22. Sep 2004
Ort: Delmenhorst
1.330 Beiträge
 
Delphi 11 Alexandria
 
#7

AW: Größter Wert aus StringGrid

  Alt 9. Feb 2018, 08:16
Ein paar grundlegende Verständnisprobleme, die du hast:
If (Form8.StringGrid1.Cells[1,1] > Form8.StringGrid1.Cells[1,2] + Form8.StringGrid1.Cells[1,3]
+ Form8.StringGrid1.Cells[1,4] + Form8.StringGrid1.Cells[1,5]) then
Form8.Edit2.Text:= Form8.StringGrid1.Cells[1,1];
Beispiel:
Zelle 1 Inhalt 5
Zelle 2 Inhalt 1
Zelle 3 Inhalt 2
Zelle 4 Inhalt 7
Zelle 5 Inhalt 21

Dann vergleichst du:
if 5 > 12721 then Der Inhalt der Zellen sind Strings. das "+" verkettet die Strings. Dein "+" ist kein logisches "and". Würdest du diesen Weg weiter verfolgen, müsstest du es in Delphi so verwenden:
Delphi-Quellcode:
If (Form8.StringGrid1.Cells[1,1] > Form8.StringGrid1.Cells[1,2]) and
   (Form8.StringGrid1.Cells[1,1] > Form8.StringGrid1.Cells[1,3]) and
   (Form8.StringGrid1.Cells[1,1] > Form8.StringGrid1.Cells[1,4]) and
   (Form8.StringGrid1.Cells[1,1] > Form8.StringGrid1.Cells[1,5]) then
ABER:
Dann wäre es immer noch falsch, da es sich um Strings und nicht um Zahlen handelt. Der Vergleich findet daher alphanumerisch statt. D.h.: 5 ist größer als 21. Dein Ziel wäre verfehlt.

Somit musst du deine Zellen-Inhalte auch noch in Zahlen umwandeln:
Delphi-Quellcode:
If (StrToInt(Form8.StringGrid1.Cells[1,1]) > StrToInt(Form8.StringGrid1.Cells[1,2])) and
   (StrToInt(Form8.StringGrid1.Cells[1,1]) > StrToInt(Form8.StringGrid1.Cells[1,3])) and
   (StrToInt(Form8.StringGrid1.Cells[1,1]) > StrToInt(Form8.StringGrid1.Cells[1,4])) and
   (StrToInt(Form8.StringGrid1.Cells[1,1]) > StrToInt(Form8.StringGrid1.Cells[1,5])) then
Allerdings würde das kein Programmierer so lösen. Die Lösung von Gollum mit geschachtelten Schleifen ist da schon der richtige Ansatz. Es war man dieser Stelle nur wichtig, dass du auch verstehst, warum es bei dir nicht funktionieren kann und welche Fallen noch zusätzlich vorhanden sind.
Peter
  Mit Zitat antworten Zitat