PDA

Ver la Versión Completa : TMaskEdit y mascara para IP


mjjj
17-07-2013, 17:45:03
Estimados, quiero utilizar un maskedit para ingresar una dirección IP.
La mascara que utilicé es 999.999.999.999;1:_

Pero tiene un problema, por ejemplo cuando ingreso 192.23.2.123, y asignar el texto del componente me arroja 192. 23. 2.123

Que mascara utilizarían ustedes?

ecfisa
17-07-2013, 18:32:15
Hola mjjj.

Revisa si te sirve el ejemplo de este enlace de Embarcadero : MaskEdit (http://docwiki.embarcadero.com/CodeExamples/XE4/en/EditMask_%28Delphi%29).

Otra opción es usar el IP address control (http://msdn.microsoft.com/en-us/library/windows/desktop/bb761374%28v=vs.85%29.aspx) de windows, aca tenes un ejemplo: ...use the IP Address Control in a Form? (http://www.swissdelphicenter.ch/torry/showcode.php?id=1132).

Saludos :)

ecfisa
17-07-2013, 19:18:24
Hola mjjj.

Se me ocurrió otra opción usando cuatro TEdit (para el ejemplo: Edit1, Edit2, Edit3 y Edit4).


...
implementation

function MakeIP(Oct: array of string): string;
var
i: Integer;
begin
for i:= Low(Oct) to High(Oct) do
Result := Result + Oct[i] + '.';
SetLength(Result, Length(Result)-1);
end;

procedure TForm1.FormCreate(Sender: TObject);
var
i: Integer;
Ed: TEdit;
begin
for i:= 1 to 4 do
begin
Ed := TEdit(FindComponent('Edit'+IntToStr(i)));
Ed.MaxLength := 3;
SetWindowLong(Ed.Handle, GWL_STYLE,
GetWindowLong(Ed.Handle, GWL_STYLE) + ES_NUMBER);
end;
end;

// Evento asignado a los 4 edits
procedure TForm1.EditExit(Sender: TObject);
begin
if not (StrToInt(TEdit(Sender).Text) in [0..255]) then
begin
TEdit(Sender).SetFocus;
raise Exception.Create('Valor fuera de rango');
end;
end;
...

Llamada de ejemplo:

procedure TForm1.Button1Click(Sender: TObject);
begin
ShowMessage(MakeIP([Edit1.Text, Edit2.Text, Edit3.Text, Edit4.Text]));
end;


Saludos. :)