Search code examples
.nettreeviewreplacedelphi-prismaddchild

Delphi Prism : Replacement for TreeView AddchildObject function


I am in the process of migrating software for win32 to .NET and currently working with TreeView control in Delphi Prism. So far I am able to add parent nodes and child nodes to the TreeView. However, I would like to know if there is replacement for AddchildObject function for Delphi Prism TreeView. If not, how would you do it?

It seems there is very little information online about this.


Solution

  • I believe that this question may not get answered by the fellow programmers in here and I feel that this is an important question for any programmers doing Delphi Prism. So, I decided to answer the question myself instead of deleting it, since I found the answer for it in another StackOverflow question. However, my question and their question is different but require the same answer.

    I wrote a quick and simple delphi prism sample to show how to use treeview and to be able to store and retrieve objects in the treeview node.

    Here is my Treeview example

    namespace TreeViewExample;
    
    interface
    
    uses
      System.Drawing,
      System.Collections,
      System.Collections.Generic,
      System.Windows.Forms,
      System.ComponentModel;
    
    type
      /// <summary>
      /// Summary description for MainForm.
      /// </summary>
      MainForm = partial class(System.Windows.Forms.Form)
      private
        method MainForm_Load(sender: System.Object; e: System.EventArgs);
        method treeView1_Click(sender: System.Object; e: System.EventArgs);
      protected
        method Dispose(disposing: Boolean); override;
      public
        constructor;
      end;
    
      theclass = class
      thestr:String;
      public
      constructor;
      end;
    
    implementation
    
    {$REGION Construction and Disposition}
    constructor MainForm;
    begin
      //
      // Required for Windows Form Designer support
      //
      InitializeComponent();
    
      //
      // TODO: Add any constructor code after InitializeComponent call
      //
    end;
    
    method MainForm.Dispose(disposing: Boolean);
    begin
      if disposing then begin
        if assigned(components) then
          components.Dispose();
    
        //
        // TODO: Add custom disposition code here
        //
      end;
      inherited Dispose(disposing);
    end;
    {$ENDREGION}
    
    constructor theclass;
    begin
      thestr:='Testing Treeview.';
    end;
    
    method MainForm.MainForm_Load(sender: System.Object; e: System.EventArgs);
    var topnode:treenode;
        theObject:theclass;
    begin
      theObject:=new theclass;
      treeview1.BeginUpdate;
      topnode:=treeview1.Nodes.Add('node1');
      topnode.Nodes.Add('no data node');
      topnode.Nodes.Add('data node').Tag := theObject;
      topnode.Expand;
      treeview1.EndUpdate;
    end;
    
    method MainForm.treeView1_Click(sender: System.Object; e: System.EventArgs);
    begin
      if treeview1.SelectedNode.Text='data node' then
        MessageBox.Show(theClass(Treeview1.SelectedNode.Tag).thestr);
    end;
    
    end.
    

    Here is the link to the question.

    save text fields to an Array (and pull data from the Array) when using treeview in c#