To create a TTreeView component in Delphi at runtime, you can use the TTreeView class and the Create method of the component’s parent container.
Here is an example of how you might create a TTreeView component at runtime and add it to a form:
|
1 2 3 4 5 6 7 8 |
var TreeView1: TTreeView; begin TreeView1 := TTreeView.Create(Form1); TreeView1.Parent := Form1; TreeView1.Align := alClient; // Make the treeview fill the entire form TreeView1.OnChange := TreeView1Change; // Set the OnChange event handler end; |
This code creates a TTreeView component and sets its parent to be the form Form1. It then sets the Align property to alClient to make the treeview fill the entire form, and sets the OnChange event handler to a procedure named TreeView1Change.
You can then implement the TreeView1Change procedure to specify the code that should be executed when the user selects a different node in the treeview. For example:
|
1 2 3 4 |
procedure TForm1.TreeView1Change(Sender: TObject; Node: TTreeNode); begin ShowMessage(Node.Text); end; |
This code displays the text of the selected node in a message box.
You can customize the appearance and behavior of the TTreeView component by setting its various properties, such as the Visible property to control whether the component is visible, the Color property to set the background color, and the ReadOnly property to control whether the user can edit the treeview. You can also use the Items property to add, delete, and modify the nodes in the treeview.
Leave a Reply