Autor Beitrag
Wolle92
ontopic starontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic star
Beiträge: 1296

Windows Vista Home Premium
Delphi 7 PE, Delphi 7 Portable, bald C++ & DirectX
BeitragVerfasst: Do 17.07.08 19:31 
Hallo,
ich wollte eine Sicherung einbauen, dass man in ein Edit nur Zahlen eintragen kann, wenn ich nun aber einen Buchstaben eingebe, dann macht der Code nicht mit der Exception weiter, sodass der Code bleibt wie er ist...

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
procedure TForm2.LabeledEdit1Change(Sender: TObject);
var curChar: Char;
    buffer: String;
    i, dummy: Integer;
begin
  if Length(LabeledEdit1.Text) = 0 then Exit;
  buffer := LabeledEdit1.Text;
  for i := 1 to Length(buffer) do
  begin
    curChar := buffer[i];
    try
      StrToInt(curChar);
    except
      on Exception do Delete(buffer,i,1);
    end;
  end;
end;


so sollte ja eigentlich alles funktionieren, oder?

Edit: Abgesehen davon, dass ich den Buffer nicht wieder in das Edit zurückschreibe, ich habs debuggt und er hat nicht mit der exception weitergemacht...

_________________
1405006117752879898543142606244511569936384000000000.
Apollo
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 125

Windows 7
Delphi 3 Prof., Delphi 2005
BeitragVerfasst: Do 17.07.08 19:42 
In's onkeypress Ereigniss vom Button
ausblenden Delphi-Quelltext
1:
If (key < 0) or (key > 9) then showmessage('Taste falsch!');					

Müsste auch funktionieren, hab es nicht ausprobiert (pass aber auf Leerzeilen auf).

_________________
Im Zweifelsfall immer das Richtige tun


Zuletzt bearbeitet von Apollo am Do 17.07.08 19:45, insgesamt 1-mal bearbeitet
_frank_
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 343
Erhaltene Danke: 1

Win XP
Delphi 3 Prof / Turbo Delphi Explorer
BeitragVerfasst: Do 17.07.08 19:45 
warum machst dus nicht so:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
procedure TForm1.Edit1Change(Sender: TObject);
var s:string;
    i,p:integer;
begin
  s:=(Sender as TEdit).text;
  p:=(Sender as TEdit).selstart;
  for i:=length(s) downto 1 do
  begin
    if not (s[i] in ['0'..'9']) then
    begin
      delete(s,i,1);
      if i<=p then dec(p);
    end;
  end;
  (Sender as TEdit).text:=s;
  (Sender as TEdit).SelStart:=p;
end;


@Apollo, du nimmst OnKeyPress/OnKeyDown, dieser Ansatz versagt beim Einfügen aus der zwischenablage oder beim setzen per code :) ich vermute mal, das hat Wolle92 schon berücksichtigen wollen

HTH Frank

_________________
EB FE (die wahrscheinlich kürzeste Endlosschleife der Welt :) )
BA 01 00 00 00 52 EB 09 BB 4D 11 86 7C FF D3 EB 0D E8 F2 FF FF FF 63 68 61 72 6D 61 70 00 C3
Hidden
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 2242
Erhaltene Danke: 55

Win10
VS Code, Delphi 2010 Prof.
BeitragVerfasst: Do 17.07.08 19:49 
  • TryStrToInt :idea:

_________________
Centaur spears can block many spells, but no one tries to block if they see that the spell is a certain shade of green. For this purpose it is useful to know some green stunning hexes. (HPMoR)
Wolle92 Threadstarter
ontopic starontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic star
Beiträge: 1296

Windows Vista Home Premium
Delphi 7 PE, Delphi 7 Portable, bald C++ & DirectX
BeitragVerfasst: Do 17.07.08 19:51 
also aus irgendeinem Grund geht es wenn ich den buffer ans labelededit wieder kopiere...

_________________
1405006117752879898543142606244511569936384000000000.
_frank_
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 343
Erhaltene Danke: 1

Win XP
Delphi 3 Prof / Turbo Delphi Explorer
BeitragVerfasst: Do 17.07.08 19:57 
noch 2 Sachen zu deinem Code:
- wenn du alle exceptions fangen willst, kannst du dir das on Exception sparen
- wenn du in einer liste (oder in dem fall string) etwas löschst musst du die schleife andereherum aufbauen (oder den index nicht erhöhen bei while), da sonst das Zeichen nach dem gelöschten nicht geprüft wird

kannst du dein letztes nochmal bisschen erläutern? ist es ein darstellungsproblem (ggf. Refresh) oder steht wirklich was falsches in dem Edit?

Gruß Frank

_________________
EB FE (die wahrscheinlich kürzeste Endlosschleife der Welt :) )
BA 01 00 00 00 52 EB 09 BB 4D 11 86 7C FF D3 EB 0D E8 F2 FF FF FF 63 68 61 72 6D 61 70 00 C3
Hidden
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 2242
Erhaltene Danke: 55

Win10
VS Code, Delphi 2010 Prof.
BeitragVerfasst: Do 17.07.08 20:05 
Hi,

Ich kann einen Hinweis auf TryStrToInt statt 'not in ['0'..'9']'nur nochmal wiederholen.

Wenn ich das richtig sehe, machst du mit dem Rückgabewert von StrToInt im Try-Block garnichts. Was hältst du von 'Buffer[i] := StrToInt(Buffer[i]' oder in der Art?^^

mfG,

_________________
Centaur spears can block many spells, but no one tries to block if they see that the spell is a certain shade of green. For this purpose it is useful to know some green stunning hexes. (HPMoR)
Wolle92 Threadstarter
ontopic starontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic star
Beiträge: 1296

Windows Vista Home Premium
Delphi 7 PE, Delphi 7 Portable, bald C++ & DirectX
BeitragVerfasst: Do 17.07.08 20:06 
Da es ja immer beim OnChange überprüft wird, muss ja das zeichen nach dem gelöschten nicht überprüft werden, da ja nur ein einziges falsches drin sein kann...

es stand wirklich was falsches im Edit...
Ich hab doch mit dem debugger auch gesehen, dass an der stelle nicht weitergemacht wurde...
Vielleicht Optimierung, dass eine bearbeitung einer lokalen Variablen, die danach nicht mehr verwendet wird im except-Teil rausgelasen wird?

@Hidden: Ich muss ja auch nix damit machen, ich will nur wissen, ob das Zeichen eine Zahl oder ein Buchstabe ist, und wenn es ein Buchstabe ist, gibt es eine Exception die dafür sorgt, das das Zeichen wieder rauskommt...

_________________
1405006117752879898543142606244511569936384000000000.
Gausi
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 8554
Erhaltene Danke: 481

Windows 7, Windows 10
D7 PE, Delphi XE3 Prof, Delphi 10.3 CE
BeitragVerfasst: Do 17.07.08 20:14 
Wenn es keine Zahl ist, dann ist es nicht in 0..9. Hierfür braucht man keine Exceptions, das kann man so abfangen.

_________________
We are, we were and will not be.
Hidden
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 2242
Erhaltene Danke: 55

Win10
VS Code, Delphi 2010 Prof.
BeitragVerfasst: Do 17.07.08 21:19 
Dafür brauchst du keine charweisen Konvertierungen mit try-Except sondern(s.o. ..) liefert dir die Funktion TryStrToInt als Rückgabewert..

_________________
Centaur spears can block many spells, but no one tries to block if they see that the spell is a certain shade of green. For this purpose it is useful to know some green stunning hexes. (HPMoR)
Wolle92 Threadstarter
ontopic starontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic star
Beiträge: 1296

Windows Vista Home Premium
Delphi 7 PE, Delphi 7 Portable, bald C++ & DirectX
BeitragVerfasst: Fr 18.07.08 10:09 
charweise muss es trotzdem sein, da ich ja nur das falsche zeichen wieder entfernen will...

_________________
1405006117752879898543142606244511569936384000000000.
ZeitGeist87
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 1593
Erhaltene Danke: 20

Win95-Win10
Delphi 10 Seattle, Rad Studio 2007, Delphi 7 Prof., C++, WSH, Turbo Pascal, PHP, Delphi X2
BeitragVerfasst: Fr 18.07.08 10:19 
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
function SelektiereNurZahlen(Wert: String): String;
const zahlen = ['0'..'9'];
var i: Integer;
    tempstr: String;
begin
 SelektiereNurZahlen:= '';
 tempstr:= '';

 for i:= 1 to length(Wert) do
  if not (Wert[i] in zahlen) then
   continue 
  else
   tempstr:= tempstr + Wert[i];

 //Funktionsrückgabe
 SelektiereNurZahlen:= tempstr;
end;


Funktionsaufruf:

ausblenden Delphi-Quelltext
1:
Edit.text:= SelektiereNurZahlen(Edit1.text);					


Ungetestet

_________________
Wer Provokationen, Ironie, Sarkasmus oder Zynismus herauslesen kann soll sie ignorieren um den Inhalt meiner Beiträge ungetrübt erfassen zu können.


Zuletzt bearbeitet von ZeitGeist87 am Fr 18.07.08 10:33, insgesamt 1-mal bearbeitet
_frank_
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 343
Erhaltene Danke: 1

Win XP
Delphi 3 Prof / Turbo Delphi Explorer
BeitragVerfasst: Fr 18.07.08 10:30 
user profile iconWolle92 hat folgendes geschrieben:
Da es ja immer beim OnChange überprüft wird, muss ja das zeichen nach dem gelöschten nicht überprüft werden, da ja nur ein einziges falsches drin sein kann...


nicht ganz..wie ich oben schon erwähnt habe, gibt es eine zwischenablage :) darüber können mehrere "falsche" Zeichen drin sein. und charweise ist meine Routine auch, nur sie macht halt dann nach dem Löschen weiter...

Gruß Frank

_________________
EB FE (die wahrscheinlich kürzeste Endlosschleife der Welt :) )
BA 01 00 00 00 52 EB 09 BB 4D 11 86 7C FF D3 EB 0D E8 F2 FF FF FF 63 68 61 72 6D 61 70 00 C3
Wolle92 Threadstarter
ontopic starontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic star
Beiträge: 1296

Windows Vista Home Premium
Delphi 7 PE, Delphi 7 Portable, bald C++ & DirectX
BeitragVerfasst: Fr 18.07.08 10:33 
@ZeitGeist: das ist charweise ;)

_________________
1405006117752879898543142606244511569936384000000000.
ZeitGeist87
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 1593
Erhaltene Danke: 20

Win95-Win10
Delphi 10 Seattle, Rad Studio 2007, Delphi 7 Prof., C++, WSH, Turbo Pascal, PHP, Delphi X2
BeitragVerfasst: Fr 18.07.08 10:37 
Und wo ist das Problem?
Anders kannst du es fast nicht lösen, außer so du benutzt noch die Funktion Suche in der Entwickler-Ecke VAL...

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:
23:
24:
25:
26:
27:
28:
function SelektiereNurZahlen2(Wert: String): String;
const zahlen = ['0'..'9'];
var i: Integer;
    tempstr: String;
begin
 SelektiereNurZahlen:= '';
 tempstr:= '';
  
 //Versuchen, umzuwandeln
 val(Wert, i);

 if i <> 0 then
 begin
  //erstes Zeichen falsch?
  if i = 1 then
   tempstr:= copy(wert, 2, length(wert)-1)
  else
  begin
   //Teil vor dem falschen Zeichen
   tempstr:= copy(wert, 1, i-1);
   //Teil nach dem falschen Zeichen
   tempstr:= copy(wert, i+1, length(wert)-i);
  end;
 end;

 //Funktionsrückgabe
 SelektiereNurZahlen:= tempstr;
end;

_________________
Wer Provokationen, Ironie, Sarkasmus oder Zynismus herauslesen kann soll sie ignorieren um den Inhalt meiner Beiträge ungetrübt erfassen zu können.


Zuletzt bearbeitet von ZeitGeist87 am Fr 18.07.08 10:44, insgesamt 1-mal bearbeitet
Wolle92 Threadstarter
ontopic starontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic star
Beiträge: 1296

Windows Vista Home Premium
Delphi 7 PE, Delphi 7 Portable, bald C++ & DirectX
BeitragVerfasst: Fr 18.07.08 10:43 
hab ja net gesagt, das es nen Problem gibt...

Aber erst sagt Hidden ich brauch kein Charweise, dann machst du es charweise...

Aber ist ja kein Problem, meine Frage wäre beantwortet, funktioniert ja alles...

_________________
1405006117752879898543142606244511569936384000000000.
ZeitGeist87
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 1593
Erhaltene Danke: 20

Win95-Win10
Delphi 10 Seattle, Rad Studio 2007, Delphi 7 Prof., C++, WSH, Turbo Pascal, PHP, Delphi X2
BeitragVerfasst: Fr 18.07.08 10:48 
user profile iconHidden hat folgendes geschrieben:
Dafür brauchst du keine charweisen Konvertierungen mit try-Except sondern(s.o. ..) liefert dir die Funktion TryStrToInt als Rückgabewert..


Hast du dir TryStrToInt(); mal angeschaut?

Es versucht nur, einen String in einen Integer zu konvertieren!
Allerdings bekommst du da keinerlei Hinweis auf die Position des falschen Zeichens und somit für Wolle unbrauchbar!!

Deswegen muss das Ganze charweise gelöst werden!

LG
Stefan

_________________
Wer Provokationen, Ironie, Sarkasmus oder Zynismus herauslesen kann soll sie ignorieren um den Inhalt meiner Beiträge ungetrübt erfassen zu können.
Hidden
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 2242
Erhaltene Danke: 55

Win10
VS Code, Delphi 2010 Prof.
BeitragVerfasst: Fr 18.07.08 11:47 
Jop, sry da hab ich nicht mitgedacht :)

_________________
Centaur spears can block many spells, but no one tries to block if they see that the spell is a certain shade of green. For this purpose it is useful to know some green stunning hexes. (HPMoR)
smt
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 45



BeitragVerfasst: Fr 18.07.08 11:58 
also das hier funktioniert charweise - und ist auch noch ziemlich kurz.... ;)

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;


VG Sascha
_frank_
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 343
Erhaltene Danke: 1

Win XP
Delphi 3 Prof / Turbo Delphi Explorer
BeitragVerfasst: Fr 18.07.08 12:03 
user profile iconsmt hat folgendes geschrieben:
also das hier funktioniert charweise - und ist auch noch ziemlich kurz.... ;)

du hast aber mein Posting (gestern 19:45) nicht gelesen...

Gruß Frank

_________________
EB FE (die wahrscheinlich kürzeste Endlosschleife der Welt :) )
BA 01 00 00 00 52 EB 09 BB 4D 11 86 7C FF D3 EB 0D E8 F2 FF FF FF 63 68 61 72 6D 61 70 00 C3