隐藏不可见列表框项目后面的空白区域 [英] Hide empty space behind invisible list box items

查看:96
本文介绍了隐藏不可见列表框项目后面的空白区域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以看到其他人遇到了这个问题,并且建议的解决方案对我不起作用。



我有一个列表框,其中包含在设计时和运行时填充的许多静态项目,一些列表框项目的可见性已更改。但是,那些不可见的项目在该项目所属的列表框中留有空白。建议将不可见项的高度设置为0,这显然对人有用,但对我却不起作用。



这是我写的一个通用函数

 程序FixHidden(AListBox:TListBox); 
var
X:整数;
I:TListBoxItem;
从X开头的
:= 0到AListBox.Count-1从
开始I:= AListBox.ItemByIndex(X);
如果I.Visible,则
I.Height:= AListBox.ItemHeight
否则
I.Height:= 0;
结尾;
结尾;

单步执行代码时,我可以确认在 I.Height之后: = 0; Height 实际上为0。但是,问题仍然存在,并且列表框中仍然有一个空白,就像



如何使不可见的项目真正隐藏在列表框中,包括其后的空白区域,而又不删除这些项目?我的应用程序有很多具有相同问题的不同列表框控件,因此是一个理想的全局解决方案。



注意:这是使用Windows 32作为平台。

>

编辑



我又做了几次试验,发现了一些东西。在设计时,列表框项目 Height 属性默认为父列表框的 ItemHeight 属性。如果我在设计时更改了特定项目的 Height ,它会立即恢复到以前的状态。因此问题就变成了……为什么每次更改Firemonkey都会还原 TListBoxItem.Height

解决方案

由于汤姆在评论中提供了一些故障排除帮助,因此我发现了问题所在以及解决方法。



问题的根源是我的 TListBox 控件被赋予了<$ c $的自定义值c> ItemHeight ,尽管默认情况下它们的值为 0 。当 ItemHeight 0 时,您可以设置单个项目的高度。但是,当 ItemHeight 是其他任何东西时,列表框控件将接管并强制将所有项目设置为该高度,而不管您为每个项目分配了什么。



因此解决此问题的步骤是:


  1. 设置 TListBox.ItemHeight 属性返回到 0

  2. 设置 TListBoxItem.Height 属性设置为期望的高度

  3. 修改我的过程以将其设置为期望的高度,而不是 TListBox.ItemHeight

现在过程如下:

 程序FixHidden(AListBox:TListBox; AHeight:Extended = 32.0); 
var
X:整数;
I:TListBoxItem;
从X开头的
:= 0到AListBox.Count-1从
开始I:= AListBox.ItemByIndex(X);
如果I.Visible,则
I.Height:= AHeight
否则
I.Height:= 0;
结尾;
结尾;

这对于原始的基础问题仍然是相当混乱的解决方案,但确实可以解决问题。 / p>

如果出于任何原因您希望具有不同高度的列表项,则可以通过将所需高度存储在 Tag 每个列表框项目的属性(尽管 Tag NativeInt 高度扩展的,因此准确性会丢失)...

 过程FixHidden(AListBox:TListBox); 
var
X:整数;
I:TListBoxItem;
从X开头的
:= 0到AListBox.Count-1从
开始I:= AListBox.ItemByIndex(X);
如果I.Visible,则
I.Height:= I.Tag
否则
I.Height:= 0;
结尾;
结尾;


I can see other people have had this issue, and the proposed solution is not working for me.

I have a list box with a number of static items populated in design-time, and in run-time, some list box items have their visibility changed. However, those items which are not visible leave an empty space in the list box where that item belongs. It's proposed to set the invisible item height to 0, and it's apparently worked for people, but it's not working for me.

This is a common function I wrote to accommodate for this:

procedure FixHidden(AListBox: TListBox);
var
  X: Integer;
  I: TListBoxItem;
begin
  for X := 0 to AListBox.Count-1 do begin
    I:= AListBox.ItemByIndex(X);
    if I.Visible then
      I.Height:= AListBox.ItemHeight
    else
      I.Height:= 0;
  end;
end;

While stepping through the code, I can confirm that after I.Height:= 0; the Height is in fact 0. However, the problem still persists, and there's still an empty void in the list box, as if the list box control ignores what height I set the item.

How can I make invisible items truly hidden from the list box including this empty space behind it, without deleting these items? My app has many different list box controls with the same issue, so a global solution would be ideal.

NOTE: This is using Windows 32 as a platform.

EDIT

I did a few more trials, and discovered something. In design-time, the list box items Height property defaults to the parent list box's ItemHeight property. If I change the Height of a specific item in design-time, it immediately reverts back to what it was before. So the question then becomes... why is Firemonkey reverting TListBoxItem.Height every time I change it?

解决方案

Thanks to some troubleshooting help from Tom in the comments, I've discovered what was going wrong and how to get around it.

The root of the issue was the fact that my TListBox controls were given a custom value for ItemHeight although by default they have 0. When ItemHeight is 0, you're allowed to set the height of individual items. However when ItemHeight is anything else, the list box control takes over and forcefully sets all items to that height, regardless of what you assign to each item.

So the steps to fix this were:

  1. Set the TListBox.ItemHeight property back to 0
  2. Set the TListBoxItem.Height property of each item to desired height
  3. Modify my procedure to set to desired height rather than TListBox.ItemHeight

And the procedure now looks like this:

procedure FixHidden(AListBox: TListBox; AHeight: Extended = 32.0);
var
  X: Integer;
  I: TListBoxItem;
begin
  for X := 0 to AListBox.Count-1 do begin
    I:= AListBox.ItemByIndex(X);
    if I.Visible then
      I.Height:= AHeight
    else
      I.Height:= 0;
  end;
end;

This is still a rather messy fix to the original underlying issue, but it does solve the problem.

If for any reason you want to be able to have list items of varying height, you can do so by storing the desired height in the Tag property of each list box item (although Tag is a NativeInt and Height is Extended so accuracy would be lost) ...

procedure FixHidden(AListBox: TListBox);
var
  X: Integer;
  I: TListBoxItem;
begin
  for X := 0 to AListBox.Count-1 do begin
    I:= AListBox.ItemByIndex(X);
    if I.Visible then
      I.Height:= I.Tag
    else
      I.Height:= 0;
  end;
end;

这篇关于隐藏不可见列表框项目后面的空白区域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆