【发布时间】:2014-11-18 13:51:22
【问题描述】:
所以我有一个带有列的字符串网格。但是每一列都可以被删除,如果一列被删除,索引会重新排列。我可以在删除之前使用索引的值,但是在删除几列时,索引根本不一样。例如,如果我删除索引 1 和 2 处的列,则索引 3 处的列将获得一个新索引,即索引 1。
所以我想做的是向我的列添加新方法,我将在其中设置并获取真正的索引,因为它从未被删除。我找到了一个关于如何向 delphi 类添加新方法的教程,它的外观是这样的:
unit columnInterceptor
interface
uses stdCtrls, sysUtils, Classes, dialogs, grids;
type
TStrings = class(Classes.TStrings)
private
public
procedure hello;
end;
implementation
procedure TStrings.Hello;
begin
ShowMessage('hello');
end;
end.
如果我使用它在 StringGrid 上添加方法,这将有效。但我想在 stringGrid 的列上使用它。我已经看到一些方法来自类 TStrings 或 TObject,我都尝试了它们,但是程序 hello 没有显示。
编辑
使用类助手我设法访问了我自己的方法,并且在更改后它是这样的:
unit columnInterceptor
interface
uses stdCtrls, sysUtils, Classes, dialogs, grids;
type
colIntercept= class helper for TStrings
public
procedure setValue(val: integer);
function getValue: integer;
end;
implementation
var
value : integer;
procedure colIntercept.setValue(val: integer);
begin
value := integer;;
end;
function colIntercept.getValue: integer;
begin
Result := value;
end;
end.
事情是,如果我添加一个私有声明,我就不能再使用我在公共声明中声明的方法了。当我设置一个值时,所有列实际上都是相同的。这就是我使用这个类的方式:
//somewhere in the unit where create all the columns
grid.Cols[aCol].setValue(aCol);
//somewhere in the unit
grid.Cols[aCol].getValue
然后任何列的所有值总是相同的。当我设置我的值时,它们每次都不同。但是得到它们,总是返回我使用 setValue 方法插入的最后一个值。
【问题讨论】:
-
我的单元现在被定义为 TStrings 的类助手。为了访问它,我正在使用 grid.Cols[0].hello 这不起作用,但这是我想要使用它的方式;
-
与其删除列,不如直接隐藏它们?然后索引不会改变。查看
ColWidths[]属性。 -
哦好主意哈哈谢谢:P
-
您是否会接受一个答案,该答案表明可以使用 Cols 存储索引,但由于“内容”正在通过 cols 移动而无济于事?
标签: delphi components extend