Autor Beitrag
Tim.exe
Hält's aus hier
Beiträge: 12



BeitragVerfasst: Di 29.07.03 11:19 
Hallo, hiermit kann ich ja folgende Zaheln in einem Edit erlauben:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
if not(Key in [#8,'0','1','2','3','4']) then
begin
MessageBox(0,'Nur Zahlen zwischen 0 und 4 zulässig.','Fehler',0);
key:=#0;
Form1.Edit1.Text:='';
end;
end;


Das klappt aber nicht bei mehrstelligen Zahlen, z.B. '127','345'

Wie macht man des am besten???
hansa
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 3079
Erhaltene Danke: 9



BeitragVerfasst: Di 29.07.03 11:26 
Manchmal macht man schon was zuviel des Guten : :lol:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
if not(Key in [#8,'0','1','2','3','4']) then
begin
MessageBox(0,'Nur Zahlen zwischen 0 und 4 zulässig.','Fehler',0);
key:=#0;
// Form1.Edit1.Text:='';
end;
end;

_________________
Gruß
Hansa
patrick
ontopic starontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic star
Beiträge: 1481

WIN2k, WIN XP
D6 Personal, D2005 PE
BeitragVerfasst: Di 29.07.03 11:31 
ich glaube du hast die funktion nicht richtig verstanden!
sobald ein text per tastatur in das edit eingegeben wird wird abgefragt welche taste es ist und ob diese "erlaubt" ist. diese abfrage macht bei dir ja das hier:

ausblenden Delphi-Quelltext
1:
if not(Key in [#8,'0','1','2','3','4']) then					


es ist allso möglich wo viele zahlen zwischen 0 und 4 einzugeben wie man will, da die funktion ja nur die tastatureingabe kontrolliert.

_________________
Patrick
im zweifelsfall immer das richtige tun!!!
gogi2207
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 45



BeitragVerfasst: Di 29.07.03 11:32 
Dann ist Onkeypress wohl nicht so gut, du kannst ja einen Button betätigen nachdem die Zahl eingegeben wurde. Dieser kann dann ja überprüfen ob die Zahl erlaubt ist.
derDoc
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 623

Win Vista Prof
D2007 Prof
BeitragVerfasst: Di 29.07.03 11:39 
Deine Zahlen können auch garnicht funktionieren.

Du lässt nur '0' - '4' zu bei '127' würde '1' gehen, '2' ebenfalls, aber '7' nicht. Bei '345' ist es das selbe Problem, da '3' erlaubt ist, '4' auch, aber '5' nicht. Einfacher wäre:
ausblenden Delphi-Quelltext
1:
if not (Key in [#8, '0'..'9']) then					

char nimmt auch nur 1 Zeichen auf, also musst du längere Zahlen einzeln betrachten '127' = '1' '2' '7'

_________________
MfG derDoc
There are only 10 types of people: those who understand binary and those who don't.
StefanH
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 1144

Win XP
D5 Standard, D7 Pers, D2005 Pers
BeitragVerfasst: Di 29.07.03 13:00 
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
if not(Key in [#8,'0','1','2','3','4']) and (Length(Form1.Edit1.Text)<>1) then
begin
MessageBox(0,'Nur Zahlen zwischen 0 und 4 zulässig.','Fehler',0);
key:=#0;
Form1.Edit1.Text:='';
end;
end;



Überprüfen, wie langs ist!

_________________
"Als es noch keine Computer gab, war das Programmieren noch relativ einfach."(Edsger W. Dijkstra)
"Ich bin nicht von Sinnen, sondern ich rede wahre und vernünftige Worte." (Paulus)
hansa
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 3079
Erhaltene Danke: 9



BeitragVerfasst: Di 29.07.03 18:15 
Tim.exe hat folgendes geschrieben:


ausblenden Delphi-Quelltext
1:
Form1.Edit1.Text:='';					


Das klappt aber nicht bei mehrstelligen Zahlen, z.B. '127','345'



Wenn Du bei jedem Tastendruck alles wieder auf '' setzt, wie soll das überhaupt gehen ? Und das sieht keiner, oder ich nicht :?: 8)

_________________
Gruß
Hansa
grayfox
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 800

win98, winXP
D4 Standard; D6 Personal
BeitragVerfasst: Di 29.07.03 18:31 
hallo tim!

ich weiss ja nicht, was du vorhast, aber wenn nur bestimmte zahlen eingegeben werden dürfen, dann stell dem bediener eine listbox bzw combobox zur verfügung, aus welchen er die zahlen wählen kann.
oder du prüfst im OnExitEvent des EditFeldes, ob die zahl deinen wünschen entspricht, falls nicht, gibst eine fehlermeldung aus, löscht das EditFeld mit Edit1.Text.Clear und setzt den focus wieder aufs eingabefeld.
es kommt halt drauf an, was das ganze werden soll...

mfg, stefan
mimi
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 3458

Ubuntu, Win XP
Lazarus
BeitragVerfasst: Di 29.07.03 19:14 
Du wilst jetzt nur das zaheln im edit fehlt erlaubt sind, richtig ?
das geht so am besten:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
onKeyDown:
  if Key in [chr(0)..chr(9)] then begin
    Key:=#0 // damit sind diese tasten nicht mehrerlaubt und ereichen auch garnicht die edit1.text variable(meine ich... sowa das doch oder ?:)

  end;

wobei ich mir bei chr nicht sicher bin es kann auch ord heißen muss du mal schauen......

_________________
MFG
Michael Springwald, "kann kein englisch...."
Taladan
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 88


Delphi 2005 Personal
BeitragVerfasst: Mi 30.07.03 09:51 
Jepp letzte möglichkeit gut, nur leider darf der User dann weder Tab noch Enter nutzen, deshalb dieser Code:

ausblenden Delphi-Quelltext
1:
2:
    if not (key in ['0'..'9', #13, #8]) then key := #0; //alle Tasten außer Zahlen, Enter und Tab in nichts verwandeln
    if (key = #13) then key := #8; //Enter in Tab verwandeln
patrick
ontopic starontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic star
Beiträge: 1481

WIN2k, WIN XP
D6 Personal, D2005 PE
BeitragVerfasst: Mi 30.07.03 11:28 
oder du bentzt statt einer edit-kompo einen spinedit

_________________
Patrick
im zweifelsfall immer das richtige tun!!!