Autor Beitrag
MilKeeway
Hält's aus hier
Beiträge: 9



BeitragVerfasst: Sa 31.10.09 17:28 
Hab schon wieder ein Problem :D

Wenn ich nun bei meinem Programm den Radiobutton Quader anklick, damit ich Volumen und Oberfläche ausrechnen kann, dann die Zahlen für a, b und h eingeb, dann auf rechnen klick, wird immer nur mit dem Wert in a gerechnet. Zb:
V=a*b*h
V=5*3*2 dann rechnet er einfach 5*5*5. Warum is das soo?
Hier der Quellcode
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:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179:
180:
181:
182:
183:
184:
185:
186:
187:
188:
189:
190:
191:
192:
193:
194:
195:
196:
197:
198:
199:
200:
201:
202:
203:
204:
205:
206:
207:
208:
209:
210:
211:
212:
213:
214:
215:
216:
217:
218:
219:
220:
221:
222:
223:
224:
225:
226:
227:
228:
229:
230:
231:
232:
233:
234:
235:
236:
237:
238:
239:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, StdCtrls, GIFImg, ImgList, DBCtrls, pngimage;

type
  TForm1 = class(TForm)
    btnRechnen: TButton;
    rbtnWuerfel: TRadioButton;
    rbtnQuader: TRadioButton;
    rbntPyramideAllgemein: TRadioButton;
    lbla: TLabeledEdit;
    lblb: TLabeledEdit;
    lblh: TLabeledEdit;
    lblHa: TLabeledEdit;
    lblV: TLabeledEdit;
    lblO: TLabeledEdit;
    lblHb: TLabeledEdit;
    rbtnPyramideQuader: TRadioButton;
    imgPyramideAllgemein: TImage;
    imgPyramideQuadrat: TImage;
    imgQuader: TImage;
    imgWuerfel: TImage;
    procedure RadioButtonCLick(Sender: TObject);
    procedure btnRechnenClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.btnRechnenClick(Sender: TObject);
var a, b, h, ha, hb, Vol, Ober: double;
begin
begin
  lblV.Visible:=true;
  lblO.Visible:=true;
  if rbtnQuader.Checked then
  begin

  try a:=strtofloat(lbla.Text);
    except
    Application.MessageBox('Nur Zahlen verwenden','Falsche Eingabe',16);
    lbla.SetFocus;
    exit;
  end;
    try b:=strtofloat(lblb.Text);
    except
    Application.MessageBox('Nur Zahlen verwenden','Falsche Eingabe',16);
    lblb.SetFocus;
    exit;
  end;
  try h:=strtofloat(lblh.Text);
    except
    Application.MessageBox('Nur Zahlen verwenden','Falsche Eingabe',16);
    lblh.SetFocus;
    exit;
  end;

  Vol:=a*b*h;
  Ober:=2*(a*b+a*h+b*h);
  lblV.Text:=floattostrF(Vol,fffixed,8,2);
  lblO.Text:=floattostrF(Ober,fffixed,8,2);
  end;
end;
   begin
     if rbtnWuerfel.Checked then
    try
    a:=strtofloat(lbla.Text)
    except
       Application.MessageBox('Nur Zahlen verwenden','Falsche Eingabe',16);
       lbla.SetFocus;
       exit;
     end;

     Vol:=a*a*a;
     Ober:=6*a*a;
     lblV.Text:=floattostrF(Vol,fffixed,8,2);
     lblO.Text:=floattostrF(Ober,fffixed,8,2);
   end;
   begin
     if rbntPyramideAllgemein.Checked then
    begin
    try
   a:=strtofloat(lbla.Text);
    except
    Application.MessageBox('Nur Zahlen verwenden','Falsche Eingabe',16);
    lbla.SetFocus;
    exit;
    end;
    try b:=strtofloat(lblb.Text);
    except
    Application.MessageBox('Nur Zahlen verwenden','Falsche Eingabe',16);
    lblb.SetFocus;
    exit;
    end;
   try
   h:=strtofloat(lblh.Text);
   except
   Application.MessageBox('Nur Zahlen verwenden','Falsche Eingabe',16);
   lblh.SetFocus;
   exit;
   end;
   try
   ha:=strtofloat(lblha.Text);
   except
   Application.MessageBox('Nur Zahlen verwenden','Falsche Eingabe',16);
   lblha.SetFocus;
   exit;
   end;
   try
   hb:=strtofloat(lblhb.Text);
   except
   Application.MessageBox('Nur Zahlen verwenden','Falsche Eingabe',16);
   lblhb.SetFocus;
   exit;
   end;
   Vol:=1/3*a*b*h;
   Ober:=a*b+2*(a*ha/2)+2*(b*hb/2);
   lblV.Text:=floattostrF(Vol,fffixed,8,2);
   lblO.Text:=floattostrF(Ober,fffixed,8,2);
   end;
   end;
   begin
     if rbtnPyramideQuader.Checked then
      begin
    try
   a:=strtofloat(lbla.Text);
    except
    Application.MessageBox('Nur Zahlen verwenden','Falsche Eingabe',16);
    lbla.SetFocus;
    exit;
    end;
   try
   h:=strtofloat(lblh.Text);
   except
   Application.MessageBox('Nur Zahlen verwenden','Falsche Eingabe',16);
   lblh.SetFocus;
   exit;
   end;
   try
   ha:=strtofloat(lblha.Text);
   except
   Application.MessageBox('Nur Zahlen verwenden','Falsche Eingabe',16);
   lblha.SetFocus;
   exit;
   end;
   Vol:=1/3*a*a*h;
   Ober:=a*a+4*(a*ha);
   lblV.Text:=floattostrF(Vol,fffixed,8,2);
   lblO.Text:=floattostrF(Ober,fffixed,8,2);
      end;
      end;
end;




procedure TForm1.FormCreate(Sender: TObject);
begin
  lbla.Visible:=false;
  lblb.Visible:=false;
  lblh.Visible:=false;
  lblha.Visible:=false;
  lblhb.Visible:=false;
  //lblV.Visible:=false;
  //lblO.Visible:=false;
  btnRechnen.Visible:=false;
end;

procedure TForm1.RadioButtonCLick(Sender: TObject);
begin
if rbntPyramideAllgemein.Checked
then
begin
ImgPyramideAllgemein.Visible:=true;
ImgQuader.Visible:=false;
ImgWUerfel.Visible:=false;
ImgPyramideQuadrat.Visible:=false;
lbla.Visible:=true;
btnRechnen.Visible:=true;
lblha.Visible:=true;
lblhb.Visible:=true;
lblb.Visible:=true;
lblh.Visible:=true;
end;

if rbtnQuader.Checked then
begin ImgQuader.Visible:=true;
      ImgWuerfel.Visible:=false;
      ImgPyramideAllgemein.Visible:=false;
      ImgPyramideQuadrat.Visible:=false;
      lbla.Visible:=true;
      btnRechnen.Visible:=true;
      lblha.Visible:=false;
      lblhb.Visible:=false;
      lblh.Visible:=true;
      lblb.Visible:=true;
      end;
if rbtnWuerfel.Checked then
begin ImgWuerfel.Visible:=true;
      ImgQuader.Visible:=false;
      ImgPyramideAllgemein.Visible:=false;
      ImgPyramideQuadrat.Visible:=false;
      lbla.Visible:=true;
      btnRechnen.Visible:=true;
      lblha.Visible:=false;
      lblb.Visible:=false;
      lblh.Visible:=false;
      lblhb.Visible:=false;
      end;
if rbtnPyramideQuader.Checked then
begin
      ImgPyramideQuadrat.Visible:=true;
      ImgPyramideAllgemein.Visible:=false;
      ImgWuerfel.Visible:=false;
      ImgQuader.Visible:=false;
      lbla.Visible:=true;
      btnRechnen.Visible:=true;
      lblb.Visible:=false;
      lblhb.Visible:=false;
      lbla.Visible:=true;
      lblh.Visible:=true;
      lblha.Visible:=true;
end;

end;
end.



Würd mich freuen wenn mir jemand helfen kann. Bin schon seit 1ner Stunde dabei und kriegs net hin >.<

Moderiert von user profile iconNarses: Titel geändert.
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 31.10.09 17:35 
schon mal versucht den then zweig von jeder if-abfrage in ein begin und ein end zu packen (hast beim würfel nämlich vergessen ;))?? und die anderen begins und ends sind so ziemlich unnötig. ;) weiters würd ich dir raten deinen code mal richtig zu formatieren. siehe styleguide

lg elundril

_________________
This Signature-Space is intentionally left blank.
Bei Beschwerden, bitte den Beschwerdebutton (gekennzeichnet mit PN) verwenden.
MilKeeway Threadstarter
Hält's aus hier
Beiträge: 9



BeitragVerfasst: Sa 31.10.09 18:07 
Danke schon mal :)

Aber es funktioniert noch immer nicht. Ich kanns nicht mal starten.
Ich bekomm 2 FEhler, 2mal der gleiche: Erwartet Begin, aber Bezeichner TForm1 enthalten.

Das is bei einer Procedure. Da gehört doch gar kein begin hin soweit ich weiß?

---Moderiert von user profile iconNarses: Beiträge zusammengefasst---

EDIT: sorry...hab schon den Fehler gefunden :D hab ein Begin vergessen.

Danke nochmal...hast mir sehr geholfen.
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 31.10.09 18:23 
das heißt dein code sieht jetzt so aus? (sollte er!)

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:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
procedure TForm1.btnRechnenClick(Sender: TObject);
var a, b, h, ha, hb, Vol, Ober: double;
begin
  lblV.Visible:=true;
  lblO.Visible:=true;
  if rbtnQuader.Checked then begin
    try 
      a:=strtofloat(lbla.Text);
    except
      Application.MessageBox('Nur Zahlen verwenden','Falsche Eingabe',16);
      lbla.SetFocus;
      exit;
    end;
    try 
      b:=strtofloat(lblb.Text);
    except
      Application.MessageBox('Nur Zahlen verwenden','Falsche Eingabe',16);
      lblb.SetFocus;
      exit;
    end;
    try 
      h:=strtofloat(lblh.Text);
    except
      Application.MessageBox('Nur Zahlen verwenden','Falsche Eingabe',16);
      lblh.SetFocus;
      exit;
    end;
    Vol:=a*b*h;
    Ober:=2*(a*b+a*h+b*h);
    lblV.Text:=floattostrF(Vol,fffixed,8,2);
    lblO.Text:=floattostrF(Ober,fffixed,8,2);
  end;
  if rbtnWuerfel.Checked then begin
    try
      a:=strtofloat(lbla.Text)
    except
       Application.MessageBox('Nur Zahlen verwenden','Falsche Eingabe',16);
       lbla.SetFocus;
       exit;
    end;
    Vol:=a*a*a;
    Ober:=6*a*a;
    lblV.Text:=floattostrF(Vol,fffixed,8,2);
    lblO.Text:=floattostrF(Ober,fffixed,8,2);
  end;
  if rbntPyramideAllgemein.Checked then begin
    try
      a:=strtofloat(lbla.Text);
    except
      Application.MessageBox('Nur Zahlen verwenden','Falsche Eingabe',16);
      lbla.SetFocus;
      exit;
    end;
    try 
      b:=strtofloat(lblb.Text);
    except
      Application.MessageBox('Nur Zahlen verwenden','Falsche Eingabe',16);
      lblb.SetFocus;
      exit;
    end;
    try
      h:=strtofloat(lblh.Text);
    except
      Application.MessageBox('Nur Zahlen verwenden','Falsche Eingabe',16);
      lblh.SetFocus;
      exit;
    end;
    try
      ha:=strtofloat(lblha.Text);
    except
      Application.MessageBox('Nur Zahlen verwenden','Falsche Eingabe',16);
      lblha.SetFocus;
      exit;
    end;
    try
      hb:=strtofloat(lblhb.Text);
    except
      Application.MessageBox('Nur Zahlen verwenden','Falsche Eingabe',16);
      lblhb.SetFocus;
      exit;
    end;
    Vol:=1/3*a*b*h;
    Ober:=a*b+2*(a*ha/2)+2*(b*hb/2);
    lblV.Text:=floattostrF(Vol,fffixed,8,2);
    lblO.Text:=floattostrF(Ober,fffixed,8,2);
  end;
  if rbtnPyramideQuader.Checked then begin
    try
      a:=strtofloat(lbla.Text);
    except
      Application.MessageBox('Nur Zahlen verwenden','Falsche Eingabe',16);
      lbla.SetFocus;
      exit;
    end;
    try
      h:=strtofloat(lblh.Text);
    except
      Application.MessageBox('Nur Zahlen verwenden','Falsche Eingabe',16);
      lblh.SetFocus;
      exit;
    end;
    try
      ha:=strtofloat(lblha.Text);
    except
      Application.MessageBox('Nur Zahlen verwenden','Falsche Eingabe',16);
      lblha.SetFocus;
      exit;
    end;
    Vol:=1/3*a*a*h;
    Ober:=a*a+4*(a*ha);
    lblV.Text:=floattostrF(Vol,fffixed,8,2);
    lblO.Text:=floattostrF(Ober,fffixed,8,2);
  end;
end;


weiters würde ich dir raten mal das ganze try und except wegzubringen indem du dem benutzer nicht erlaubst falsche eingaben zu machen ;) dadurch wird das ganze kürzer und übersichtlicher.

außerdem solltest dir mal Christian's Crashkurs zu gemüte führen oder den Programmierkurs von Delphi auf Wikibooks. Dann bekommst du vielleicht auch die überflüssigen Begins und ends weg. ;)

lg elundril

_________________
This Signature-Space is intentionally left blank.
Bei Beschwerden, bitte den Beschwerdebutton (gekennzeichnet mit PN) verwenden.
MilKeeway Threadstarter
Hält's aus hier
Beiträge: 9



BeitragVerfasst: Sa 31.10.09 18:35 
ja so sieht er jetzt aus :)

meinst du das man bei den Labels die Eigenschaft Numbers Only auf true setzt? Das wär nämlich nicht gut weil man dann keine Kommazahlen eingeben kann.

Ich lerns ja eh in der Schule also schön Schritt für Schritt da werden die Übungen des CrashKurs auch dabei sein ;)

aber danke nochmal...wenn ich dich bewerten könnte würdest ne 5/5 bekommen :D
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 31.10.09 18:46 
danke schön. da werd ich ja gleich rot. :oops:

Auf nummbers only setzen nicht, aber mit diesem Code kann man nur mehr Zahlen eingeben(auch komma-zahlen in ein Edit eingeben kann):

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
  if not (key in ['0'..'9', DecimalSeparator, #8#46] ) then
    key := #0;
end;


Einfach nur in die KeyPress-Ereignisse von deinem Edit schreiben ;) wenn du es ganz fein machen willst, schreibst du es in eines und Verlinkst im KeyPress-ereignis der anderen edits zu dem. ;)

lg elundril

_________________
This Signature-Space is intentionally left blank.
Bei Beschwerden, bitte den Beschwerdebutton (gekennzeichnet mit PN) verwenden.
MilKeeway Threadstarter
Hält's aus hier
Beiträge: 9



BeitragVerfasst: Sa 31.10.09 19:01 
Das heißt dann, ich könnte mit der Prozedur die ganzen try und except Sachen weglassen und nur das strtofloat, die formel und floattostrF verwenden?
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 31.10.09 19:05 
in der tat, so ist es. Da es prinzipiell böse ist mit exceptions zu arbeiten sondern man eher schauen soll das der benutzer nur das eingeben kann was man will. ;)

lg elundril

_________________
This Signature-Space is intentionally left blank.
Bei Beschwerden, bitte den Beschwerdebutton (gekennzeichnet mit PN) verwenden.
MilKeeway Threadstarter
Hält's aus hier
Beiträge: 9



BeitragVerfasst: Sa 31.10.09 19:08 
ahh...k danke :)

also wir haben es mit except in der Schule gelernt und jetzt wo ich das programmiert hab is es mir schon ziemlich aufwändig vorgekommen^^

aber es gibt ja leute die mir helfen :D