YanRamgoulam
Programmer
Hi Everyone. I am trying to make a component inherited from TMaskEdit. The idea is to make a reusable code for all my applications that involve IP addresses. As you know an IP is made up of 4 octets. Each octet between 1 and 3 numbers long. the valid values range from 0-255. I have been able to set the mask, but further validation can be made.
My problem is I dont seem to succeed in creating a check triggered on events similar to OnKeyPress or OnChange. Below is my code how would you guys make this work?
My problem is I dont seem to succeed in creating a check triggered on events similar to OnKeyPress or OnChange. Below is my code how would you guys make this work?
Code:
unit UntIPMask;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, Mask;
type
TIPMask = class(TMaskEdit)
private
// procedure CMExit(var Message: TCMExit); message CM_EXIT;
procedure CMTextChanged(var Message: TMessage); message CM_TEXTCHANGED;
{ Private declarations }
protected
{ Protected declarations }
public
{ Public declarations }
constructor Create(AOwner: TComponent); override;
procedure ValidateEdit;
published
{ Published declarations }
end;
procedure Register;
implementation
constructor TIPMask.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
editmask := '009\.009\.009\.009;1; ';
end;
procedure TIPMask.CMTextChanged(var Message: TMessage);
begin
showmessage(string(text));
end;
procedure CMExit(var Message: TCMExit);
var
Octet1, Octet2, Octet3,Octet4: Word;
theString: String;
I:integer;
begin
if IsMasked and not (csDesigning in ComponentState) then
ValidateEdit;
// if text <> ' . . . ' then
// begin
// parse out octets
theString := Text;
i := Pos('.', theString);
Octet1 := StrToIntDef(Copy(theString, 1, i - 1),0);
theString := Copy(theString, i + 1, 20);
if (Octet1 > 255) Or (Octet1 < 0) then
begin
Showmessage('Range of values between 0 and 255');
text := '0 .' + theString;
exit;
end;
i := Pos('.', theString);
Octet2 := StrToIntDef(Copy(theString, 1, i - 1),0);
theString := Copy(theString, i + 1, 20);
if (Octet2 > 255) Or (Octet2 < 0) then
begin
Showmessage('Range of values between 0 and 255');
text := IntToStr(Octet1) + '0 .' + theString;
exit;
end;
i := Pos('.', theString);
Octet3 := StrToIntDef(Copy(theString, 1, i - 1),0);
theString := Copy(theString, i + 1, 20);
if (Octet3 > 255) Or (Octet3 < 0) then
begin
Showmessage('Range of values between 0 and 255');
text := IntToStr(Octet1) + IntToStr(Octet2) + '0 .' + theString;
exit;
end;
i := Pos('.', theString);
Octet4 := StrToIntDef(Copy(theString, 1, i - 1),0);
if (Octet4 > 255) Or (Octet4 < 0) then
begin
Showmessage('Range of values between 0 and 255');
text := IntToStr(Octet1) + IntToStr(Octet2) + IntToStr(Octet3) + '0 .';
exit;
end;
inherited;
end;
procedure TIPMask.ValidateEdit;
begin
inherited;
end;
procedure Register;
begin
RegisterComponents('Samples', [TIPMask]);
end;
end.