Recent

Author Topic: combobox inside a treeview  (Read 419 times)

Paolo

  • Hero Member
  • *****
  • Posts: 537
combobox inside a treeview
« on: October 13, 2024, 07:11:26 pm »
Dear all,

How can I put a combobox in one of treeView node ? is it possible ? are the some demo around ?

(win64, LAz 3.6 fpc 3.2.2)

thank you.

jamie

  • Hero Member
  • *****
  • Posts: 6695
Re: combobox inside a treeview
« Reply #1 on: October 14, 2024, 01:02:48 am »
yes you can and I just spent some time in doing that for you however, I found a bug in the TTreeView code.

I know it won't ever get fixed so there is basically a useless event in there.

The "OnCustomDrawItem" which allows you to create a node there and set text and data items, however. the code follows through
correctly up to the point there it decides to overwrite the Text and Data properties, there for making all that work at that customDrawItem event just about useless. I can't think of anything else there you can do in that event that you can't elsewhere ?

Code: Pascal  [Select][+][-]
  1.   Result := fNewNodeToBeAdded; // Used by AddNode to pass an existing node.
  2.   if Result = Nil then
  3.     Result := Owner.CreateNode; //This is where a new node is created normally or from the OnCustomCreateitem
  4.   fNewNodeToBeAdded := nil;
  5.   ok:=false;
  6.   try
  7.     Result.Data := Data;  //<< as you can see, Data gets overwritten
  8.     Result.Text := S;  //<< Same here.
  9.     // move node in tree (tree of TTreeNode)
  10.     Result.InternalMove(Node,AddMode);          
  11.  
  12.  

You can look at this in the TreeView.inc file starting at ~ line #2440-2470

So what you can do is use the OnAddition and Set the DATA pointer there to a new combobox

using the OnCustonDrawItem, you can set the X,Y plots of the box and have it view when you select the node, for example.

make sure you use the TreeView as the owner so it will remove it self properly and using the OnDelete event you can free this object etc.


 I hope someone with more power than me looks at this bug I found, at least correct the DOCS to indicate an overwrite of the Text and DATA property.

The only true wisdom is knowing you know nothing

jamie

  • Hero Member
  • *****
  • Posts: 6695
Re: combobox inside a treeview
« Reply #2 on: October 14, 2024, 01:45:21 am »
Here is a starting point.
Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Windows, Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls,
  9.   ExtCtrls, Arrow, ComCtrls;
  10.  
  11. type
  12.  
  13.   { TForm1 }
  14.  
  15.   TForm1 = class(TForm)
  16.     Button1: TButton;
  17.     TreeView1: TTreeView;
  18.     procedure Button1Click(Sender: TObject);
  19.     procedure TreeView1Addition(Sender: TObject; Node: TTreeNode);
  20.     procedure TreeView1CustomDrawItem(Sender: TCustomTreeView; Node: TTreeNode;
  21.       State: TCustomDrawState; var DefaultDraw: Boolean);
  22.   private
  23.  
  24.   public
  25.  
  26.   end;
  27.  
  28. var
  29.   Form1: TForm1;
  30.  
  31. implementation
  32.  
  33. {$R *.lfm}
  34.  
  35. { TForm1 }
  36.  
  37. procedure TForm1.Button1Click(Sender: TObject);
  38. begin
  39.   TreeView1.Items.AddFirst(nil,'');
  40. end;
  41.  
  42. procedure TForm1.TreeView1Addition(Sender: TObject; Node: TTreeNode);
  43. begin
  44.  Node.Text := 'A-Commie-Box';
  45.  Node.Data := TComboBox.Create(TTreeView(Sender));
  46.  TComboBox(Node.data).Visible :=False;
  47.  TComboBox(Node.data).Enabled := True;
  48.  TComboBox(Node.Data).height := Node.DisplayRect(true).height;
  49.  TComboBox(Node.data).Parent := TTreeView(Sender);
  50.  TComboBox(Node.Data).Items.CommaText := 'one,two,three,etc';
  51. end;
  52.  
  53. procedure TForm1.TreeView1CustomDrawItem(Sender: TCustomTreeView;
  54.   Node: TTreeNode; State: TCustomDrawState; var DefaultDraw: Boolean);
  55. begin
  56.   if node.Data <> nil then with TCombobox(Node.Data) do
  57.     begin
  58.        Left := Node.DisplayTextLeft;
  59.        Top := Node.Top;
  60.        Visible := Node.Selected;
  61.     end;
  62. end;
  63.  
  64. end.
  65.  
  66.  
  67.  

Set the options in the TreeView for Read only for now.
You can also remove the border on the combobox too if you wish.

BUt this will give you an idea.
The only true wisdom is knowing you know nothing

Paolo

  • Hero Member
  • *****
  • Posts: 537
Re: combobox inside a treeview
« Reply #3 on: October 14, 2024, 08:53:32 am »
thanks a lot Jamie, I'll try waht you suggest

Paolo

  • Hero Member
  • *****
  • Posts: 537
Re: combobox inside a treeview
« Reply #4 on: October 14, 2024, 11:40:09 am »
Hi Jamie,

testing on going, almost good, but I see that if the node that has as sub-node the combobox is collapsed then the combobox, if it was in the selected state, remain visible ! What I did was to go trough the current collapsed node children and make any combo not visible.

thanks again.

 

jamie

  • Hero Member
  • *****
  • Posts: 6695
Re: combobox inside a treeview
« Reply #5 on: October 14, 2024, 01:54:14 pm »
yes, glad you got it working for you.

I also want to correct an error I made in the bug report above.

its "OnCustonCreateItem" event that is useless. :o
The only true wisdom is knowing you know nothing

 

TinyPortal © 2005-2018