Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Component Event

Status
Not open for further replies.

YanRamgoulam

Programmer
Feb 5, 2007
3
CA
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?


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.
 
Hm, in Vista you won't be able to catch the new IPv6 format that has way more digits in it and a 'new' colon separator... I'd go for a blunt TEdit-control and either not handle the formatting at all, or have it check by trying to connect to the entered server-name _or_ -address. A Test-button should be able to handle that situation.

HTH
TonHu
 
Hi TonHu,
Thanks for your reply but it doesnt not help me at all. First thing i dont want to connect to anything really... This is a component.

1) I dont really care about vista, AIX or even NASA supercomputers. This is an attempt to learn a simple concept.

2) The point is to build a component. I can use a TMaskEdit with an onchange event or keypress event. Which is already what I am using, but I am learning by trying something different.
 
Hi YanRamgoulam,

Here's some code that you could adapt to your needs...
-----------------------------------------------------------
type
TNotifyValid = procedure(Sender: TObject; var Cancel:
Boolean) of object;
TMyEdit = class(TEdit)
private
{ Private declarations }
eTextValid: TNotifyValid;
function CheckValid(sValue: String=''): Boolean;
procedure CheckLostFocus(var Msg: TMessage);
message WM_KILLFOCUS;
protected
{ Protected declarations }
procedure KeyPress(var Key: Char); override;
public
{ Public declarations }
constructor Create(AOwner: TComponent); override;
destructor Destroy(); override;
published
{ Published declarations }
property OnTextValid: TNotifyValid read eTextValid
write eTextValid;
end;

procedure Register;
begin
RegisterComponents('MyVcl', [TMyEdit]);
end;

constructor TMyEdit.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
end;

destructor TMyEdit.Destroy();
begin
inherited Destroy;
end;

procedure TMyEdit.CheckLostFocus(var Msg: TMessage);
begin
CheckValid;
inherited;
end;

procedure TMyEdit.KeyPress(var Key: Char);
begin
if (Key = #13) then CheckValid
end;

function TCanimexEdit.CheckValid(): Boolean;
var bCancel: Boolean;
begin
bCancel := False;
// Check out the mask...

if not bCancel then
if Assigned(eTextValid) then
begin
// New event if specific validations...
eTextValid(Self, bCancel);
if bCancel then Text := '';
end;
Result := bCancel=False;
end;
-----------------------------------------------------------

Hope this helps,

Rej Cloutier
 
perfectly and exactly what i needed. thanks a LOT for your help. great stuff.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top