Autor Beitrag
crossit
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 39



BeitragVerfasst: Fr 25.12.09 02:28 
tach alle zusammen,
erstmal frohe weihnachten euch und hier ist gleich mal meine frage
habe mehr als 50 edit felder die summiert werden sollen und überprüft werden ob eine zahl drinn ist. ich habe es mit trytostr ausprobiert und alles hatte auch ganz gut geklappt jedoch habe ich zb 18 editfelder die ausgerechnet werden und das doch eine relativ große schreibarbeit ist vllt kennt jmd eine kürzere variante
hier mein code
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
var
zahl1,zahl2,zahl3,ergebnis : integer;
begin

if TryStrToInt(edit1.Text, zahl1)  and TryStrToInt(edit2.Text, zahl2) and TryStrToInt(edit3.Text, zahl3) then
begin
edit4.Text := IntToStr(zahl1 + zahl2+ zahl3) ;
end
else
 ShowMessage('Bitte nur Zahlen eingeben!');

end;
BenBE
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 8721
Erhaltene Danke: 191

Win95, Win98SE, Win2K, WinXP
D1S, D3S, D4S, D5E, D6E, D7E, D9PE, D10E, D12P, DXEP, L0.9\FPC2.0
BeitragVerfasst: Fr 25.12.09 02:54 
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
var 
    X: Integer;
    Summe: Integer;
    E: TEdit;
begin
    For X := 1 to 50 do
    Begin
        E := TEdit(FindComponent('Edit' + IntToStr(X)));
        If E <> nil then
        begin
            Summe := Summe + IntToStrDef(E.Text, 0);
        end;
    end;

    EditErgebnis.Text := IntToStr(Summe);
end;

_________________
Anyone who is capable of being elected president should on no account be allowed to do the job.
Ich code EdgeMonkey - In dubio pro Setting.
crossit Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 39



BeitragVerfasst: Fr 25.12.09 12:55 
danke für deine antwort! mir ist ein fehler aufgefallen unzwar mus es strtointdef heißen
elundril
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 3747
Erhaltene Danke: 123

Windows Vista, Ubuntu
Delphi 7 PE "Codename: Aurora", Eclipse Ganymede
BeitragVerfasst: Fr 25.12.09 13:26 
wie wäre es im KeyPress bzw KeyDown gleich mal nur zahlen eingeben zu lassen? also das der benutzer gar ned auf die idee kommen kann buchstaben einzugeben. ;)

lg elundril

_________________
This Signature-Space is intentionally left blank.
Bei Beschwerden, bitte den Beschwerdebutton (gekennzeichnet mit PN) verwenden.
crossit Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 39



BeitragVerfasst: Fr 25.12.09 14:04 
wäre auch eine variante aber so ists auch in ordnung :D
habe noch eine frage unzwar wie kann ich die beiden zusammen addieren ?
ausblenden volle Höhe Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
procedure tform1.r4;
  var
    X: Integer;
    Summe: Integer;
    E: TEdit;
    ergebnis2 : integer;
begin
    For X := 10 to 11 do
    Begin
        E := TEdit(FindComponent('l' + IntToStr(X)));
        If E <> nil then
        begin
            Summe := Summe + strtointdef(E.Text, 0);
        end;
    end;

    ergebnis2 := Summe;
end;

procedure tform1.r5;
  var
    X: Integer;
    Summe: Integer;
    E: TEdit;
    ergebnis3 : integer;
begin
    For X := 12 to 16 do
    Begin
        E := TEdit(FindComponent('l' + IntToStr(X)));
        If E <> nil then
        begin
            Summe := Summe + strtointdef(E.Text, 0);
        end;
    end;

    ergebnis3 := Summe;
end;
Hugo343
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 114
Erhaltene Danke: 2

Windows 7
Turbo Delphi, Dev C++
BeitragVerfasst: Fr 25.12.09 14:35 
Wär das nicht besser wenn man für so viele Felder ein TStringgrid zu nehmen...?
crossit Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 39



BeitragVerfasst: Fr 25.12.09 15:53 
top danke habe es hinbekommen!
Metschu
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 135

Windows XP SP2 Home
Delphi 7; Delphi XE2-Starter
BeitragVerfasst: Fr 25.12.09 20:48 
Eine andere Variante wäre noch, das man nur Zahlen in das entsprechende Feld eintragen kann:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
// Freigabe nur von Positiven ganzen Zahlen
procedure Teingabe.NurZahl(Sender: TObject; var Key: Char);
begin
 if not (Key in [#8'0'..'9']) // #8 ist die "Löschtaste"
  then key := #0;
end;


Wenn du Komma-Zahlen brauchst:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
procedure Teingabe.Komma_Zahl(Sender: TObject; var Key: Char);
var i:integer;
begin
 if Key = '.' then //wandelt Punkt in Komma um
    Key := ',';
 for i:=1 to length((Sender as TlabeledEdit).Text) do // Verhindert das mehrfache eingeben von Kommas
  if     (copy((Sender as TlabeledEdit).Text,i,1)=',')
     and (key =','
   then key:=#0;
  if NOT (Key in [#8'0'..'9'','])
   then Key := #0;
end;


Gruß

Torsten
elundril
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 3747
Erhaltene Danke: 123

Windows Vista, Ubuntu
Delphi 7 PE "Codename: Aurora", Eclipse Ganymede
BeitragVerfasst: Sa 26.12.09 00:39 
user profile iconMetschu hat folgendes geschrieben Zum zitierten Posting springen:

Wenn du Komma-Zahlen brauchst:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
procedure Teingabe.Komma_Zahl(Sender: TObject; var Key: Char);
var i:integer;
begin
 if Key = '.' then //wandelt Punkt in Komma um
    Key := ',';
 for i:=1 to length((Sender as TlabeledEdit).Text) do // Verhindert das mehrfache eingeben von Kommas
  if     (copy((Sender as TlabeledEdit).Text,i,1)=',')
     and (key =','
   then key:=#0;
  if NOT (Key in [#8'0'..'9'','])
   then Key := #0;
end;


Gruß

Torsten


wozu so umständlich? ;)

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
procedure Teingabe.Komma_Zahl(Sender: TObject; var Key: Char);
var i:integer;
begin
 if Key = '.' then //wandelt Punkt in Komma um
    Key := ',';
 if pos(',', (Sender as TlabeledEdit).Text) > 0 then
   Key := #0;
  if NOT (Key in [#8'0'..'9'','])
   then Key := #0;
end;

_________________
This Signature-Space is intentionally left blank.
Bei Beschwerden, bitte den Beschwerdebutton (gekennzeichnet mit PN) verwenden.
Metschu
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 135

Windows XP SP2 Home
Delphi 7; Delphi XE2-Starter
BeitragVerfasst: Sa 26.12.09 13:25 
user profile iconelundril hat folgendes geschrieben Zum zitierten Posting springen:

wozu so umständlich? ;)


Form follows Function... :mrgreen:

Danke für den Tip, werde es übernehmen
Bergmann89
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 1742
Erhaltene Danke: 72

Win7 x64, Ubuntu 11.10
Delphi 7 Personal, Lazarus/FPC 2.2.4, C, C++, C# (Visual Studio 2010), PHP, Java (Netbeans, Eclipse)
BeitragVerfasst: Sa 26.12.09 15:55 
user profile iconelundril hat folgendes geschrieben Zum zitierten Posting springen:
wozu so umständlich? ;)

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
procedure Teingabe.Komma_Zahl(Sender: TObject; var Key: Char);
var i:integer;
begin
 if Key = '.' then //wandelt Punkt in Komma um
    Key := ',';
 if pos(',', (Sender as TlabeledEdit).Text) > 0 then
   Key := #0;
  if NOT (Key in [#8'0'..'9'','])
   then Key := #0;
end;


das musst du aber trotzdem noch prüfen, ob die Komma oder Punkt gedrückt wurden. So kannst du keine Zahl mehr eingeben, wenn ein Komma drin steht ;)

MfG Bergmann.

_________________
Ich weiß nicht viel, lern aber dafür umso schneller^^
elundril
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 3747
Erhaltene Danke: 123

Windows Vista, Ubuntu
Delphi 7 PE "Codename: Aurora", Eclipse Ganymede
BeitragVerfasst: Sa 26.12.09 16:22 
stimmt, denkfehler, sorry. ich denke aber um diese uhrzeit kann man mir das verzeihen, oder? :)

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
procedure Teingabe.Komma_Zahl(Sender: TObject; var Key: Char);
begin
 if Key = '.' then //wandelt Punkt in Komma um
    Key := ',';
 if (Key = ','AND (pos(',', (Sender as TlabeledEdit).Text) > 0then
   Key := #0;
 if NOT (Key in [#8'0'..'9'','])
   then Key := #0;
end;


so sollte es jetzt passen.

lg elundril

_________________
This Signature-Space is intentionally left blank.
Bei Beschwerden, bitte den Beschwerdebutton (gekennzeichnet mit PN) verwenden.