Recent

Author Topic: TTIComboBox example (RTTI)  (Read 2284 times)

feds

  • New Member
  • *
  • Posts: 33
TTIComboBox example (RTTI)
« on: September 26, 2019, 11:16:54 pm »
Hi all

Can anyone point me to an example how to use TTIComboBox, please?

I just started with the RTTI components and the few things i tried so far works as expected.
But i can't figure out what TTIComboBox is intended to do.

I have had a look to the example program provided with Lazarus, wich use the "special case" of a boolean property to show true/false in the combobox.

Having a look into the source of TTIComboBox i found that the component wants a TStrings as property. But i didn't understand how this can work. Unless i manually change one of the entries there was no change in the property, right? It does not change.
Obvious, because only the ItemIndex changes (beside the visual representation). But ItemIndex is not part of the property.


My intended use is this:
In my model i have some integers (subranges) that i would like to present using constant strings.
I.e. 0 in the model means 'Left channel', 1 means 'Right channel', 3 means 'Both channels' etc.

Just as an experiment i replaced the LinkLoadFromProperty and LinkSaveToProperty methods with modified ones that handles the ItemIndex instead of Itmes.

For now the following works (playcode only):
Code: [Select]
procedure TForm1.FormCreate(Sender: TObject);
begin
  fModel := TModel.Create;                               // Only to provide the strings to TIComboBox1
  ...
  with TIComboBox1 do
  begin
    Link.OnLoadFromProperty := @LinkLoadFromProperty;
    Link.OnSaveToProperty := @LinkSaveToProperty;
    Link.SetObjectAndProperty(SpinEdit1, 'Value');        // SpinEdit1 is "misused" to provide a model property
    Items := fModel.fStrings;
    if Items.Count > 0 then  ItemIndex := 0;
  end;

end;     

procedure TForm1.LinkLoadFromProperty(Sender: TObject);
var
  theOwner: TTICustomComboBox;
theLink: TCustomPropertyLink;
actualIndex: integer;
begin
   if Sender=nil then ;

    theLink := TCustomPropertyLink(Sender);
  if (theLink.Editor = nil) then
    exit;

  theOwner := TTICustomComboBox(theLink.Owner);
  actualIndex := theLink.GetAsInt;

  theOwner.ItemIndex:=actualIndex;

end;

procedure TForm1.LinkSaveToProperty(Sender: TObject);
var
  theOwner: TTICustomComboBox;
  theLink: TCustomPropertyLink;
  actualIndex: integer;
begin
  if Sender = nil then
  ;


  theLink := TCustomPropertyLink(Sender);
  if (theLink.Editor = nil) then
    exit;
  theOwner := TTICustomComboBox(theLink.Owner);
  actualIndex := theOwner.ItemIndex;

  theLink.SetAsInt(actualIndex);

end;               


But i'm not shure if this is the intended use of that class. Hence i'm looking for examples. But found none, unfortunally ...

So, as usual: Any hints are highly welcome.  :)

Regards
feds

« Last Edit: September 27, 2019, 12:47:00 am by feds »

jamie

  • Hero Member
  • *****
  • Posts: 7705
Re: TTIComboBox example (RTTI)
« Reply #1 on: September 27, 2019, 12:45:15 am »
I've never found a real use for those because I make my own HMI interface code for users to configure..

  But if you drop two combo boxes on the form one being a normal combobox and the other being a TIComboBox, you can then activate the editor of what ever property of the normal combobox you wish..

 Basically it would be the same editor you see in the OI as you code.

 with the norm combo you set the Link.TIobject : = MyNormalCombo;

 Link.TIPropertyName : = 'WIDTH';

 The Editor is active on startup from what I remember..

  When run, the TICombo will become the Property editor for the WIDTH property of MyNormalCombo.

 Basically, you could make your own GUI script that runs at near speeds of full native..
The only true wisdom is knowing you know nothing

feds

  • New Member
  • *
  • Posts: 33
Re: TTIComboBox example (RTTI)
« Reply #2 on: September 27, 2019, 01:40:24 am »
  When run, the TICombo will become the Property editor for the WIDTH property of MyNormalCombo.

Thanks for that suggestion. But to be honest i can't imagine this in an application.
Assume the user enters 'hello' into the TTiCombo. Since this can't be converted to an int the application would raise an exception (i fear).

But maybe using the TTiCombo with enums could work. Iirc enums.ToString works and the TTiBox can translate this using the alias table?
Looks like an intersting test tomorrow.

Regards
feds

jamie

  • Hero Member
  • *****
  • Posts: 7705
Re: TTIComboBox example (RTTI)
« Reply #3 on: September 27, 2019, 02:03:11 am »
These are the property editors you find when making a component, you can use of those as the editor..

 For components that don't have a listing on there, you can use one of them existing RTTI components to edit properties of components that do not exist on the RTTI palette

 For example an exotic component that has a label caption in it, you can use the TTILabel one to edit its caption, just select the caption and so on.

 But like I said, I've never found a lot of use for them except in apps where you are some how allowing the user to build a GUI using the LCL as the controls. Where a user may have the option to add a memo, edit box or something in a document..
The only true wisdom is knowing you know nothing

feds

  • New Member
  • *
  • Posts: 33
Re: TTIComboBox example (RTTI)
« Reply #4 on: September 29, 2019, 05:47:55 pm »

 For example an exotic component that has a label caption in it, you can use the TTILabel one to edit its caption, just select the caption and so on.


But my application is quite more simple.

Look at the example https://forum.lazarus.freepascal.org/index.php/topic,40909.msg283029.html#msg283029.
nana232 operates on an enum whose representation is shown in an TTiCombobox. The user can select one of them.

All i want is the same but not with an enums but with TStrings instead.
This would allow my application to provide a set of strings (not enums) to the user to choose from.

regards
feds

egsuh

  • Hero Member
  • *****
  • Posts: 1791
Re: TTIComboBox example (RTTI)
« Reply #5 on: September 01, 2022, 02:54:45 pm »
I might be late, but I added a simple explanation to wiki regarding this. (Actually for my own reference ... I test many things but I forget later  :D)

https://wiki.freepascal.org/TTIComboBox

Hope others to add more details.

 

TinyPortal © 2005-2018