Autor Beitrag
Darth Sitthiander
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 95



BeitragVerfasst: Mi 01.03.06 15:55 
Hallo Leute
Ich weiss nicht ob ich im richtigen bereich bin für diese Frage, aber trotzdem kann man ja verschieben oder? Ich bin neu hier und n Neewbie was programmieren angeht. Ich habe mal was versucht, nämlich bytes und bits und soweiter zu berechnen. Alles funktiuoniert reibungslos. Wenn man in eine edit die anzahl bytes eingibt und wählt in welche form(also bytes megabytes gigabytes ...) man sie haben will und dann auf umwandelns klickt kommt unten in ner label das ergebnis angezeigt. Jetzt möchte ich aber noch haben dass nicht nur die zahlen in der label angezeigt werden, sonder soll auch noch ' Bity' ' Bytes' usw stehen. Dann kommt da immer so ein fehler. Ich kenne mich überhaupt nicht mit strings integern und so weiter aus. Vieleicht könnt ihr euch mal den quellcode ansehen? Danke im Vorraus

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:
unit byterechner;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Edit1: TEdit;
    Label1: TLabel;
    ComboBox1: TComboBox;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Button2: TButton;
    Label5: TLabel;
    procedure Button1Click(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;
  zahl1, zahl2, ergebnis, a:real;
  wert:string;
  size:integer;

implementation

{$R *.dfm}

function FormatFileSizeg(Size: INT64): string;
var
  Byte, Ausgabe: string;
begin
  result       := IntToStr(Size);
    Ausgabe := CurrToStrF((Size / (1024 * 1024 * 1024)), ffNumber, 2);
    Byte := ' GigaBytes';

      Ausgabe := IntToStr(Size);

  result := Ausgabe + Byte;
end;

function FormatFileSizem(Size: INT64): string;
var
  Byte, Ausgabe: string;
begin
  result       := IntToStr(Size);
    Ausgabe := CurrToStrF((Size / (1024 * 1024 * 1024)), ffNumber, 2);
    Byte := ' MegaBytes';

      Ausgabe := IntToStr(Size);

  result := Ausgabe + Byte;
end;

function FormatFileSizek(Size: INT64): string;
var
  Byte, Ausgabe: string;
begin
  result       := IntToStr(Size);
    Ausgabe := CurrToStrF((Size / (1024 * 1024 * 1024)), ffNumber, 2);
    Byte := ' KiloBytes';

      Ausgabe := IntToStr(Size);

  result := Ausgabe + Byte;
end;

function FormatFileSizeb(Size: INT64): string;
var
  Byte, Ausgabe: string;
begin
  result       := IntToStr(Size);
    Ausgabe := CurrToStrF((Size / (1024 * 1024 * 1024)), ffNumber, 2);
    Byte := ' Bytes';

      Ausgabe := IntToStr(Size);

  result := Ausgabe + Byte;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
wert:= edit1.text;
zahl1:=strtofloat(edit1.text);

if Edit1.text <> '' then
begin
if Combobox1.Text = 'Bits' then
begin
a:= 8;
end;
if Combobox1.Text = 'Bytes' then
begin
a:= 1;
size:=strtoint('Bytes');
end;
if Combobox1.Text = 'Kilobytes' then
begin
a:= strtoint(inttostr(1024));
size:=strtoint('KiloBytes');
end;
if Combobox1.Text = 'Megabytes' then
begin
a:= strtoint(inttostr(1024*1024));
size:=strtoint('MegaBytes');
end;
if Combobox1.Text = 'Gigabytes' then
begin
a:= strtoint(inttostr(1024*1024*1024));
size:=strtoint('GigaBytes');
end;
zahl2:=a+size;
ergebnis := zahl1 / zahl2;
label1.caption:=floattostr(ergebnis);
end;

end;
end.


Moderiert von user profile iconGausi: Code- durch Delphi-Tags ersetzt
Gausi
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 8548
Erhaltene Danke: 477

Windows 7, Windows 10
D7 PE, Delphi XE3 Prof, Delphi 10.3 CE
BeitragVerfasst: Mi 01.03.06 16:03 
Hallo und :welcome: in der Entwickler-Ecke!

Zuerstmal wäre es schön, wenn du auch sagen könntest, was für ein Fehler kommt. Das erleichtert nämlich die Suche danach etwas ;-)

Aber das hier: size := strtoint('Bytes'); ist Quatsch. Size ist ein Integer, also eine Zahl. 'Bytes' ist ein String, also eine Zeichenkette (ein "Wort"). Der Befehl StrToInt versucht, die Zeichenkette, die sie übergeben bekommt (hier "Bytes") als Zahl zu interpretieren. Z.B. kommt bei strtoint('42') die Zahl 42 bei raus. "Bytes" ist aber keine Darstellung für eine Zahl, daher kommt eine Fehlermeldung.

Edit: irgendwie macht da gar nichts wirklich einen Sinn...:gruebel: Pack mal bitte an einigen Stellen ein paar Kommentare rein, was du dir dabei gedacht hast, und was passieren sollte...

_________________
We are, we were and will not be.
jasocul
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 6393
Erhaltene Danke: 147

Windows 7 + Windows 10
Sydney Prof + CE
BeitragVerfasst: Mi 01.03.06 16:14 
Du solltest besser eine ComboBox(bit, Byte, kByte, MByte, GByte) für den Typ der eingegebenen Zahl verwenden. Das hat den Vorteil, dass du dich nicht um die schreibweise kümmern musst und es auch keine Tippfehler geben kann. Dein Edit-Feld solltest du so aufbereiten, dass nur Zahlen eingetippt werden können. eVtl. bietet sich sogar ein TMaskEdit an.
Welche Exception kommt, wenn Buchstaben mit StrToInt umgewandelt werden, ist doch klar. :wink:
Darth Sitthiander Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 95



BeitragVerfasst: Mi 01.03.06 16:25 
Vielen Dank für die schnelle Antwort, allerdings funkrioniert es immernoch nicht. Da kommt Inkommertable Typen 'integer' und 'String'. Noch ne frage, wie mache ich das man nur zahlen eingeben kann, in eine edit? Und wie ist das mit der combobox gemeint? ich hab doch ne combobox drinne.
Darth Sitthiander Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 95



BeitragVerfasst: Mi 01.03.06 16:38 
Entschuldigung für doppelpost. Ich hab das problem gefunden. Aber trotzdem:
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:
unit byterechner;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Edit1: TEdit;
    Label1: TLabel;
    ComboBox1: TComboBox;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Button2: TButton;
    Label5: TLabel;
    procedure Button1Click(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;
  zahl1, zahl2, ergebnis, a:real;  //Bauch ich wohl nicht viel zu sagen^^
  wert:string//hier auch nicht
  size:integer; //Weiss auch nicht^^

implementation

{$R *.dfm}

function FormatFileSizeg(Size: INT64): string;
var
  Byte, Ausgabe: string;
begin
  result       := IntToStr(Size);
    Ausgabe := CurrToStrF((Size / (1024 * 1024 * 1024)), ffNumber, 2);
    Byte := ' GigaBytes';

      Ausgabe := IntToStr(Size);

  result := Ausgabe + Byte;   //Diesen Teil hab ich auf www.ensacom.de gefunden, stimmt also(is nur ein biscchen abgeändert für mein "projekt"
end;

function FormatFileSizem(Size: INT64): string;
var
  Byte, Ausgabe: string;
begin
  result       := IntToStr(Size);
    Ausgabe := CurrToStrF((Size / (1024 * 1024 * 1024)), ffNumber, 2);
    Byte := ' MegaBytes';

      Ausgabe := IntToStr(Size);

  result := Ausgabe + Byte;
end;

function FormatFileSizek(Size: INT64): string;
var
  Byte, Ausgabe: string;
begin
  result       := IntToStr(Size);
    Ausgabe := CurrToStrF((Size / (1024 * 1024 * 1024)), ffNumber, 2);
    Byte := ' KiloBytes';

      Ausgabe := IntToStr(Size);

  result := Ausgabe + Byte;
end;

function FormatFileSizeb(Size: INT64): string;
var
  Byte, Ausgabe: string;
begin
  result       := IntToStr(Size);
    Ausgabe := CurrToStrF((Size / (1024 * 1024 * 1024)), ffNumber, 2);
    Byte := ' Bytes';

      Ausgabe := IntToStr(Size);

  result := Ausgabe + Byte;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
wert:= edit1.text;
zahl1:=strtofloat(edit1.text);

if Edit1.text <> '' then  //also hier fäng mein schlammassel an. Solange ich nur
mit den zahlen ohne angaben gearbeitet habe, hat das funkioniert.
begin
if Combobox1.Text = 'Bits' then //Wenn in der Combobox1 Bits drinnesteht sollte
a(also die zahl1(also die zahl mit der der wert der edit1 dividiert werden soll))
begin
a:= 8// a wird eine 8 zugewiesen(also wird edit1 durch 8 geteilt und man erhält die anzahl bits)
also 8 bits sind ja ein byte => 1 Byte = 8 bits^^
end;
if Combobox1.Text = 'Bytes' then
begin
a:= 1;  //das gleiche prinzip, hier wird edit1 durch 1 geteilt und man erhält die anzahl bits
man könnte genauso schreiben label1.caption := edit1.text^^
size:=('Bytes'); // hier will ich dass label1.caption = der geteilte wert der edit1
+' bytes stehen soll. z. b. edit1 steht 8 bytes dann will man die bytes wissen, dann
sollte bei der label1 stehen ('
8 Bytes'), aber das tut es nicht.
end;
if Combobox1.Text = '
Kilobytes' then
begin
a:= strtoint(inttostr(1024));
size:=('
KiloBytes');
end;
if Combobox1.Text = '
Megabytes' then
begin
a:= strtoint(inttostr(1024*1024));
size:=('
MegaBytes');
end;
if Combobox1.Text = '
Gigabytes' then
begin
a:= strtoint(inttostr(1024*1024*1024));
size:=('
GigaBytes');
end;
zahl2:=a+size; // a = der wert der divison size = z. b. '
 Bytes'
ergebnis := zahl1 / zahl2; // die edit1 soll mit der zahl2(a + der wert(z.b. '
Bytes')
dividiert werden
label1.caption:=floattostr(ergebnis); // auswertung label1 soll edit1 durch a + size (ahaaaaaa)^^ ich habs. vielleicht gehts jetzt
ich darf ja nicht durch ein wort dividieren^^
end;

end;
end.


Ich habe versucht dass in der label1 stehen soll:
zahl 1(edit1.text) durch zahl 2(a(wert der division)+size(ein Wort^^)). Man kann ja nicht durch ein wort dividieren.

Moderiert von user profile iconGausi: Code- durch Delphi-Tags ersetzt
Tastaro
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 414
Erhaltene Danke: 23



BeitragVerfasst: Mi 01.03.06 16:53 
Huhu!

So sollte das funktionieren:

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:
procedure TForm1.Button1Click(Sender: TObject);
var
   rValue: real;
   strValue: string;
   boCanConvert: boolean;
begin
   //setzt voraus, dass in der Combo-Box folgend Einträge stehen
   //Bits
   //Bytes
   //Kilobytes
   //Megabytes
   //Gigabytes
   strValue := Edit.Text;
   //wegen unterschiedlichen Dezimalseparatoren
   strValue := stringreplace(strValue, '.', DecimalSeparator, [rfReplaceAll]);
   strValue := stringreplace(strValue, ',', DecimalSeparator, [rfReplaceAll]);
   try
      rValue := strtofloat(strValue);
      boCanConvert := TRUE;
   except
      rValue := 0;
      boCanConvert := FALSE;
   end;
   if boCanConvert then
   begin
      case ComboBox1.ItemIndex of
         0: rValue := rValue * 8;
         1: ;
         2: rValue := rValue / 1024;
         3: rValue := rValue / (1024 * 1024);
         4: rValue := rValue / (1024 * 1024 * 1024);
         else
            rValue := -1;
      end;
      Label1.Caption := floattostr(rValue) + ComboBox1.Items[ComboBox1.ItemIndex];
   end
   else
      showmessage('Ungültige Eingabe!');
end;


Habs aber nicht getestet.

Beste Grüße
Tastaro
Darth Sitthiander Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 95



BeitragVerfasst: Mi 01.03.06 17:05 
Heeeey fettes THX. Nur hat die sache einen hacken. Wenn ich die Bits berechnen will, gehn die zahlen ins minus^^ ich würde gerne dezimalzahlen haben^^ thx schonmal.
Tastaro
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 414
Erhaltene Danke: 23



BeitragVerfasst: Mi 01.03.06 18:10 
Die Zahlen gehn ins Minus? Das kann aber nur passieren, wenn du eine negative Anzahl von Bytes eingibst. Und wo gibts denn negative Bytes?

Beste Grüße
Tastaro
Darth Sitthiander Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 95



BeitragVerfasst: Mi 01.03.06 18:17 
Ich weiss jetzt was das problem ist. Ich habe in der combobox den text Bits eingegeben(also standard) damit man nicht immer selber wählen muss(macht einen besseren eindruck). und da muss man jetzt natürlich erst ein Item auswählen damit ers rechnen kann, weil du mir ja so gemcht hast dass er erst dann rechnen kann wenn man ein item wählt. Wär es möglich es so zu machen dass man nur des programm starten muss und direkt loslegen kann?
Tastaro
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 414
Erhaltene Danke: 23



BeitragVerfasst: Mi 01.03.06 18:20 
Setze die Eigenschaft "Style" der Combobox auf csDropDownList und die Eigenschaft "ItemIndex" auf 0.

Beste Grüße
Tastaro
Darth Sitthiander Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 95



BeitragVerfasst: Mi 01.03.06 18:23 
Danke! :D
PS: Es stimmt wirklich, ihr habt hier ne nette Community :D
Nicht so wie in manchen anderen Foren, wo man kaum bedient wird^^(ich darf keine Namen nennen^^)