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
202 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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 Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

947 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now