继承TPanel控件生成了个新控件,增加了OnKeyPress事件,但有个问题,在动态生成控件时事件不响应,不动态时可响应...VCL代码:
unit EdtSelect;
interface
uses
Windows, Messages, SysUtils, Classes, Controls, StdCtrls, DB, ADODB, Buttons, Forms,
ComCtrls, ExtCtrls, Graphics;
type TKeyPressEvent = procedure(Sender: TObject; var Key: Char) of object;
TEdtSelect = class(TPanel)
private
{ Private declarations }
FOnKeyPress: TKeyPressEvent;
strDataFromType:String;
edt, edt2: Tedit;
cbm: TComboBox;
datetime, datetime2: TDateTimePicker;
memo: Tmemo;
btn: TSpeedButton;
protected
{ Protected declarations }
procedure Notification(AComponent: TComponent;
Operation: TOperation); override;
procedure SetFocus(); override;
procedure btnClick(Sender: TObject);
procedure KeyPress(Sender: TObject; var Key: Char) ; //; dynamic;
public
{ Public declarations }
procedure ReSetPosition();
procedure CreateCtl(iCtlType: Integer);
procedure SetText();
function GetText(): string;
procedure SetColor();
procedure SetCtlColor(cl: TColor);
procedure WinMsg(var Msg: TMsg; var Hnd: Boolean); //处理消息;
published
{ Published declarations }
property AdoCn: TADOConnection read dbcn write dbcn; //数据连接
property QuerySQL: string read Sql write Sql; //数据连接
property QueryKey: string read Key write Key; //数据连接
property iID: Integer read PKID write PKID; //数据关联
property Text: string read Txt write Txt; //数据关联
property CtlType: Integer read iType write iType; //数据关联
property DataFromType:String read strDataFromType write strDataFromType; //TCOMBOBOX数据来源 T : ' '+ ' ' ,S : TABLE FROM QuerySQL
property OnKeyPress: TKeyPressEvent read FOnKeyPress write FOnKeyPress;
end;
var
strOK: string;
ID: Integer; //数据关联
//iType: Integer;
procedure Register;
implementation
uses Select;
procedure Register;
begin
RegisterComponents('Standard', [TEdtSelect]);
end;procedure TEdtSelect.KeyPress(Sender: TObject; var Key: Char);
begin
end;procedure TEdtSelect.Notification(AComponent: TComponent;
Operation: TOperation);
begin
inherited;
case Operation of
opRemove:
if AComponent = dbcn then
dbcn := nil;
end;
end;
procedure TEdtSelect.CreateCtl(iCtlType: Integer);
var
tmpadoq: TADOQuery;
Terms: TStringList;
p: Integer;begin
iType := iCtlType;
case iType of
1: begin
edt := Tedit.Create(Self);
edt.Parent := Self;
edt.Width := Self.Width;
edt.Left := 0;
edt.Top := 0;
edt.Height := Self.Height;
edt.Text := Txt;
edt.Color := Self.Color;
edt.OnClick := CtlClick;
edt.OnKeyPress :=self.onKeyPress;
end; 2: begin
edt2 := Tedit.Create(Self);
edt2.Parent := Self;
edt2.Width := Self.Width - 20;
edt2.Left := 0;
edt2.Top := 0;
edt2.Height := Self.Height;
edt2.ReadOnly := true;
edt2.Text := Txt;
edt2.Color := Self.Color;
edt2.OnClick := CtlClick;
edt2.OnKeyPress := Self.OnKeyPress; btn := TSpeedButton.Create(Self);
btn.Parent := Self;
btn.Top := 0;
btn.Height := Self.Height;
btn.Width := 20;
btn.Left := Self.Width - 20;
btn.Caption := '>>';
btn.OnClick := btnClick;
btn.ShowHint := true;
btn.Hint := '选择记录...';
end;
3: begin
cbm := TComboBox.Create(Self);
cbm.Parent := Self;
cbm.Width := Self.Width;
cbm.Left := 0;
cbm.Top := 0;
cbm.Height := Self.Height;
cbm.Text := Txt;
cbm.Color := Self.Color;
cbm.OnClick := CtlClick;
cbm.OnKeyPress := Self.OnKeyPress; if strDataFromType='T' then
begin
Terms := TStringList.Create; SeparateTerms(Sql, '+', Terms);
for p := 0 to Terms.Count - 1 do
cbm.Items.Add(Terms.Strings[p]);
Terms.free;
end
else if strDataFromType='S' then
begin
tmpadoq := TADOQuery.Create(self);
tmpadoq.Connection := dbcn;
tmpadoq.Close;
tmpadoq.SQL.Clear;
tmpadoq.SQL.Add(Sql);
tmpadoq.Open;
for p := 0 to tmpadoq.RecordCount - 1 do begin
cbm.Items.Add(tmpadoq.Fields.Fields[0].AsString);
tmpadoq.Next;
end;
tmpadoq.Close;
tmpadoq.free;
end;
end;
end;
end.
unit EdtSelect;
interface
uses
Windows, Messages, SysUtils, Classes, Controls, StdCtrls, DB, ADODB, Buttons, Forms,
ComCtrls, ExtCtrls, Graphics;
type TKeyPressEvent = procedure(Sender: TObject; var Key: Char) of object;
TEdtSelect = class(TPanel)
private
{ Private declarations }
FOnKeyPress: TKeyPressEvent;
strDataFromType:String;
edt, edt2: Tedit;
cbm: TComboBox;
datetime, datetime2: TDateTimePicker;
memo: Tmemo;
btn: TSpeedButton;
protected
{ Protected declarations }
procedure Notification(AComponent: TComponent;
Operation: TOperation); override;
procedure SetFocus(); override;
procedure btnClick(Sender: TObject);
procedure KeyPress(Sender: TObject; var Key: Char) ; //; dynamic;
public
{ Public declarations }
procedure ReSetPosition();
procedure CreateCtl(iCtlType: Integer);
procedure SetText();
function GetText(): string;
procedure SetColor();
procedure SetCtlColor(cl: TColor);
procedure WinMsg(var Msg: TMsg; var Hnd: Boolean); //处理消息;
published
{ Published declarations }
property AdoCn: TADOConnection read dbcn write dbcn; //数据连接
property QuerySQL: string read Sql write Sql; //数据连接
property QueryKey: string read Key write Key; //数据连接
property iID: Integer read PKID write PKID; //数据关联
property Text: string read Txt write Txt; //数据关联
property CtlType: Integer read iType write iType; //数据关联
property DataFromType:String read strDataFromType write strDataFromType; //TCOMBOBOX数据来源 T : ' '+ ' ' ,S : TABLE FROM QuerySQL
property OnKeyPress: TKeyPressEvent read FOnKeyPress write FOnKeyPress;
end;
var
strOK: string;
ID: Integer; //数据关联
//iType: Integer;
procedure Register;
implementation
uses Select;
procedure Register;
begin
RegisterComponents('Standard', [TEdtSelect]);
end;procedure TEdtSelect.KeyPress(Sender: TObject; var Key: Char);
begin
end;procedure TEdtSelect.Notification(AComponent: TComponent;
Operation: TOperation);
begin
inherited;
case Operation of
opRemove:
if AComponent = dbcn then
dbcn := nil;
end;
end;
procedure TEdtSelect.CreateCtl(iCtlType: Integer);
var
tmpadoq: TADOQuery;
Terms: TStringList;
p: Integer;begin
iType := iCtlType;
case iType of
1: begin
edt := Tedit.Create(Self);
edt.Parent := Self;
edt.Width := Self.Width;
edt.Left := 0;
edt.Top := 0;
edt.Height := Self.Height;
edt.Text := Txt;
edt.Color := Self.Color;
edt.OnClick := CtlClick;
edt.OnKeyPress :=self.onKeyPress;
end; 2: begin
edt2 := Tedit.Create(Self);
edt2.Parent := Self;
edt2.Width := Self.Width - 20;
edt2.Left := 0;
edt2.Top := 0;
edt2.Height := Self.Height;
edt2.ReadOnly := true;
edt2.Text := Txt;
edt2.Color := Self.Color;
edt2.OnClick := CtlClick;
edt2.OnKeyPress := Self.OnKeyPress; btn := TSpeedButton.Create(Self);
btn.Parent := Self;
btn.Top := 0;
btn.Height := Self.Height;
btn.Width := 20;
btn.Left := Self.Width - 20;
btn.Caption := '>>';
btn.OnClick := btnClick;
btn.ShowHint := true;
btn.Hint := '选择记录...';
end;
3: begin
cbm := TComboBox.Create(Self);
cbm.Parent := Self;
cbm.Width := Self.Width;
cbm.Left := 0;
cbm.Top := 0;
cbm.Height := Self.Height;
cbm.Text := Txt;
cbm.Color := Self.Color;
cbm.OnClick := CtlClick;
cbm.OnKeyPress := Self.OnKeyPress; if strDataFromType='T' then
begin
Terms := TStringList.Create; SeparateTerms(Sql, '+', Terms);
for p := 0 to Terms.Count - 1 do
cbm.Items.Add(Terms.Strings[p]);
Terms.free;
end
else if strDataFromType='S' then
begin
tmpadoq := TADOQuery.Create(self);
tmpadoq.Connection := dbcn;
tmpadoq.Close;
tmpadoq.SQL.Clear;
tmpadoq.SQL.Add(Sql);
tmpadoq.Open;
for p := 0 to tmpadoq.RecordCount - 1 do begin
cbm.Items.Add(tmpadoq.Fields.Fields[0].AsString);
tmpadoq.Next;
end;
tmpadoq.Close;
tmpadoq.free;
end;
end;
end;
end.
解决方案 »
- hook鼠标滚轮的问题。
- 关于显示提示对话框的问题!
- 使用SocketConnection做的三层应用问题
- delphi的窗体显示的顺序是怎么样控制的?
- 如何再delphi中实现多个连接?并且分开传输数据!
- 組件高手進!我在組件中為其的某個事件指向一個方法,如ONCLICK:=MYDEFINE PROCEDURE,這個MYDEFINE PROCEDURE是寫在組件類中的,當我在FORM
- 打包问题:delphi6的installshield在哪里设置安装时显示中文提示?
- 开放远程控制代码
- 怎么样在 TRichText 控件中插入对象
- 又是一个关于SQL语句的问题?
- 请问TreeView节点带CheckBox的控件是什么?谢谢!
- 搜索引擎 谢谢大家关注
SetLength(myctl,1);
MyCtl[0] := TEdtSelect.Create(self);
MyCtl[0].Parent := Form1;
// MyCtl[0].AdoCn := AdoCn; MyCtl[0].Top := 0;
MyCtl[0].Left := 80;
MyCtl[0].Width := 180;
MyCtl[0].Height := 20;
MyCtl[0].CreateCtl(1);
MyCtl[0].ReSetPosition ;
MyCtl[0].Tag :=1;
MyCtl[0].OnKeyPress := cKeyPress;
EdtSelect1.CreateCtl(1);
EdtSelect1.ReSetPosition ;
procedure TForm1.cKeyPress(Sender: TObject; var Key: Char);
var
CType: string;
CIndex: Integer;
begin //不能正常运行
CIndex := -1;
CType := ''; MessageBox(form1.Handle, ' OK ', '提示', MB_OK) ;
CIndex := (Sender as TEdtSelect).Tag;
if Key = #13 then
MyCtl[CIndex - 1].Color := clWindow;end;
procedure TForm1.EdtSelect1KeyPress(Sender: TObject; var Key: Char);
begin
//正常运行
if Key = #13 then
MessageBox(form1.Handle, ' OK! ', '提示', MB_OK) ;
end;