Recent

Author Topic: Can't get Item's Text property  (Read 11902 times)

fafastrungen

  • New Member
  • *
  • Posts: 24
Can't get Item's Text property
« on: February 28, 2011, 08:49:53 pm »
Hi, I'm making an unit to translate applications made with Lazarus.
I save in an ini file all captions, hints and text value and then I set them automatically using SetPropValue function.
But before set properties values, I have to find them and I do it using GetPropList function.

This is an example:
Count := GetPropList(comboBox, PropList);

This works perfectly, finds every combobox's property, but the next example does not work:
Count := GetPropList(comboBox.Items, PropList);

It return nothing, and the property ("Text") I have to set is owned by Items, not combobox.

In short, what I need is to find the "Items.Text" property, not the "ComboBox.Text" property.

Thanks in advance.

DirkS

  • Sr. Member
  • ****
  • Posts: 251
Re: Can't get Item's Text property
« Reply #1 on: February 28, 2011, 09:26:51 pm »
According to the docs GetPropList returns a list with published properties (http://www.freepascal.org/docs-html/rtl/typinfo/getproplist.html). TStrings (the type of 'Items') does not have any published props.

Gr.
Dirk.

JuhaManninen

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4632
  • I like bugs.
Re: Can't get Item's Text property
« Reply #2 on: February 28, 2011, 10:26:11 pm »
Hi, I'm making an unit to translate applications made with Lazarus.

You mean translating the GUI texts?
"resourcestring" is the way to go:
 http://wiki.lazarus.freepascal.org/Translations_/_i18n_/_localizations_for_programs

Juha
Mostly Lazarus trunk and FPC 3.2 on Manjaro Linux 64-bit.

fafastrungen

  • New Member
  • *
  • Posts: 24
Re: Can't get Item's Text property
« Reply #3 on: March 01, 2011, 12:14:53 am »
Thank you both, very helpful.

fafastrungen

  • New Member
  • *
  • Posts: 24
Re: Can't get Item's Text property
« Reply #4 on: March 03, 2011, 08:45:18 am »
Is there a way to assign ComboBox.Items.Text property at run time ?
I store in a xml file all properties I want to assign, for example:

...
<comboBox>
  <items text="Item 1\nItem 2" />
</comboBox>
...

I search for "comboBox" component, if I found it I search for "Items" property, if I found it I try to assign property using SetPropValue, but I can't assign it because "Items" has no published "Text" property, so how can I assign it ?

JuhaManninen

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4632
  • I like bugs.
Re: Can't get Item's Text property
« Reply #5 on: March 03, 2011, 10:31:56 am »
Is there a way to assign ComboBox.Items.Text property at run time ?

Like this:
  ComboBox.Items.Text := MyText;

which splits by newline automatically. The property does not need to be published to be assigned.
Other ways to add items:

  ComboBox.Items.Add(aString);
  ComboBox.Items.Assign(aStringList);

Juha
Mostly Lazarus trunk and FPC 3.2 on Manjaro Linux 64-bit.

fafastrungen

  • New Member
  • *
  • Posts: 24
Re: Can't get Item's Text property
« Reply #6 on: March 03, 2011, 08:49:17 pm »
Quote

Like this:
  ComboBox.Items.Text := MyText;

which splits by newline automatically. The property does not need to be published to be assigned.
Other ways to add items:

  ComboBox.Items.Add(aString);
  ComboBox.Items.Assign(aStringList);

Juha


Hi, I know that, but my problem is that I assign at runtime all properties that I store in a xml file.
For instance:

...
<butonOk caption"Button" hint="this is a button" />
<labelInfo caption="Info about this" hint="This is a label" />
...

I read every node's properties using a loop and try to assign them using SetPropValue.
Something like this:

var
AttrName, AttrValue: String;
XmlNode: TDomNode;
Obj: TObject;

begin
 ReadXMLFile(XmlDoc, 'english.xml');   
 Node := XmlDoc.DocumentElement.FindNode('Form').FirstChild; // Form is control's parent
 while Node <> nil do begin
  for I := 1 to Node.Attributes.Length do begin
  AttrName := Node.Attributes[I-1].NodeName;
  AttrValue := AnsiToUtf8 (Node.Attributes[I-1].NodeValue);
  Obj := FindComponent(Node.NodeName);
  if Obj <> nil then SetPropValue(Obj, AttrName, AttrValue);
 end;
 Node := Node.NextSibling;
end;

This code is working but only with published properties, if in the xml file I store this:
...
<listBox>
 <items text="Line 1\nLine 2\nLine 3" />
</listBox>
...

I can't assign the property "text" of the "items" object because "text" is not published.
I need a way to assign not-published-properties.

Thanks.

JuhaManninen

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4632
  • I like bugs.
Re: Can't get Item's Text property
« Reply #7 on: March 03, 2011, 11:31:43 pm »
I read every node's properties using a loop and try to assign them using SetPropValue.

ComboBox.Items is published. You must "Add" or "Assign" elements to it.
So, you need little more logic than just SetPropValue.

The Lazarus form loader already loads a form's controls and their properties from a .lfm file.
I think you are duplicating much of that functionality now. You could as well use .lfm form files, too.

Juha
Mostly Lazarus trunk and FPC 3.2 on Manjaro Linux 64-bit.

DirkS

  • Sr. Member
  • ****
  • Posts: 251
Re: Can't get Item's Text property
« Reply #8 on: March 03, 2011, 11:45:02 pm »
BTW: I don't think it makes much sense to fill the .Text property when it's for translation purposes. As already mentioned you should populate the list and then e.g. set .ItemIndex and let the system do the rest.

Gr.
Dirk.

fafastrungen

  • New Member
  • *
  • Posts: 24
Re: Can't get Item's Text property
« Reply #9 on: March 04, 2011, 01:08:45 am »
Quote
The Lazarus form loader already loads a form's controls and their properties from a .lfm file.
I think you are duplicating much of that functionality now. You could as well use .lfm form files, too.

Lazarus form loader use .lfm in compilation time, not in execution time. After I compile the app I have an .exe file only, so I want to translate the form at runtime.

Quote
BTW: I don't think it makes much sense to fill the .Text property when it's for translation purposes. As already mentioned you should populate the list and then e.g. set .ItemIndex and let the system do the rest.

Gr.
Dirk.

It's easier to set text property because I read data only one time and set data in one time, 2 actions to achieve the translation.
If I populate the list I must use a loop, read each node in the xml file, read every item in the items property and set the text.
I want a solution with minimum code.

Of course, thanks to all for your help.

 

TinyPortal © 2005-2018