Recent

Author Topic: Can I assign object self as a property of RTTI-controls?  (Read 967 times)

egsuh

  • Hero Member
  • *****
  • Posts: 1273
Can I assign object self as a property of RTTI-controls?
« on: October 04, 2020, 11:48:17 am »
Hi,

This is rather interesting. I'd like to use TTIMemo for editing a TStringlist object.

Code: Pascal  [Select][+][-]
  1. type
  2.    TMyStringList = class (TStringList)
  3.         MyVar : integer;
  4.         procedure MyMethod;
  5.    end;
  6.  
  7.   TCFEditor = class(TForm)
  8.      TIMemo1: TTIMemo;
  9.  
  10.    public
  11.        ASL: TMyStringList;
  12.   end;
  13.  
  14. ///////
  15.  
  16. procedure TCFEditor.FormCreate(Sender: TObject);
  17. begin
  18.    ASL := TMyStringList.Create;
  19.  
  20.    with TIMemo1.Link do begin
  21.       TIObject := ASL;
  22.       TIPropertyName := 'self';  // Self does not work.
  23.    end;
  24. end;
  25.  

Is there anything that I can write at the place of 'self' above?  'Self' does not raise exception, but does nothing either.

wp

  • Hero Member
  • *****
  • Posts: 11858
Re: Can I assign object self as a property of RTTI-controls?
« Reply #1 on: October 04, 2020, 12:53:25 pm »
My experience with the RTTI controls is very limited, I only worked with TIPropertyGrid so far. When I set its TIObject property to "self" (the form) the properties of the form are displayed and follow the changes in the TIPropertyGrid. Nice.

Then I tried a TTIEdit, set its Link.TIObject to a "normal" Listbox on the form, and its Link.PropertyName to "Width". When I type a new value into the TIEdit at runtime the width of the Listbox follows. Nice, too.

But now it gets difficult: I added a TTIListbox, linked it to the Items of the "normal" Listbox (TIListbox1.Link.TIObject = Listbox1, TILisbox1.Link.PropertyName = 'Items') --> no effect, neither at runtime, nor at designtime: I don't see the Listbox.Items in the TIListbox.

The same when I link a TIMemo and a "normal" Memo via the "Lines" property.

So, are the RTTI controls having problems with listed properties? Or is my understanding of these controls too limited?

Beyond that, I doubt that the items of a TStringList can be displayed in a TTIMemo at all. AFAIK, RTTI requires the properties to be published, but TStringList only has public properties...

Why don't you simply assign the stringlist to the Lines of a "normal" TMemo?  (Memo1.Lines.Assign(SL)).

P.S.
I added a short article on TIPropertyGrid to thw wiki
« Last Edit: October 04, 2020, 01:23:50 pm by wp »

egsuh

  • Hero Member
  • *****
  • Posts: 1273
Re: Can I assign object self as a property of RTTI-controls?
« Reply #2 on: October 04, 2020, 02:54:01 pm »
The reason I use RTTI controls is to synchronize between non-visible content within a class and visual content of the control without any coding.

Quote
I added a TTIListbox, linked it to the Items of the "normal" Listbox (TIListbox1.Link.TIObject = Listbox1, TILisbox1.Link.PropertyName = 'Items') --> no effect, neither at runtime, nor at designtime: I don't see the Listbox.Items in the TIListbox.

The same when I link a TIMemo and a "normal" Memo via the "Lines" property.
   

Regarding TTIListBox, I confirmed the following works.

Code: Pascal  [Select][+][-]
  1.    listbox1.Items.Text := 'List 1'#13#10'List 2'#13#10'List 3';
  2.  
  3.    tilistbox1.link.TIObject := listbox1;
  4.    tilistbox1.link.tipropertyname := 'itemindex';
  5.  
  6.    tilistbox1.items.CommaText := 'i1,i2,i3';
  7.    tilistbox1.Link.AliasValues.CommaText := '0=i1,1=i2,2=i3';
  8.  

You can type in Listbox1.Items, TIListBox1.Link.TiObject and TIListBox1.Link.TiPropertyName in object inspector at design time, but you have to define contents of TTListBox1.Items and TIListBox1.Link.AliasValues during runtime.

And regarding TTIMemo and TMemo, you can synchronize the content of the two controls by linking TTIMemo.Link.TIPropertyName to TMemo.Lines.

But you should be aware of one thing.

When normal control is changed, the content of RTTL control is changed immediately. If you type in one character in normal memo, TIMemo will show them.

The vice versa is not true --- content of normal control is updated AFTER YOU LEAVE RTTI CONTROL. It seems that reverse update is done at ONEDITINEDONE or similar event.

Quote
TStringList only has public properties...

You are right. I actually use TStringList descendants so that I can redefine a property under published section.

Quote
Why don't you simply assign the stringlist to the Lines of a "normal" TMemo?  (Memo1.Lines.Assign(SL))

I didn't know Assign function. Does this mean that I can change the content of SL via editing the content of Memo1, and vice versa?  If this is available, I don't have to use RTTL controls in my applications.

-----------------------------------

ADDITION:

I hope somebody add the usage of AliasValues to the document somewhere. I could find this only through trial and error.
« Last Edit: October 04, 2020, 03:08:10 pm by egsuh »

Sieben

  • Sr. Member
  • ****
  • Posts: 310
Re: Can I assign object self as a property of RTTI-controls?
« Reply #3 on: October 04, 2020, 03:00:01 pm »
With Assign() you can copy the whole content of a TStringList to another. Memo.Lines and ListBox.Items are TStrings resp TStringLists as well. You can gather your data in SttringList, assign it to a Memo Lines, edit it there and when editing is done assign it back to your original list or another one. Same with ListBox Items.
« Last Edit: October 04, 2020, 03:02:43 pm by Sieben »
Lazarus 2.2.0, FPC 3.2.2, .deb install on Ubuntu Xenial 32 / Gtk2 / Unity7

egsuh

  • Hero Member
  • *****
  • Posts: 1273
Re: Can I assign object self as a property of RTTI-controls?
« Reply #4 on: October 04, 2020, 03:03:46 pm »
Quote
With Assign() you can copy the whole content of a TStringList to another.

Yes. I just checked it, so they are not synchronized.

And I tested     Memo1.Lines := SL; as well, which did not work either (this is little bit ridiculous, but we can think of...). Even though I changed the content of Memo1, content of SL did not change^^.

Sieben

  • Sr. Member
  • ****
  • Posts: 310
Re: Can I assign object self as a property of RTTI-controls?
« Reply #5 on: October 04, 2020, 03:18:16 pm »
No, they are not synchronized. But if you can do like I said - assign it to Memo, do editing, and then assign it back once editig is done - you get to the same ending.
Lazarus 2.2.0, FPC 3.2.2, .deb install on Ubuntu Xenial 32 / Gtk2 / Unity7

 

TinyPortal © 2005-2018