Witam serdecznie ten post bedzie nieco inny niz inne gdyz nie szukam pomocy tylko chcialem cos dodac od siebie dla tego portalu. Troche szukalem na ten temat i nie bylo nic co by mnie zadowalalo tak do konca wiec napisalem cos sam. (rowniez dzieki pomocy tego forum) Otoz potrzebowalem maske do IP ale taka na key presie w zwyklym edicie. Udalo mi sie cos tam napisac i spelnia to moje oczekiwania. Mam nadzieje ze rowniez wam sie przyda moj kod a kto wie moze ktos go jeszcze ulepszy. Pozdrawiam i polecam :)
procedure TMainForm.edtIPKeyPress(Sender: TObject; var Key: Char);
var
Przecinek, tmp : string;
PozycjaKursora : Integer;
PozycjaPrzecinka : Integer;
IloscPrzecinkow : integer;
IloscMiejscPoPrzecinku : integer;
i : Integer;
begin
Przecinek := '.';
IloscPrzecinkow := 0;
IloscMiejscPoPrzecinku := 3;
with Sender as TEdit do
begin
SelStart := Length(Text);
PozycjaKursora := SelStart - SendMessage(Handle,
EM_LINEINDEX, PozycjaKursora, 0);
PozycjaPrzecinka := Pos(Przecinek, Text);
//sprawdzamy czy string nie jest dluzszy niz maxlenght
if (Length(Text) >= MaxLength) and (Key <> #8) then
begin
if (PozycjaKursora >2) and (Text[PozycjaKursora] <> Przecinek[1]) and (Text[PozycjaKursora-1] <> Przecinek[1]) and (Text[PozycjaKursora-2] <> Przecinek[1]) then
begin
if (StrToInt(Text[PozycjaKursora-2]) >= 2) then
begin
tmp := Text;
tmp[PozycjaKursora-2] := '2';
Text := tmp;
SelStart := PozycjaKursora + 2;
if (StrToInt(Text[PozycjaKursora-1]) >= 5) then
begin
tmp := Text;
tmp[PozycjaKursora-1] := '5';
Text := tmp;
SelStart := PozycjaKursora + 2;;
begin
if (StrToInt(Text[PozycjaKursora]) >= 5) then
tmp := Text;
tmp[PozycjaKursora] := '5';
Text := tmp;
SelStart := PozycjaKursora + 2;
end;
end;
end;
end;
Key := Chr(0);
Exit;
end;
if not ((Key in ['0'..'9', #8]) or (Key = Przecinek[1])) then
begin
Key := Chr(0);
Exit;
end
else
begin
//pierwszy znak nie moze byc kropka
if (Length(Text) = 0) and ((Key = Przecinek[1]) OR (Key = #48)) then
begin
Key := Chr(0);
Exit;
end;
if (PozycjaKursora > 0) then
begin
// liczymy kropki
for i := 0 to Length(Text) do
begin
if Text[i] = Przecinek[1] then
begin
Inc(IloscPrzecinkow);
// i pobieramy pozycje ostatniego przecinka
PozycjaPrzecinka := PosEx(Przecinek, Text, i);
end;
end;
end;
// nie mozna wstawic 2 kropek obok siebie
if (PozycjaKursora >0) and (Key = Przecinek[1]) then
begin
if Text[PozycjaKursora] = Przecinek[1] then
begin
Key := Chr(0);
Exit;
end;
if (IloscPrzecinkow >= 3) and (Text[PozycjaKursora+1] = Przecinek[1])then
begin
Key := Chr(0);
Exit;
end;
end;
// I tu sie zaczyna zabawa
if (PozycjaKursora >0) then
begin
// sprawdzamy kilka warunkow
if ((PozycjaKursora >= 3) and (Key <> #8) and (PozycjaKursora < MaxLength) and (Text[PozycjaKursora] <> Przecinek[1]) and (Text[PozycjaKursora-1] <> Przecinek[1]) and (Text[PozycjaKursora-2] <> Przecinek[1])) then
begin
//jesli sa spelnione sprawdzamy kolejne
if (IloscPrzecinkow < 3) and (Key <> Przecinek[1]) then
begin
// po 3 liczbie dodajemy kropke automatycznie i nasz wcisniety klawisz
Text :=Text + '.' + Key;
SelStart := PozycjaKursora + 2;
// tu jest funkcja sprawdzajaca czy liczba nie jest wieksza niz 255
if (Text[PozycjaKursora] <> Przecinek[1]) and (Text[PozycjaKursora-1] <> Przecinek[1]) and (Text[PozycjaKursora-2] <> Przecinek[1]) then
begin
if (StrToInt(Text[PozycjaKursora-2]) >= 2) then
begin
tmp := Text;
tmp[PozycjaKursora-2] := '2';
Text := tmp;
SelStart := PozycjaKursora + 2;
if (StrToInt(Text[PozycjaKursora-1]) >= 5) then
begin
tmp := Text;
tmp[PozycjaKursora-1] := '5';
Text := tmp;
SelStart := PozycjaKursora + 2;;
begin
if (StrToInt(Text[PozycjaKursora]) >= 5) then
tmp := Text;
tmp[PozycjaKursora] := '5';
Text := tmp;
SelStart := PozycjaKursora + 2;
end;
end;
end;
end;
end;
// nie moze byc 2 kropek obok siebie
if Text[PozycjaKursora+1] = Przecinek[1] then
begin
Key := Chr(0);
Exit;
end;
end;
end;
if (IloscPrzecinkow >= 3)then
begin
//jesli sa wiecej niz 3 przecinki to skracamy edita i zostawiamy mu 3 miejsca po przecinku
MaxLength := PozycjaPrzecinka + IloscMiejscPoPrzecinku;
if (Key = Przecinek[1]) then
begin
Key := Chr(0);
Exit;
end;
end;
//to sprawdzanie 255 jest tu poraz drugi gdyz wpisanie po 3 znaku '.' uniemozliwialo
//sprawdzenie w poprzedniej funkcji z tego wzgledu ze '.' jest wpisywana automatycznie
if (PozycjaKursora >2) and (Text[PozycjaKursora] <> Przecinek[1]) and (Text[PozycjaKursora-1] <> Przecinek[1]) and (Text[PozycjaKursora-2] <> Przecinek[1]) then
begin
if (StrToInt(Text[PozycjaKursora-2]) >= 2) then
begin
tmp := Text;
tmp[PozycjaKursora-2] := '2';
Text := tmp;
SelStart := PozycjaKursora + 2;
if (StrToInt(Text[PozycjaKursora-1]) >= 5) then
begin
tmp := Text;
tmp[PozycjaKursora-1] := '5';
Text := tmp;
SelStart := PozycjaKursora + 2;;
begin
if (StrToInt(Text[PozycjaKursora]) >= 5) then
tmp := Text;
tmp[PozycjaKursora] := '5';
Text := tmp;
SelStart := PozycjaKursora + 2;
end;
end;
end;
end;
end;
end;
end;