source: trunk/Packages/TEditExtensions/UIPv4Edit.pas

Last change on this file was 5, checked in by chronos, 12 years ago
  • Added: Required packages.
File size: 2.3 KB
Line 
1unit UIPv4Edit;
2
3{$mode Delphi}{$H+}
4
5interface
6
7uses
8 Classes, SysUtils, StdCtrls, StrUtils;
9
10type
11
12{ TIPv4Edit }
13
14 TIPv4Edit = class(TEdit)
15 private
16 FAddress: Cardinal;
17 procedure ChangeExecute(Sender: TObject);
18 procedure SetAddress(const AValue: Cardinal);
19 public
20 function TryStrToIPv4(S: string; out Value: Cardinal): Boolean;
21 constructor Create(AOwner: TComponent); override;
22 destructor Destroy; override;
23 published
24 property Address: Cardinal read FAddress write SetAddress;
25 end;
26
27procedure Register;
28
29implementation
30
31procedure Register;
32begin
33 RegisterComponents('TEditExtensions', [TIPv4Edit]);
34end;
35
36{ TIPv4Edit }
37
38procedure TIPv4Edit.ChangeExecute(Sender: TObject);
39var
40 NewText: string;
41 Temp: Cardinal;
42begin
43 NewText := Text;
44 Delete(NewText, SelStart + 1, 1);
45 if TryStrToIPv4(NewText, Temp) then
46 FAddress := Temp;
47 SetAddress(FAddress);
48end;
49
50procedure TIPv4Edit.SetAddress(const AValue: Cardinal);
51var
52 LastPos: Integer;
53 Adr: string;
54 I: Integer;
55 Octet: string;
56begin
57 FAddress := AValue;
58 LastPos := SelStart;
59 OnChange := nil;
60 Adr := '';
61 for I := 0 to 3 do begin
62 Octet := IntToStr((FAddress shr (24 - (I * 8))) and $ff);
63 Adr := Adr + Octet + DupeString(' ', 3 - Length(Octet));
64 if I < 3 then Adr := Adr + '.';
65 end;
66 Text := Adr;
67 OnChange := ChangeExecute;
68 if LastPos = 3 then LastPos := 4;
69 if LastPos = 7 then LastPos := 8;
70 if LastPos = 11 then LastPos := 12;
71 SelStart := LastPos;
72end;
73
74function TIPv4Edit.TryStrToIPv4(S: string; out Value: Cardinal): Boolean;
75var
76 P: Integer;
77 Octet: Integer;
78 OctetText: string;
79 I: Integer;
80begin
81 Result := False;
82 Value := 0;
83 for I := 0 to 3 do begin
84 if I < 3 then begin
85 P := Pos('.', S);
86 if P > 0 then
87 OctetText := Copy(S, 1, P - 1) else Exit;
88 Delete(S, 1, P);
89 end else OctetText := S;
90 if TryStrToInt(Trim(OctetText), Octet) then begin
91 if (Octet >= 0) and (Octet <= 255) then
92 Value := Value or (Byte(Octet) shl (24 - (I * 8)))
93 else Exit;
94 end else Exit;
95 end;
96 Result := True;
97end;
98
99constructor TIPv4Edit.Create(AOwner: TComponent);
100begin
101 inherited Create(AOwner);
102 OnChange := ChangeExecute;
103end;
104
105destructor TIPv4Edit.Destroy;
106begin
107 inherited Destroy;
108end;
109
110end.
111
Note: See TracBrowser for help on using the repository browser.