Autor Beitrag
GiuStyler
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 65
Erhaltene Danke: 1

Win 8.1
Delphi XE6
BeitragVerfasst: Fr 26.09.14 11:55 
Hallo Leute, nachdem ich das Alte Probleme behoben haben, habe ich nun ein neues. Wie muss ich es nun machen, dass mein (,) nicht als Zeichen gezählt wird.

Zur Erläuterung. Mein Taschenrechner darf maximal nur 10 Zahlen, aber wenn man ein (,) macht, dann kann man nur noch 9 Zahlen. Ich möchte es aber nun so haben das man 10 Zahlen machen kann und falls ein (,) vorhanden ist auch.

Einfache Lösung wäre, wenn ich sage man kann 11 Zeichen machen, aber trotzdem angebe das man 10 kann, so könnte es gut gehen sofern keiner auf die Idee käme es mal zu testen.

mein Aktueller Code sieht nun so aus

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:
procedure TFormTaschenrechner.EditZahlKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = '.' then // Wenn "Punkt" (.) gedrückt wird, dann
      Key := ','// ein "Komma" (,) machen!

  if not (Key in [#8'0'..'9'',']) then // wenn nicht die Taste gedruckt wurde, dann
    Key := #0// nichts machen!

    if ((Sender as TEdit).Text = ''and ((Key = '0'or (Key = ',')) then // wenn Text "leer" () ist und wenn "Null" (0) oder "Komma" (,) gedrückt wird, dann
    begin
      (Sender as TEdit).Text := '0,'// "NullKomma" (0,) eingeben!

      (Sender as TEdit).SelStart := Length((Sender as TEdit).Text); // Cursor hinten dran hängen, wenn "NullKomma" (0,) ist

      Key := #0// verhindert beim drück von "Komma" (,) das, dass "Komma" (,) zwei mal kommt

      if Key = '0' then // Wenn "Null" (0) gedrückt wird, dann
      begin
        if (pos('0',(Sender as TEdit).Text) > 0then // prüfen ob "Null" (0) schon vorhanden ist, wenn ja, dann
            Key := #0// Taste nicht zulassen
      end;
    end
    else
    begin
      if Key = ',' then // Wenn "Komma" (,) gedrückt wird, dann
        if (pos(',', (Sender as TEdit).Text) > 0then // prüfen ob "Komma" (,) schon vorhanden ist, wenn ja, dann
            Key := #0// Taste nicht zulassen
    end;

    // BAUSTELLE (Komma nicht als Zeichen zählen)

end;


Wäre Dankbar für jede Hilfe.

MfG
jasocul
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 6388
Erhaltene Danke: 146

Windows 7 + Windows 10
Sydney Prof + CE
BeitragVerfasst: Fr 26.09.14 12:47 
Wie üblich kein Delphi zum Testen dabei, aber so sollte es funktionieren:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
if Key = ',' then
  if (length((Sender as TEdit).Text) > 10then
    Key := #0;
else
  if (length((Sender as TEdit).Text) > 9then
    Key := #0;
GiuStyler Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 65
Erhaltene Danke: 1

Win 8.1
Delphi XE6
BeitragVerfasst: Fr 26.09.14 13:12 
Danke jasocul,

funktioniert noch nicht ganz, aber damit kann ich weiterarbeiten.

Aktuell ist es so, dass er mir das (,) trotzdem noch macht, aber trotzdem nur 9 zahlen dann zulässt, außer es sind schon 9 bzw. 10 zahlen da.

Was ich genau brauche ist, dass er das (,) nicht mitzählt.

Aktuell ist es so

1,23456789 (9 Zahlen inklusive (,))

ich brauche es so

1,234567890 (10 Zahlen mit (,))

aber wenn kein (,) vorhanden ist, dass er dann trotzdem 0123456789 macht und danach das (,) nicht mehr zulässt

MfG
WasWeißDennIch
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 653
Erhaltene Danke: 160



BeitragVerfasst: Fr 26.09.14 13:25 
Schreib Dir doch eine kleine Hilfsfunktion, die prüft, ob ein Komma zugelassen werden soll. Das ist ja dann der Fall, wenn noch keins enthalten und die Stringlänge kleiner als 10 ist.
GiuStyler Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 65
Erhaltene Danke: 1

Win 8.1
Delphi XE6
BeitragVerfasst: Fr 26.09.14 14:28 
Bin dabei WasWeißDennIch :-)

sieht aktuell so aus

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
if (Pos(',', (Sender as TEdit).Text) > 0and (length((Sender as TEdit).Text) > 11then
    begin
      Key := #0;
    end
    else
    if (Pos(',', (Sender as TEdit).Text) = 0and (length((Sender as TEdit).Text) > 10then
      Key := #0;


Aktuell ist es aber so, dass er mir immer 11 macht mit und ohne (,) da ist nun noch der Wurm drin, aber es läuft schon mal wie ich es haben will. Ich komme der Sache näher :-)

MfG
WasWeißDennIch
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 653
Erhaltene Danke: 160



BeitragVerfasst: Fr 26.09.14 15:09 
Und wo ist nun die angesprochene Funktion? OK, mal aus der hohlen Hand getippt:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
function CommaPossible(const s: string): Boolean;
begin
  Result := (Pos(',', s) = 0and (Length(s) < 10);
end;

Benutzen kannst Du das dann z.B. so:
ausblenden Delphi-Quelltext
1:
2:
3:
if Key = ',' then
  if not CommaPossible((Sender as TEdit).Text) then
    Key := #0;
GiuStyler Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 65
Erhaltene Danke: 1

Win 8.1
Delphi XE6
BeitragVerfasst: Mo 29.09.14 09:00 
So. Das Wochenende ist vorbei :-( weiter ans Werk. Sry das ich das nicht mehr am Freitag gesehen habe, aber habe mit einem Kollegen an einem Rechner gearbeitet.

Also, mein Code sieht nun komplett so aus und es Funktioniert auch.

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 TFormTaschenrechner.EditZahlKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = '.' then // Wenn "Punkt" (.) gedrückt wird, dann
      Key := ','// ein "Komma" (,) machen!

  if not (Key in [#8'0'..'9'',']) then // wenn nicht die Taste gedruckt wurde, dann
    Key := #0// nichts machen!

    if ((Sender as TEdit).Text = ''and ((Key = '0'or (Key = ',')) then // wenn Text "leer" () ist und wenn "Null" (0) oder "Komma" (,) gedrückt wird, dann
    begin
      (Sender as TEdit).Text := '0,'// "NullKomma" (0,) eingeben!

      (Sender as TEdit).SelStart := Length((Sender as TEdit).Text); // Cursor hinten dran hängen, wenn "NullKomma" (0,) ist

      Key := #0// verhindert beim drück von "Komma" (,) das, dass "Komma" (,) zwei mal kommt

      if Key = '0' then // Wenn "Null" (0) gedrückt wird, dann
      begin
        if (pos('0',(Sender as TEdit).Text) > 0then // prüfen ob "Null" (0) schon vorhanden ist, wenn ja, dann
            Key := #0// Taste nicht zulassen
      end;
    end
    else
    begin
      if Key = ',' then // Wenn "Komma" (,) gedrückt wird, dann
        if (pos(',', (Sender as TEdit).Text) > 0then // prüfen ob "Komma" (,) schon vorhanden ist, wenn ja, dann
            Key := #0// Taste nicht zulassen
    end;

    if (length((Sender as TEdit).Text) > 10and (pos(',', (Sender as TEdit).Text) > 0then
    begin
      Key := #0;
    end
    else
    if (length((Sender as TEdit).Text) > 9and (pos(',', (Sender as TEdit).Text) = 0then
      Key := #0;
end;


Klappt auch wunderabar. Habe gerade mal dein getestet bei dem Code

ausblenden Delphi-Quelltext
1:
2:
3:
if Key = ',' then
  if not CommaPossible((Sender as TEdit).Text) then
    Key := #0;


kommt in der Zeile if not ... die Fehlermeldung das es Boolean sein muss.

Ich muss nun noch das gleiche mit dem (,) für die Tastatur auch nun für die Buttons machen. Habe das so gemacht, aber funktioniert nicht.

Nur den Code in den {} beachten, der Rest funktioniert ohne probleme.

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
procedure TFormTaschenrechner.Zeichenhinzu(Eingabefeld: TEdit; Eingabe: string);
begin
  if Length(Eingabefeld.Text) <= 9 then
    begin
      if (Eingabe = ','then
        if (pos (',', Eingabefeld.Text) > 0then
          Eingabe := '';
          if ((Eingabefeld.Text = ''and ((Eingabe = '0'or (Eingabe = ','))) then
              Eingabefeld.Text := '0,'
              else
              Eingabefeld.Text := Eingabefeld.Text+Eingabe;
    end;

    {if (pos (',', Eingabefeld.Text) > 0) and (length(Eingabefeld.Text) > 10) then
    begin
      Eingabe := '';
    end
    else
    if (pos (',', Eingabefeld.Text) = 0) and (length(Eingabefeld.Text) > 9) then
      Eingabe := '';}


end;
WasWeißDennIch
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 653
Erhaltene Danke: 160



BeitragVerfasst: Mo 29.09.14 09:03 
CommaPossible ist doch eine Boolean-Funktion, hast Du sie auch in Deinen Code eingebaut?
GiuStyler Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 65
Erhaltene Danke: 1

Win 8.1
Delphi XE6
BeitragVerfasst: Mo 29.09.14 09:12 
Das hier

ausblenden Delphi-Quelltext
1:
2:
3:
4:
function CommaPossible(const s: string): Boolean;
begin
  Result := (Pos(',', s) = 0and (Length(s) < 10);
end;


muss ja unterhalb end; sein oder? und das hier

ausblenden Delphi-Quelltext
1:
2:
3:
if Key = ',' then
  if not CommaPossible((Sender as TEdit).Text) then
    Key := #0;


muss in die procedure oder?
baumina
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 305
Erhaltene Danke: 61

Win 7
Delphi 10.2 Tokyo Enterprise
BeitragVerfasst: Mo 29.09.14 09:29 
Du solltest dich wirklich bei solch grundlegenden Programmierfragen an deinen Ausbilder wenden, oder ein paar Basics im Internet oder Büchern lesen. Der Weg jede einzelne Zeile deines Programmcodes hier zu erfragen hilft dir echt nicht und für uns ist es sehr anstrengend immer bei Adam und Eva anzufangen um dir eine Programmzeile zu erklären.
WasWeißDennIch
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 653
Erhaltene Danke: 160



BeitragVerfasst: Mo 29.09.14 09:58 
Das sehe ich genauso. Für den Einstieg kann ich diese Seite (englisch) empfehlen.
GiuStyler Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 65
Erhaltene Danke: 1

Win 8.1
Delphi XE6
BeitragVerfasst: Mo 29.09.14 10:58 
Danke. Werde ich mir mal anschauen. Mein Sprachenglisch ist nun nicht so besonderns, aber ich verstehe es zum Glück besser als das ich es spreche ^^

PS: Habe es nun auch bei den Buttons geschafft. Der Code sieht nun so aus und macht das gleiche für die Buttons wie das andere für die Tastatur.

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
procedure TFormTaschenrechner.Zeichenhinzu(Eingabefeld: TEdit; Eingabe: string);
begin
  if (Length(Eingabefeld.Text) > 10and (pos (',', Eingabefeld.Text) > 0then
    begin
      Eingabe := '';
    end
    else
    if (Length(Eingabefeld.Text) > 9and (pos (',', Eingabefeld.Text) = 0then
      Eingabe := '';
  
    if (Eingabe = ','then
      if (pos (',', Eingabefeld.Text) > 0then
        Eingabe := '';
        if ((Eingabefeld.Text = ''and ((Eingabe = '0'or (Eingabe = ','))) then
          Eingabefeld.Text := '0,'
          else
          Eingabefeld.Text := Eingabefeld.Text+Eingabe;
end;