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
214 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
Independent Software Vendors: 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Delphi Spellcheck in Webbrowser 1 76
how can i search if string exist in array ? 3 78
IP without any Dots 1 99
Remove all child-forms from a parent-panel 16 13
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
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…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

734 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