开发者

Advice on setting up new TreeView Data Type

I would like some advice and or tips on how to go about setting up a new Object Class for a TreeView using the TreeView's Node.Data property.

I know how to create a basic Object type that can write/read from the .Data property, I do something like this:

type
  TMyData = class(TComponent)
  private
    FString1: String;
    FString2: String;
    FInteger1: Integer;
  published
    property String1: String read FString1 write FString1;
    property String2: String read FString2 write FString2;
    property Integer1: Integer read FInteger1 write FInteger1;
  end;

To write a value to one of the properties Node.Data property:

TMyData(TreeView1.Selected.Data).String1:= 'save this String';

To read a val开发者_如何学Pythonue:

ShowMessage(TMyData(TreeView1.Selected.Data).String1);

Thats a pretty basic example of assigning Data to a Tree Node, Now this is where I need the help and advice, tips etc...

From my example above, you can easily read/write standard properties such as String, Integer, Boolean etc. What about including and reading/writing to and from say a TListBox or TListView?

Instead of writing standard data to a Tree Node, how about adding items to that TListView and writing the contents of that TListView to the TreeView Node.

What if when clicking on one of the Tree Nodes, instead of returning a string, it could populate that TListView with Items and Images that were saved to earlier.

(1) This is the main form where you can add the Nodes to the TreeView. I actually use a more enhanced function to add to my TreeView, like so:

function AddMyDataNode(ATreeView: TTreeView; NodeName: String; NodeImage: Integer;
    String1, String2: String; Integer1: Integer;
    SelectNode: Boolean): TTreeNode;
var
  Obj: TMyData;
  Node: TTreeNode;
begin
  Result:= nil;

  Obj:= TMyData.Create(nil);
  try
    Obj.String1:= String1;
    Obj.String2:= String2;
    Obj.Integer1:= Integer1;

    Node:= ATreeView.Items.AddObject(ATreeView.Selected, NodeName, Obj);
    Node.ImageIndex:= NodeImage;
    Node.SelectedIndex:= NodeImage;

    Result:= Node;

    if SelectNode then
      Node.Selected:= True;

    ATreeView.SetFocus;
  except on E: Exception do
    ShowMessage('Could not add MyData Node: ' + E.Message);
  end;
end;

Editing a Node will show another form called the Data Form...:

(2) This Data Form is where the data from the Node on the Main Form is read/written. With an addition of a TListView. The ListView should also be read/saved with the standard properties too.

Adding/Editing a ListView item may include other standard properties, such as:

(3) The values on this form are saved into the ListView data.

So in a nutshell:

  • The TreeView Node.Data stores standard properties and also a TListView.
  • This Data is show on another form called Data Form.
  • The ListView on the Data Form also has data.
  • Editing the ListView on the Data Form will show an additional Form with standard properties.

I would like to know how I could best approach this, what tips could you provide, what is the easiest way to achieve something like this etc?

Writing/Reading to and from the ListView is easily implemented I imagine in that it can be done by accessing the ListView Items.Item[x].Data property. Getting the ListView Data to store with the TreeView is the problem, I don't know how I could implement this type of behaviour.

I would like to know advice on things like how I could define the Types etc.

I look forward to hearing your ideas on this.


I think your confusion is thinking of storing Visual Forms and components including their data. I believe you can do this with the the streaming system - although I never have! I like to keep the data and the visuals as separate as possible.

So you need a custom list to put in MyData that contains all the data and settings that need to go in the listview something like MyDataList: (I'm typing this straight in!)

type

TMyDataViewData = class(TPersistent)
  Caption: string;
  ImageIndex: Integer;
  StateIndex: Integer;
  Group: Integer;
  MemoData: TStrings;
end;

TMyDataViewDataList = class(TPersistent)
  TheList: TObjectList;
  property Items[Index:Integer]:MyDataViewData read getItems write SetItems;
  property Count: Integer read GetCount;
  . 
  .
  .
end;

  TMyData = class(TComponent)
  private
    FString1: String;
    FString2: String;
    FInteger1: Integer;
  private 
    MyForm: TMyEditform;
  published
    procedure Edit(Owner: TComponent);
    property String1: String read FString1 write FString1;
    property String2: String read FString2 write FString2;
    property Integer1: Integer read FInteger1 write FInteger1;
    property TheDataList: TMyDataViewDataList read fTheDataList write fTheDataList;  
end;

procedure TMyData.Edit;
var
  F: TMyEditForm;
begin
  F:= TMyEditForm.create(Owner);
  F.DataList := TheDatalist;  // data for listview and memos
  F.ShowModal;
  .
  .
  . 
end;

Then in your Treeview you only need to do:

  If TreeNode.Data is TMyData then
    TMyData(TreeNode.Data).Edit(Self);

After the edit all the data for changing the treeview node info should be in MyData so you can do

  Node.Text := TMyData(Node.Data).TheDataList.Items[0].Caption;

or

for i := 0 to TMyData(Node.data).TheDataList.Count do
    Node.AddChild(TMyData(Node.Data).TheDataList.Items[i].Caption);

You can also easily expose properties of the Datalist to TMyData directly :)

and such like. Probably a few syntax error in there!


I don't see any problems with your approach. You can let your data object contain a list of objects (I don't know why you inherit from TComponent in a class that has nothing to with visual components):

TMyData = class
private
  FString  : String;
  FInteger : Integer;
  FItems   : TList <TItem>   // where TItem is your custom object class
public
  // ... properties ...
end;

This list is then displayed in the list box:

 for Item in MyData.Items do
   ListBox.Items.AddObject (Item.ToString, Item);

If you edit one of the items, you do the same thing: get the object out of the list box's data and pass it on to the next form.

Hope that helps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜