Autor Beitrag
Terra23
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 872

Win 8
Delphi 7
BeitragVerfasst: Di 25.03.03 12:54 
Hi Leute.

Ich weiß, klingt sicher dumm, aber kann mir jemand mal einen Code geben, wie ich kurz und wirksam verhindern kann, daß in einem Edit-Feld wirklich NUR Nummern eingegeben werden können? :oops:

Ich hatte das mal in der Schule, aber da bin ich ja schon lang raus. :wink:

Danke und Grüße..

Moderiert von user profile iconTino: Titel geändert & Topic verschoben.

_________________
Hasta La Victoria Siempre
Simon Joker
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 236
Erhaltene Danke: 1



BeitragVerfasst: Di 25.03.03 13:05 
tu's so
ausblenden Quelltext
1:
2:
3:
4:
procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
  if not (key in ['0'..'9']) then key:=#0;
end;
Terra23 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 872

Win 8
Delphi 7
BeitragVerfasst: Di 25.03.03 13:08 
Yo Danke.

Und wie ermögliche ich es, den Rückschritt zuzulassen?

THX..

_________________
Hasta La Victoria Siempre
AXMD
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 4006
Erhaltene Danke: 7

Windows 10 64 bit
C# (Visual Studio 2019 Express)
BeitragVerfasst: Di 25.03.03 13:12 
Hi,

ich weiß leider den ASCII-Code der BS-Taste nicht. Ich sag jetzt einfach mal, er wär 29. Dann müsste dein Code lauten:

ausblenden Quelltext
1:
2:
3:
...
if (not(key in ['0'..'9'])) and (not(key = #29)) then key:=#0; 
...


Stehgreif - ich hoffe, das stimmt so.

AXMD
Simon Joker
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 236
Erhaltene Danke: 1



BeitragVerfasst: Di 25.03.03 13:12 
ausblenden Quelltext
1:
2:
3:
  if not (key in ['0'..'9']) and
     not (Key=#8)
  then key:=#0;
Terra23 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 872

Win 8
Delphi 7
BeitragVerfasst: Di 25.03.03 13:16 
Danke nochmal. Ihr habt mir echt sehr geholfen.

Wenn mir jetzt vielleicht noch einer von euch sagen könnte, wo ich eine solche Liste herbekomme (ich glaube ASCII-Liste), in der z.B. steht, daß #8 der Rückschritt ist, wäre ich euch noch dankbarer.

CU, Terra..

_________________
Hasta La Victoria Siempre
AXMD
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 4006
Erhaltene Danke: 7

Windows 10 64 bit
C# (Visual Studio 2019 Express)
BeitragVerfasst: Di 25.03.03 13:19 
Hi,

so eine Liste kannst du dir auch selbst machen:

ausblenden Quelltext
1:
2:
3:
4:
5:
6:
7:
...
var
  i: Byte;
begin
  for i := Low(Byte) to High(Byte) do
    Memo1.Lines.Add(InttoStr(i) + ' : ' + Chr(i));
...


AXMD
Tino
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Veteran
Beiträge: 9839
Erhaltene Danke: 45

Windows 8.1
Delphi XE4
BeitragVerfasst: Di 25.03.03 13:21 
Terra23 hat folgendes geschrieben:
Wenn mir jetzt vielleicht noch einer von euch sagen könnte, wo ich eine solche Liste herbekomme[...]

Wie wäre es mit einer Suchmaschiene. Dann findest Du solche links wie den hier: www.asciitable.com/

Gruß
Tino
Terra23 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 872

Win 8
Delphi 7
BeitragVerfasst: Di 25.03.03 13:21 
Ich probier's mal, vielen Dank.

Bis demnächst..

_________________
Hasta La Victoria Siempre
Simon Joker
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 236
Erhaltene Danke: 1



BeitragVerfasst: Di 25.03.03 13:30 
Aufpassen!

OnKeyPress reagiert nicht auf Tasten wie Del, ESC usw! Die Codes in OnKeyDown/Up sind aber verschieden von den ASCII-Codes.
Bei OnKeyDown ist Key=46 für DEL -> ASCII ist das aber "."!
Luckie
Ehemaliges Mitglied
Erhaltene Danke: 1



BeitragVerfasst: Di 25.03.03 13:44 
Simon Joker hat folgendes geschrieben:
ausblenden Quelltext
1:
2:
3:
  if not (key in ['0'..'9']) and
     not (Key=#8)
  then key:=#0;

warum denn nicht gleich so:
ausblenden Quelltext
1:
2:
3:
4:
5:
procedure TForm1.Edit1KeyPress(Sender: TObject;
  var Key: Char);
begin
  if not (Key in [#8,#46,#48 .. #57]) then Key := #0
end;

#46 ist der Punkt oder das Komma.

Die Umwandlung sollte man meiner Meinung nicht bei der Eiongabe machen (verwirrt nur), sondern im Code entsprechend berücksichtigen mit der Variable DecimalSeparator.