Club Delphi  
    FTP   CCD     Buscar   Trucos   Trabajo   Foros

Retroceder   Foros Club Delphi > Principal > OOP
Registrarse FAQ Miembros Calendario Guía de estilo Temas de Hoy

Grupo de Teaming del ClubDelphi

Respuesta
 
Herramientas Buscar en Tema Desplegado
  #1  
Antiguo 04-09-2008
rauros rauros is offline
Miembro
 
Registrado: feb 2008
Ubicación: Alicante - Sax / Sax - Alicante
Posts: 235
Poder: 17
rauros Va por buen camino
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.
Responder Con Cita
  #2  
Antiguo 04-09-2008
Avatar de roman
roman roman is offline
Moderador
 
Registrado: may 2003
Ubicación: Ciudad de México
Posts: 20.269
Poder: 10
roman Es un diamante en brutoroman Es un diamante en brutoroman Es un diamante en bruto
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
Responder Con Cita
  #3  
Antiguo 04-09-2008
[maeyanes] maeyanes is offline
Capo de los Capos
 
Registrado: may 2003
Ubicación: Campeche, México
Posts: 2.732
Poder: 23
maeyanes Va por buen camino
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...
Responder Con Cita
  #4  
Antiguo 04-09-2008
Avatar de xEsk
[xEsk] xEsk is offline
Miembro Premium
 
Registrado: feb 2006
Posts: 454
Poder: 19
xEsk Va por buen camino
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
Responder Con Cita
  #5  
Antiguo 04-09-2008
rauros rauros is offline
Miembro
 
Registrado: feb 2008
Ubicación: Alicante - Sax / Sax - Alicante
Posts: 235
Poder: 17
rauros Va por buen camino
Vale, voy a probar.
Responder Con Cita
  #6  
Antiguo 04-09-2008
rauros rauros is offline
Miembro
 
Registrado: feb 2008
Ubicación: Alicante - Sax / Sax - Alicante
Posts: 235
Poder: 17
rauros Va por buen camino
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'
Responder Con Cita
  #7  
Antiguo 04-09-2008
[maeyanes] maeyanes is offline
Capo de los Capos
 
Registrado: may 2003
Ubicación: Campeche, México
Posts: 2.732
Poder: 23
maeyanes Va por buen camino
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...
Responder Con Cita
  #8  
Antiguo 05-09-2008
rauros rauros is offline
Miembro
 
Registrado: feb 2008
Ubicación: Alicante - Sax / Sax - Alicante
Posts: 235
Poder: 17
rauros Va por buen camino
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;
Responder Con Cita
Respuesta



Normas de Publicación
no Puedes crear nuevos temas
no Puedes responder a temas
no Puedes adjuntar archivos
no Puedes editar tus mensajes

El código vB está habilitado
Las caritas están habilitado
Código [IMG] está habilitado
Código HTML está deshabilitado
Saltar a Foro

Temas Similares
Tema Autor Foro Respuestas Último mensaje
Heredar un frame. adebonis OOP 4 21-05-2008 20:10:57
Heredar Objetos ADO mcarazas Varios 3 19-10-2006 10:07:20
Problema al heredar frames choty Varios 5 21-06-2006 17:03:47
Heredar objeto carlomagno OOP 5 19-06-2006 01:00:05
Heredar del DBNavigator Carlosj OOP 0 11-01-2005 09:29:43


La franja horaria es GMT +2. Ahora son las 16:13:56.


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
Copyright 1996-2007 Club Delphi