Foros Club Delphi

Foros Club Delphi (https://www.clubdelphi.com/foros/index.php)
-   OOP (https://www.clubdelphi.com/foros/forumdisplay.php?f=5)
-   -   Heredar eventos de un Edit. (https://www.clubdelphi.com/foros/showthread.php?t=59693)

rauros 04-09-2008 19:43:57

Heredar eventos de un Edit.
 
Saludos a todos. Estoy creando un TIpEdit, para hacer más fácil escribir ip, y me encuentro con el problema de que no sé heredar el evento OnChange para usarlo. Me gustaría usar ese evento para evitar escribir letras, y otras cosas.

Hasta ahora tengo hecho esto, pero me da un error que más abajo voy a poner:

Código Delphi [-]
Constructor TIpEdit.Crear(Padre: TWinControl;
X: Integer; Y: Integer;
TX: Integer; TY: Integer);
Begin
  inherited;
  With TIpEdit.Create(Padre) do Begin
    Self.OnChange:=Cambiar(Padre); <---- Línea 26
  End;
End;

[Error] EditSoloIp.pas(26): Incompatible types: 'TNotifyEvent' and 'procedure, untyped pointer or untyped parameter'

No he continuado el constructor porque me falta esa ayuda. Gracias.

roman 04-09-2008 19:55:21

Cuando estás creando componentes, no se usan los eventos directamente. Lo que debes hacer es redefinir el método Change:

Código Delphi [-]
type
  TIPEdit = class(TEdit)
    ...
    procedure Change; override;
    ...
  end;

implementation

procedure TIPEdit.Change; 
begin
  inherited;
  
  { aquí tu código }
end;

// Saludos

maeyanes 04-09-2008 20:01:13

Hola...

Esto que mencionas lo puedes hacer de dos formas...

Uno, puedes redefinir el método que dispara el evento, que en el TEdit es Change:

Código Delphi [-]
TIpEdit = class(TEdit)
protected
  procedure Change; override;
end;

implementation

procedure TIpEdit.Change;
begin
  // Código que quieres implementar
  inherited // Mandamos llamar el método heredado para que se dispare
              // correctamente el evento OnChange
end;

La forma en que lo quieres hacer, tendrías que crear un método en tu clase que tenga como parámetro Sender: TObject:

Código Delphi [-]
constructor TIpEdit.Create(AOwner: TComponent);
begin
  inherited;
  // Asignas el manejador de evento al evento OnChange
  OnChange = Changed
end;

procedure TIpEdit.Changed(Sender: TObject);
begin
  // Lo que desees hacer
end;

El lado malo de esta última forma es que si en tiempo de diseño asignan un manejador de eventos para OnChange, se perdería el que estableciste al momento de crear el componente.


Saludos...

xEsk 04-09-2008 20:02:36

Para controlar lo que se escribe es mejor capturar el evento OnKeyPress, así si es un caracter invalido ya no dejas escribirlo.

Por ejemplo, en el OnKeyPress puedes poner algo parecido a esto:
Código Delphi [-]
if not (key in ['0'..'9','.']) then Key:=#0;

Como alternativa, también tienes el componente TMaksEdit, y un ejemplo de mascara: 000.000.000.000;1;_

Saludos

rauros 04-09-2008 20:34:20

Vale, voy a probar.

rauros 04-09-2008 20:42:52

Tengo esto y me da el siguiente error en la línea de OnKeyPress:

Código Delphi [-]
Procedure TipEdit.ApretarUnBoton(Sender: TObject);
Begin
End;

Constructor TIpEdit.Crear(Padre: TWinControl;
X: Integer; Y: Integer;
TX: Integer; TY: Integer);
Begin
  With TIpEdit.Create(Padre) do Begin
    Self.OnKeyPress:=ApretarUnBoton;
  End;
End;

[Error] EditSoloIp.pas(30): Incompatible types: 'Parameter lists differ'

maeyanes 04-09-2008 20:49:58

Hola...

En lugar de usar el evento OnKeyPress, es mejor que redefinas el método KeyPress:

Código Delphi [-]
TIpEdit = class(TEdit)
protected
  procedure KeyPress(var Key: Char); override;
end;

implementation

procedure TIpEdit.KeyPress(var Key: Char);
begin
  // Tu código
  inherited
end;


Saludos...

rauros 05-09-2008 02:45:18

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;


La franja horaria es GMT +2. Ahora son las 21:40:03.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Traducción al castellano por el equipo de moderadores del Club Delphi