Главная страница
Top.Mail.Ru    Яндекс.Метрика
Текущий архив: 2005.11.27;
Скачать: CL | DM;

Вниз

Свойство TCollection   Найти похожие ветки 

 
Бывший студент ©   (2005-04-18 02:27) [0]

Публикую свойство типа потомка TCollection. В инспекторе объектов появляется, но при обращении к нему ничего не происходит.


 
Юрий Зотов ©   (2005-04-18 06:11) [1]

> Бывший студент ©   (18.04.05 02:27)

Значит, где-то ошибка.


 
Deka ©   (2005-04-18 11:19) [2]

Не уверен, н омне кажется надо копать в направлении редактора этого самого свойства. Для каждого свойства отображаемого в инспекторе объектов должен быть редактор. Они есть для свойств стандартных типов, а для таких свойст как "свойство типа потомка TCollection" стандартного редактора нет, потому и ничего не происходит. Хотя описание ситуации не полное и происхождение чего предполагается сказать трудно...


 
Юрий Зотов ©   (2005-04-18 11:57) [3]

> Deka ©   (18.04.05 11:19) [2]

> а для таких свойст как "свойство типа потомка TCollection"
> стандартного редактора нет

Есть. И если все сделано правильно, то к любому потомку TCollection он подцепится автоматически.


 
Бывший студент ©   (2005-04-18 12:47) [4]

Вот код:

type
 TTest1Grid = class;
 
 TColStyle = class(TCollectionItem)
 private
   FFont: TFont;
   procedure SetFont(Value: TFont);
 protected
 public
   constructor Create(Collection: TCollection); override;
   destructor Destroy; override;
 published
   property TestFont: TFont read FFont write SetFont;
end;

 TColStyles = class(TCollection)
 private
   FGrid: TTest1Grid;
   function GetItem(Index: Integer): TColStyle;
   procedure SetItem(Index: Integer; Value: TColStyle);
 protected
 public
   constructor Create(test1grid: TTest1Grid);
   function Add: TColStyle;
   function IndexOf(const DisplayName: string): Integer;
   property Items[Index: Integer]: TColStyle read GetItem write SetItem; default;
end;

TTest1Grid = class(TStringGrid)
private
FCStyle: TColStyles;
procedure SetColStyles(Value: TColStyles);
.
published
property ColStyle: TColStyles read FCStyle write SetColStyles;
end;

implementation
.
.
constructor TColStyle.Create(Collection: TCollection);
begin
 FFont := TFont.Create;
 inherited Create(Collection);
end;

destructor TColStyle.Destroy;
begin
 FFont.Free;
 inherited Destroy;
end;

procedure TColStyle.SetFont(Value: TFont);
begin
FFont.Assign(Value);
end;

constructor TColStyles.Create(Test1Grid: TTest1Grid);
begin
 inherited Create(TColStyle);
 FGrid:= Test1Grid;
end;

function TColStyles.Add: TColStyle;
begin
 Result := TColStyle(inherited Add);
end;

function TColStyles.GetItem(Index: Integer): TColStyle;
begin
 Result := TColStyle(inherited GetItem(Index));
end;

procedure TColStyles.SetItem(Index: Integer; Value: TColStyle);
begin
 inherited SetItem(Index, Value);
end;

function TColStyles.IndexOf(const DisplayName: string): Integer;
begin
 for Result := 0 to Count - 1 do
   if AnsiCompareText(TColStyle(Items[Result]).DisplayName, DisplayName) = 0 then Exit;
 Result := -1;
end;

procedure TTest1Grid.SetColStyles(Value: TColStyles);
begin
 FCStyle.Assign(Value);
end;

end.


 
Юрий Зотов ©   (2005-04-18 12:59) [5]

Либо перекройте в коллекции метод GetOwner и возвращайте FGrid, либо наследуйте коллекцию от TOwnedCollection, где это уже сделано (кстати, тогда и поле FGrid не понадобится).


 
Бывший студент ©   (2005-04-18 13:18) [6]

Перекрыл, результат прежний.
TColStyles = class(TCollection)
private
  FGrid: TTest1Grid;
  function GetItem(Index: Integer): TColStyle;
  procedure SetItem(Index: Integer; Value: TColStyle);
protected
function GetOwner: TPersistent; override;
public
  constructor Create(test1grid: TTest1Grid);
  function Add: TColStyle;
  function IndexOf(const DisplayName: string): Integer;
  property Items[Index: Integer]: TColStyle read GetItem write SetItem; default;
end;
.
.
function TColStyles.GetOwner: TPersistent;
begin
 Result := FGrid;
end;


 
Юрий Зотов ©   (2005-04-18 14:22) [7]

Чудес не бывает - обязано работать. Если, конечно, коллекция вообще создается в конструкторе (и, соответственно уничтожается в деструкторе).

Вот минимальный код, который должен быть написан (я наследовался от TOwnedCollection, что и Вам советую - это не обязательно, но так немного проще).


type
 TTestGrid = class;

 TColStyle = class(TCollectionItem)
 private
   FFont: TFont;
   procedure SetFont(Value: TFont);
 protected
 public
   constructor Create(Collection: TCollection); override;
   destructor Destroy; override;
 published
   property Font: TFont read FFont write SetFont;
 end;

 TColStyles = class(TOwnedCollection)
 private
   function GetGrid: TTestGrid;
 public
   property Grid: TTestGrid read GetGrid;
 end;

 TTestGrid = class(TStringGrid)
 private
   FColStyles: TColStyles;
   procedure SetColStyles(Value: TColStyles);
 public
   constructor Create(AOwner: TComponent); override;
   destructor Destroy; override;
 published
   property ColStyles: TColStyles read FColStyles write SetColStyles;
 end;

{ TColStyle }

constructor TColStyle.Create(Collection: TCollection);
begin
 inherited;
 FFont := TFont.Create
end;

destructor TColStyle.Destroy;
begin
 FFont.Free;
 inherited
end;

procedure TColStyle.SetFont(Value: TFont);
begin
 FFont.Assign(Value)
end;

{ TColStyles }

function TColStyles.GetGrid: TTestGrid;
begin
 Result := GetOwner as TTestGrid
end;

{ TTestGrid }

constructor TTestGrid.Create(AOwner: TComponent);
begin
 inherited;
 FColStyles := TColStyles.Create(Self, TColStyle)
end;

destructor TTestGrid.Destroy;
begin
 FColStyles.Free;
 inherited
end;

procedure TTestGrid.SetColStyles(Value: TColStyles);
begin
 FColStyles.Assign(Value)
end;


Почему сделано так, а не иначе - можно посмотреть здесь:
http://www.delphikingdom.com/asp/viewitem.asp?catalogid=215


 
Бывший студент ©   (2005-04-18 15:23) [8]

Спасибо, теперь работает.


 
Бывший студент ©   (2005-04-18 21:17) [9]

А как завязать ColCount и TCollection.Count? Например, можно через конструктор - но это однократно.



Страницы: 1 вся ветка

Текущий архив: 2005.11.27;
Скачать: CL | DM;

Наверх




Память: 0.49 MB
Время: 0.056 c
4-1126778916
XGarik
2005-09-15 14:08
2005.11.27
Функция CERapiInit


2-1131540149
max999
2005-11-09 15:42
2005.11.27
Вопрос на тему "иконка в трее" :)


2-1131605582
начиннающий
2005-11-10 09:53
2005.11.27
DynamicSkinForm


5-1113908678
prodigydancer
2005-04-19 15:04
2005.11.27
Компоненты


2-1131438558
DevMaster
2005-11-08 11:29
2005.11.27
TadvStringGrid (TMS) - проблемма с памятью