MASKA do IP

0

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;
0

Za dużo warunków stosujesz.
Przyjrzyj się jak często odnosisz się sprawdzając i konwertując przy tym pozycję kursora.

if (StrToInt(Text[PozycjaKursora]) >= 5) then

Po co stosujesz porównanie ">=" ?
Nie lepiej zamiast tego zastosować po prostu:

if (...) > 4) then

Ogólnie jest też pare innych problemów jak choćby:

  if ... then
    begin
      ...
      Exit;
    end
  else
  if ... then
    begin
      ...
      Exit;
    end;

Jak już wychodzisz przez Exit, to nie ma sensu robić warunku ELSE.

0

Opi wiem ze kod nie jest idealny ale dziala tak jak chcialem :) Mozesz go ponaprawiac jesli masz ochote :)

0

dodaj ten kod do gotowców w dziale delphi - ten serwis to nie tylko forum. a swoją drogą mógłbyś go (kod, nie serwis) zoptymalizować i porozbijać na małe, czytelne metody.

1 użytkowników online, w tym zalogowanych: 0, gości: 1