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
217 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses
Course of the Month6 days, 14 hours left to enroll

622 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