Autor Beitrag
JoBoCAD
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 24



BeitragVerfasst: Mi 05.12.12 11:01 
Hallo Zusammen,
wie schaffe ich es immer den obersten Eintrag des TreeViews als Text zu ermitteln - egal an welcehr Stelle ich im TreeView stehe.
Beispiel:
A--
---Test1
B--
---Test2
C--
---Test3

Selektiert ist zur Laufzeit Test2 - - gewünschte Anzeige B
Selektiert Test3 --> Anzeige C

Vielen Dank für Eure Mithilfe

Gruß
Joachim
bummi
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 1248
Erhaltene Danke: 187

XP - Server 2008R2
D2 - Delphi XE
BeitragVerfasst: Mi 05.12.12 11:09 
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
Function GetBaseNode(TN:TTreenode):TTreenode;
begin
  If Assigned(TN.Parent) then Result := GetBaseNode(TN.Parent)
  else Result := TN;

end;

procedure TForm3.Button1Click(Sender: TObject);
begin
  if assigned(Treeview1.Selected) then
     Showmessage(GetBaseNode(Treeview1.Selected).Text);
end;

_________________
Das Problem liegt üblicherweise zwischen den Ohren H₂♂
DRY DRY KISS

Für diesen Beitrag haben gedankt: JoBoCAD
Mathematiker
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 2622
Erhaltene Danke: 1448

Win 7, 8.1, 10
Delphi 5, 7, 10.1
BeitragVerfasst: Mi 05.12.12 11:11 
Hallo,
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
function GetDir(Node: TTreeNode): string;
var
  s: string;
begin
   s := Node.Text+ #13#10;
   while Node.Parent <> nil do
   begin
     s    := Node.Parent.Text + #13#10 + s;
     Node := Node.Parent;
   end;
   Result := s;
end;

liefert Dir als String alle Einträge vom obersten Eintrag bis zum gewählten. Am Ende müsstest Du nur den 1.Teilstring bis #13#10 abtrennen.

Beste Grüße
Mathematiker

Nachtrag: Schade. Ich war zu langsam und Bummis Lösung ist besser.

_________________
Töten im Krieg ist nach meiner Auffassung um nichts besser als gewöhnlicher Mord. Albert Einstein
WasWeißDennIch
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 653
Erhaltene Danke: 160



BeitragVerfasst: Mi 05.12.12 11:25 
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
Function GetBaseNode(TN:TTreenode):TTreenode;
begin
  Result := TN;
  if Assigned(Result) then
    if Assigned(Result.Parent) then 
      Result := GetBaseNode(Result.Parent);
end;

So knallt es nicht, wenn TN nil ist.

Für diesen Beitrag haben gedankt: bummi
JoBoCAD Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 24



BeitragVerfasst: Mi 05.12.12 11:52 
Hallo nochmals,
klasse - funktioniert so wie ich es mir wünschte.
Danke Euch
Joachim