Einzelnen Beitrag anzeigen

TiGü

Registriert seit: 6. Apr 2011
Ort: Berlin
3.060 Beiträge
 
Delphi 10.4 Sydney
 
#23

AW: Virtual TreeView - Anhand des längsten Eintrages die Breite der Spalte anpassen

  Alt 14. Dez 2016, 11:57
@Aviator: Danke, mit den AutoFitColumns sieht das ganze schon viel besser aus.
Kannst du mal deine Lösung veröffentlichen?
Beachte die letzte Zeile im FormCreate!

Delphi-Quellcode:
unit VST.View;

interface

uses
  System.SysUtils, System.Classes,
  Vcl.Controls, Vcl.Forms, VirtualTrees;

type
  PMyNodeData = ^TMyNodeData;

  TMyNodeData = record
    Caption: string;
  end;

  TForm2 = class(TForm)
    procedure FormCreate(Sender: TObject);
    procedure VstFreeNode(Sender: TBaseVirtualTree; Node: PVirtualNode);
    procedure VstGetText(Sender: TBaseVirtualTree; Node: PVirtualNode; Column: TColumnIndex; TextType: TVSTTextType;
      var CellText: string);
  private
    VST: TVirtualStringTree;
  end;

var
  Form2: TForm2;

implementation

{$R *.dfm}

procedure TForm2.FormCreate(Sender: TObject);
var
  LPr, LP: PVirtualNode;
  LData: PMyNodeData;
begin
  VST := TVirtualStringTree.Create(Self);

  VST.name := 'VST';
  VST.Parent := Self;
  VST.Header.Options := VST.Header.Options + [hoVisible];
  VST.Align := alClient;
  VST.OnFreeNode := VstFreeNode;
  VST.OnGetText := VstGetText;

  with VST.Header.Columns.Add do
  begin
    Position := 0;
    Width := 10;
    Text := 'Spalte 1';
    Visible := True;
  end;

  with VST.Header.Columns.Add do
  begin
    Position := 1;
    Width := 10;
    Text := 'Spalte 2';
    Visible := True;
  end;

  VST.NodeDataSize := SizeOf(TMyNodeData);
  LPr := VST.AddChild(nil);
  LData := VST.GetNodeData(LPr);
  LData.Caption := 'Eintrag 1';

  LPr := VST.AddChild(nil);
  LData := VST.GetNodeData(LPr);
  LData.Caption := 'Eintrag 2';

  LPr := VST.AddChild(nil);
  LData := VST.GetNodeData(LPr);
  LData.Caption := 'Eintrag 3 1/4';

  LPr := VST.AddChild(nil);
  LData := VST.GetNodeData(LPr);
  LData.Caption := 'Hallo Welt';

  LPr := VST.AddChild(LPr);
  LData := VST.GetNodeData(LPr);
  LData.Caption := 'Hallo Welt';

  VST.FullExpand();

  VST.Header.AutoFitColumns; // <---- das ist der entscheide Punkt!
end;

procedure TForm2.VstFreeNode(Sender: TBaseVirtualTree; Node: PVirtualNode);
var
  LPData: PMyNodeData;
begin
  LPData := Sender.GetNodeData(Node);
  Finalize(LPData^);
end;

procedure TForm2.VstGetText(Sender: TBaseVirtualTree; Node: PVirtualNode; Column: TColumnIndex; TextType: TVSTTextType;
  var CellText: string);
var
  LData: PMyNodeData;
begin
  LData := VST.GetNodeData(Node);
  if TextType = ttNormal then
    CellText := LData.Caption;
end;

end.
  Mit Zitat antworten Zitat