Solved

How do I select the parent node of a treeview based on the index of node from another treeview

Posted on 2006-10-28
6
205 Views
Last Modified: 2012-05-05
Hello

I am writing a chat program and I want to be able to log conversations
I have 2 treeviews and it looks like this

Ben: Can you show me how it is done?                       Joe:      - Sure, all i do is download the file and go to this website and wala
Ben: so how was your day anyway?                                                 but make sure you download the file
                                                                                                    before the weblink becomes broken
                                                                                             Yeah It was good thanks

and when i collaspe it it looks like this:

Ben: Can you show me how it is done                        Joe:      + Sure, all i do is download the file and go to this website and wala
Ben: so how was your day anyway?                                          Yeah It was good thanks

because i want the both treeviews conversations to line up with one another

any know how i can solve this problem and somehow make both of the treeviews have the blue setfocus highlight.
Any ideas???
0
Comment
Question by:Alpha_AI
  • 3
  • 2
6 Comments
 

Author Comment

by:Alpha_AI
ID: 17828118
You will have maximise your internet browser to read it properly
0
 
LVL 11

Expert Comment

by:calinutz
ID: 17829008
What does the internet browser have to do with your Treeviews?
Is your chat program written in delphi?
0
 
LVL 17

Expert Comment

by:TheRealLoki
ID: 17830199
not exactly sure what you want, but give this a go.
I am expanding both treeviews when 1 is expanded (and collapsing others) both treeviews should line up by doing this.


unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, ComCtrls;

type
  TForm1 = class(TForm)
    TreeView1: TTreeView;
    TreeView2: TTreeView;
    Button1: TButton;
    TreeView3: TTreeView;
    procedure FormCreate(Sender: TObject);
    procedure TreeView1Expanded(Sender: TObject; Node: TTreeNode);
    procedure TreeView2Expanded(Sender: TObject; Node: TTreeNode);
    procedure TreeView1Click(Sender: TObject);
    procedure TreeView2Click(Sender: TObject);
  private
    procedure LineUpTreeviews(tv1, tv2: TTreeView; SelectedNode: TTreeNode; expanding: boolean);
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.FormCreate(Sender: TObject);
begin
  Treeview1.HideSelection := False;
  Treeview1.RowSelect := False;
  Treeview1.OnExpanded := TreeView1Expanded;
  Treeview1.OnClick := TreeView1Click;
  Treeview2.HideSelection := False;
  Treeview2.RowSelect := False;
  Treeview2.OnExpanded := TreeView2Expanded;
  Treeview2.OnClick := TreeView2Click;
end;

procedure TForm1.TreeView1Expanded(Sender: TObject; Node: TTreeNode);
begin
  LineUpTreeviews(TreeView1, TreeView2, Node, true);
end;

procedure TForm1.TreeView2Expanded(Sender: TObject; Node: TTreeNode);
begin
  LineUpTreeviews(TreeView2, TreeView1, Node, true);
end;

procedure TForm1.TreeView1Click(Sender: TObject);
begin
  LineUpTreeviews(TreeView1, TreeView2, (Sender as TTreeView).Selected, false);
end;

procedure TForm1.TreeView2Click(Sender: TObject);
begin
  LineUpTreeviews(TreeView2, TreeView1, (Sender as TTreeView).Selected, false);
end;

procedure TForm1.LineUpTreeviews(tv1, tv2: TTreeView; SelectedNode: TTreeNode; expanding: boolean);
var
  i: integer;
  tempnode, matchingnode: TTreeNode;
  nodeindex: integer;
  oldtv1expandedevent, oldtv2expandedevent: TTVExpandedEvent;
begin
  if tv2.items.count = 0 then exit;
  if SelectedNode.Level <> 0 then exit; // only collapse other nodes if we are on the first level
  tv1.Items.BeginUpdate;
  tv2.Items.BeginUpdate;
  oldtv1expandedevent := tv1.OnExpanded;
  oldtv2expandedevent := tv2.OnExpanded;
  tv1.OnExpanded := nil;
  tv2.OnExpanded := nil;
  try

// collapse all other branches in 1st treeview, and find out which node index this is on the 1st level
// look for siblings above this node first
    nodeindex := 0;
    tempnode := SelectedNode.getPrevSibling;
    while tempnode <> nil do
    begin
      if expanding then tempnode.Collapse(true);
      inc(nodeindex);
      tempnode := tempnode.getPrevSibling;
    end;
// now collapse the nodes below this one
    tempnode := SelectedNode.getNextSibling;
    while tempnode <> nil do
    begin
      if expanding then tempnode.Collapse(true);
      tempnode := tempnode.getNextSibling;
    end;
// collapse all branches in 2nd treeview, and find the matching node on this level
// the first node in the treeview will be level 0, so we will start with that
    i := 0;
    tempnode := tv2.items[0];
    while tempnode <> nil do
    begin
      if i = nodeindex then
      begin
        matchingnode := tempnode;
        if expanding then tempnode.Expand(false);
      end
      else
      begin
        if expanding then tempnode.Collapse(true);
      end;
      inc(i);
      tempnode := tempnode.getNextSibling;
    end;
    tv1.Selected := SelectedNode;
    matchingnode.Selected := true; // highlight it

  finally
    tv1.Items.EndUpdate;
    tv2.Items.EndUpdate;
    tv1.OnExpanded := oldtv1expandedevent;
    tv2.OnExpanded := oldtv2expandedevent;
  end;
end;

end.
0
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 

Author Comment

by:Alpha_AI
ID: 17831264
The question I posted can only be viewed properly when your browser enlarged. Maximise and Unmaximise and you'll see what i mean.

Therealloki, i will give that code a try.
0
 

Author Comment

by:Alpha_AI
ID: 17849127
Yes, Thereallloki so far so good
but I was also expecting your code to allow me to do the following:

The first node in the treeview1 is a parent and has no child
The first node in the treeview2 is a parent and has a child

so I was hoping that when you click on the treeview1 first node parent, it would expand the
firstnode in treeview2

and same vice versa, if I click on treeview2 first node parent, it would expand showing a child node in treeview1 if one existed.

Any clues?

Ben
0
 
LVL 17

Accepted Solution

by:
TheRealLoki earned 250 total points
ID: 17852001
if i understand you correctly, then simply change this part
    tv1.Selected := SelectedNode;
    matchingnode.Selected := true; // highlight it
to
    tv1.Selected := SelectedNode;
    matchingnode.Selected := true; // highlight it
    matchingnode.Expand(true); // expand
0

Featured Post

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
build html 5 client that connect to indy Tidtcpserver 3 402
delphi exception 7 64
Simple Delphi Question 9 90
RESTRequest Parameter 4 32
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question