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
201 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
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…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

760 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