Bua, me he tirado toda la tarde pero mirad lo que he conseguido:
Conjunto de edits especiales para escribir ips:
Código Delphi
[-]unit EditSoloIp;
interface
Uses StdCtrls, Controls, SysUtils, Windows;
Type
TEditByte=Class(TEdit)
Public
SiguienteEdit: HWnd;
protected
procedure KeyPress(var Key: Char); override;
End;
Type
TEditIp=Object
Edits: Array [1..4] of TEditByte;
Separadores: Array [1..3] of TLabel;
Public
Function GetIp: String;
Procedure SetIp(Ip: String);
Procedure Crear(Padre: TWinControl; X: Integer; Y: Integer; Separador: Char);
End;
implementation
Function TEditIp.GetIp: String;
Begin
Result:=Edits[1].Text + '.' + Edits[2].Text + '.' + Edits[3].Text + '.' + Edits[4].Text
End;
Procedure TEditIp.SetIp(Ip: String);
Var
I,b: Integer;
Bytes: Array [1..4] of string;
Begin
IP:=Trim(ip);
I:=0;
If not (Ip[1] in ['0'..'9']) then
Begin
MessageBox(0,'Error, esa ip es incorrecta','Error IpToStr',16);
Exit;
End;
b:=1;
Repeat Inc(I);
If Ip[i] = '.' then
Inc(b)
Else If B > 4 Then Begin
MessageBox(0,'Error, esa ip es incorrecta','Error IpToStr',16);
Exit;
End Else If not (Ip[i] in ['0'..'9']) then
Begin
MessageBox(0,'Error, esa ip es incorrecta','Error IpToStr',16);
Exit;
End
Else
Bytes[b]:=Bytes[b] + Ip[i];
Until I = Length(ip);
For I := 1 to 4 do
If StrToInt(Bytes[i]) > 255 then Bytes[i]:='255';
Edits[1].Text:=bytes[1];
Edits[2].Text:=bytes[2];
Edits[3].Text:=bytes[3];
Edits[4].Text:=bytes[4];
End;
Procedure TEditByte.KeyPress(var key: Char);
Begin
If Key = ' ' then Begin
Windows.SetFocus(SiguienteEdit);
Key:=#0;
End;
If not (key in ['0'..'9',' ',Char(8),Char(46)]) then Key:=#0;
Inherited;
If Text = '' then exit;
If StrToInt(Text) > 255 Then Begin Text:='255';
Key:=#0;
End;
End;
Procedure TEditIP.Crear(Padre: TWinControl;
X: Integer; Y: Integer; Separador: Char);
Var
I: Byte;
Begin
For I:=1 to 4 Do
Begin
Edits[i]:=TEditByte.Create(Padre);
Edits[i].Parent:=Padre;
Edits[i].Text:='255';
Edits[i].Top:=x;
Edits[i].Left:=y;
Edits[i].Height:=17;
Edits[i].Width:=25;
Edits[i].Visible:=True;
Y:=Y + 32;
End;
Y:=y - 126 + 24;
For I:=1 to 3 Do
Begin
Separadores[i]:=TLabel.Create(Padre);
Separadores[i].Parent:=Padre;
Separadores[i].Caption:=Separador;
Separadores[i].Top:=x;
Separadores[i].Left:=y;
Separadores[i].Height:=20;
Separadores[i].Width:=4;
Separadores[i].Font.Size:=12;
Separadores[i].Visible:=True;
Y:=Y + 32;
End;
Edits[1].SiguienteEdit:=Edits[2].Handle;
Edits[2].SiguienteEdit:=Edits[3].Handle;
Edits[3].SiguienteEdit:=Edits[4].Handle;
Edits[4].SiguienteEdit:=Edits[1].Handle;
End;
End.
Un ejemplo para crearlo:
var
EditIp: TEditIp;
procedure TForm1.FormCreate(Sender: TObject);
begin
EditIp.Crear(Form1,96,296,'.');
end;