附所有代码,想做一个下拉弹出Listview的combobox,却出现assess violation的错误 ( 积分: 100 )

  • 主题发起人 主题发起人 conis
  • 开始时间 开始时间
C

conis

Unregistered / Unconfirmed
GUEST, unregistred user!
unit lvCombobox;

interface

uses
SysUtils, Classes, Controls, StdCtrls, Forms, Windows, ComCtrls,Dialogs ;

type
{
TSelectFieldEditer= class(TStringProperty)
function GetAttributes: TPropertyAttributes; override;
procedure GetValues(Proc: TGetStrProc); override;
end;
}


TlvCombobox = class(TCustomComboBox)
private
FExListView: TListView;
protected
procedure DropDown; override;
procedure Notification(AComponent: TComponent; Operation: TOperation); override;
procedure SetParent(AParent: TWinControl); override;
procedure Loaded; override;
public
constructor Create(AOwner : TComponent); override;
procedure SetBounds(ALeft: Integer; ATop: Integer; AWidth: Integer; AHeight: Integer); override;
published
property Color;
property Constraints;
property Ctl3D;
property BevelEdges;
property BevelInner;
property BevelKind default bkNone;
property BevelOuter;
property Font;
property Text;
property Visible;
property OnChange;
property OnClick;
property OnCloseUp;
property OnContextPopup;
property OnDblClick;
property OnDragDrop;
property OnDragOver;
property OnDrawItem;
property OnDropDown;
property OnEndDock;
property OnEndDrag;
property OnEnter;
property OnExit;
property OnKeyDown;
property OnKeyPress;
property OnKeyUp;
property OnMeasureItem;
property OnSelect;
property OnStartDock;
property OnStartDrag;
end;

procedure Register;

implementation

procedure Register;
begin
RegisterComponents('kitop', [TlvCombobox]);
//RegisterPropertyEditor(TypeInfo(string), TlvCombobox, 'SelectField', TSelectFieldEditer);
end;

{ TlvCombobox }

constructor TlvCombobox.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FExListView := TListView.Create(self);
FExListView.FreeNotification(self);
FExListView.Width := Width;
end;

procedure TlvCombobox.DropDown;
begin
inherited;
DroppedDown := False;
FExListView.Visible := True;
end;

procedure TlvCombobox.Loaded;
begin
inherited;
end;

procedure TlvCombobox.Notification(AComponent: TComponent;
Operation: TOperation);
begin
inherited;
if (Operation = opRemove) and (AComponent = FExListView) then
FExListView := nil;
end;

procedure TlvCombobox.SetBounds(ALeft, ATop, AWidth, AHeight: Integer);
begin
inherited;
if FExListView <> nil then with FExListView do
begin
Top := ATop + AHeight;
Left := ALeft;
Width := AWidth;
Height := 0;
end;
end;

procedure TlvCombobox.SetParent(AParent: TWinControl);
begin
inherited;
FExListView.Parent := AParent;
end;

end.
 
unit lvCombobox;

interface

uses
SysUtils, Classes, Controls, StdCtrls, Forms, Windows, ComCtrls,Dialogs ;

type
{
TSelectFieldEditer= class(TStringProperty)
function GetAttributes: TPropertyAttributes; override;
procedure GetValues(Proc: TGetStrProc); override;
end;
}


TlvCombobox = class(TCustomComboBox)
private
FExListView: TListView;
protected
procedure DropDown; override;
procedure Notification(AComponent: TComponent; Operation: TOperation); override;
procedure SetParent(AParent: TWinControl); override;
procedure Loaded; override;
public
constructor Create(AOwner : TComponent); override;
procedure SetBounds(ALeft: Integer; ATop: Integer; AWidth: Integer; AHeight: Integer); override;
published
property Color;
property Constraints;
property Ctl3D;
property BevelEdges;
property BevelInner;
property BevelKind default bkNone;
property BevelOuter;
property Font;
property Text;
property Visible;
property OnChange;
property OnClick;
property OnCloseUp;
property OnContextPopup;
property OnDblClick;
property OnDragDrop;
property OnDragOver;
property OnDrawItem;
property OnDropDown;
property OnEndDock;
property OnEndDrag;
property OnEnter;
property OnExit;
property OnKeyDown;
property OnKeyPress;
property OnKeyUp;
property OnMeasureItem;
property OnSelect;
property OnStartDock;
property OnStartDrag;
end;

procedure Register;

implementation

procedure Register;
begin
RegisterComponents('kitop', [TlvCombobox]);
//RegisterPropertyEditor(TypeInfo(string), TlvCombobox, 'SelectField', TSelectFieldEditer);
end;

{ TlvCombobox }

constructor TlvCombobox.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FExListView := TListView.Create(self);
FExListView.FreeNotification(self);
FExListView.Width := Width;
end;

procedure TlvCombobox.DropDown;
begin
inherited;
DroppedDown := False;
FExListView.Visible := True;
end;

procedure TlvCombobox.Loaded;
begin
inherited;
end;

procedure TlvCombobox.Notification(AComponent: TComponent;
Operation: TOperation);
begin
inherited;
if (Operation = opRemove) and (AComponent = FExListView) then
FExListView := nil;
end;

procedure TlvCombobox.SetBounds(ALeft, ATop, AWidth, AHeight: Integer);
begin
inherited;
if FExListView <> nil then with FExListView do
begin
Top := ATop + AHeight;
Left := ALeft;
Width := AWidth;
Height := 0;
end;
end;

procedure TlvCombobox.SetParent(AParent: TWinControl);
begin
inherited;
FExListView.Parent := AParent;
end;

end.
 
经查:是这段代码出错
procedure TlvCombobox.Notification(AComponent: TComponent;
Operation: TOperation);
begin
inherited;
if (Operation = opRemove) and (AComponent = FExListView) then
FExListView := nil;
end;

当运行时(F9),FExListView 已经为nil ,所以导制出错,但不明白为什么运行时Operation的值为什么是opRemove呢?有什么好办法可以解决?
 
在Create的时候FExListView.FreeNotification(self);去掉看看
 
To:chenybin
去掉是没有问题
但在设计中看不到结果
 
在Create(AOwner:TComponent)中:ControlStyle:=ControlStyle+[csInvisibleDesign];

http://www.delphibbs.com/delphibbs/dispq.asp?lid=2433003
 
还是不行啊
运行后退出还是出错
狂晕
 
后退
顶部