Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make a tree view re-think whether or not a horizontal scroll bar is needed?

Tags:

delphi

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

Add this to a VCL Forms app and run. The result looks like this:

enter image description here

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.

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.

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.

like image 282
David Heffernan Avatar asked Oct 02 '12 18:10

David Heffernan


1 Answers

Very simple, use TreeView1.Items.BeginUpdate/EndUpdate methods and the scrollbar will be calculated accordingly.

like this:

...
 TreeView1.Items.BeginUpdate;
 // change your nodes here
 TreeView1.Items.EndUpdate
like image 80
whosrdaddy Avatar answered Oct 24 '22 16:10

whosrdaddy