如何使树形视图重新考虑是否需要水平滚动条? [英] How can I make a tree view re-think whether or not a horizontal scroll bar is needed?
问题描述
考虑以下非常简单的单位:
Consider the following very simple unit:
Unit1.pas
unit Unit1;
interface
uses
Windows, Classes, Controls, Forms, ComCtrls;
type
TForm1 = class(TForm)
TreeView1: TTreeView;
procedure FormCreate(Sender: TObject);
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
const
SLongString = 'blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah';
procedure TForm1.FormCreate(Sender: TObject);
var
Node: TTreeNode;
begin
TreeView1.Width := 200;
Node := TreeView1.Items.Add(nil, SLongString);
Node.Text := 'blah';
end;
end.
Unit1.dfm
object Form1: TForm1
ClientHeight = 137
ClientWidth = 216
OnCreate = FormCreate
PixelsPerInch = 96
TextHeight = 13
object TreeView1: TTreeView
Left = 8
Top = 8
Width = 198
Height = 121
end
end
将其添加到VCL Forms应用程序并运行。结果看起来像这样:
Add this to a VCL Forms app and run. The result looks like this:
我希望水平滚动条不显示。我该如何实现呢?
I would like for the horizontal scroll bar not to be showing. How can I achieve this?
现在我意识到我可以删除分配超长字符串的代码行。但是,出于我的问题,这是一个缩减程序。在实际应用中节点的文本正在更改,我希望滚动条显示是否需要它们,而不显示是否不需要它们。
Now I realise that I could remove the line of code that assigns the very long string. But this is a cut-down program for the purpose of my question. In the real app the text of the nodes is changing and I want the scroll bars to show if they are needed, and not show if they are not needed.
我知道关于 TVS_NOHSCROLL
样式,但我不能使用它。有时树形视图包含的文本宽于可用空间。有时不是。
I know about the TVS_NOHSCROLL
style but I can't use that. Sometimes the tree view contains text that is wider than the available space. And sometimes not.
我也想使用 TTreeView
,并且不想使用虚拟树视图。并不是说我对虚拟树视图有什么反对,只是我的应用程序当前正在使用 TTreeView
。
I also want to use TTreeView
and do not want to use virtual tree view. Not that I have anything against virtual tree view, just that my app is currently using TTreeView
.
推荐答案
非常简单,使用 TreeView1.Items.BeginUpdate / EndUpdate
方法,滚动条将相应地进行计算。
Very simple, use TreeView1.Items.BeginUpdate/EndUpdate
methods and the scrollbar will be calculated accordingly.
像这样:
...
TreeView1.Items.BeginUpdate;
// change your nodes here
TreeView1.Items.EndUpdate
这篇关于如何使树形视图重新考虑是否需要水平滚动条?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!