问题描述
我的表单上有一个Horizontal Scroll Box项.运行该程序后,我将得到一个json字符串,其中包含必须在Horizontal Scroll Box中的项列表,而且我必须动态添加它们.
i have a Horizontal Scroll Box item on my form.after run the program i will get a json string that include the list of items that must be in Horizontal Scroll Box .and i must add them dynamically.
例如我有这个:在运行程序后?区域我必须有新图像.
for example i have this :after run the program in the ? area i must a a new image.
我发现了这个功能:HorzScrollBox1.AddObject();
,但为此需要一个参数
i found the function :HorzScrollBox1.AddObject();
but a argument is required for this
我有两个问题:
1)我如何向其中添加新对象?
1)how can i add the new object to this?
2)我可以克隆现有图像并将其添加到列表的末尾吗?
2)can i clone an existing image and add it at the end of the list?
推荐答案
添加对象:有两种方法-设置子级的Parent
属性或执行父级的AddObject
方法. Parent
属性设置器有一些检查,然后调用AddObject
.根据控件类,可以将子对象添加到控件的Controls
集合或其Content
私有字段中.并非所有的类都提供对该字段的访问权限(在某些情况下,您可以使用替代方法,例如这个问题). HorzScrollBox在公共部分具有此字段.
Add object: there are two ways - set Parent
property of children or execute AddObject
method of parent. Parent
property setter has some checks, and call AddObject
.Depending on control class, child object can be added to Controls
collection of the control or to its Content
private field. Not all classes provide access to this field (in some cases you can use workaround, like in this question). HorzScrollBox has this field in public section.
因此,如果要克隆现有图像,则必须:
So, if you want to clone existing image, you must:
-
从
HorzScrollBox
的Content
创建新图像并设置其属性
Create new image and set it properties
将新图像放入HorzScrollBox.
Put new image to HorzScrollBox.
例如:
procedure TForm2.btnAddImgClick(Sender: TObject);
function FindLastImg: TImage;
var
i: Integer;
tmpImage: TImage;
begin
Result:=nil;
// search scroll box content for "most right" image
for i := 0 to HorzScrollBox1.Content.ControlsCount-1 do
if HorzScrollBox1.Content.Controls[i] is TImage then
begin
tmpImage:=TImage(HorzScrollBox1.Content.Controls[i]);
if not Assigned(Result) or (Result.BoundsRect.Right < tmpImage.BoundsRect.Right) then
Result:=tmpImage;
end;
end;
function CloneImage(SourceImage: TImage): TImage;
var
NewRect: TRectF;
begin
Result:=TImage.Create(SourceImage.Owner);
Result.Parent:=SourceImage.Parent;
// Copy needed properties. Assign not work for TImage...
Result.Align:=SourceImage.Align;
Result.Opacity:=SourceImage.Opacity;
Result.MultiResBitmap.Assign(SourceImage.MultiResBitmap);
// move new image
NewRect:= SourceImage.BoundsRect;
NewRect.Offset(NewRect.Width, 0); // move rect to right.
Result.BoundsRect:=NewRect;
end;
begin
CloneImage(FindLastImg);
end;
这篇关于firemonkey即时将项目添加到HorzScrollBox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!