Using .AsString or .Text? - delphi

I have just seen a bit of code (D5) where they used...
aStr:=tblAcct.FieldByName('Name').Text;
It seems to work fine but I have always used...
aStr:=tblAcct.FieldByName('Name').AsString;
I have used both when loading a TMemo and again there seems no difference.
aMemo.Lines.Text:=tblAcct.FieldByName('History').Text;
aMemo.Lines.Text:=tblAcct.FieldByName('History').AsString;
Is there a reason why I should use one over the other?
If so, which one?
Actually for TMemo, I usually use...
aMemo.Lines.Assign(tblAcct.FieldByName('History'))
which seems to work fine too.
Thanks

The Text property is meant to be used to obtain the textual representation of a field being edited in a DataAware control, in contrast with the DisplayText property that gives you a string to represent the value to the user (it may contain punctuation or other decoration to the plain value).
Contains the string to display in a data-aware control when the field is in edit mode
A typical example is a TFloatField with the Currency property set to True. The DisplayText gives you a string with the number containing commas (if needed), the decimal separator and a currency symbol. The Text property gives you a string without commas or currency symbol.
begin
MyFloatField.Currency := True;
MyFloatField.AsFloat := 1234.56;
A := MyFloatField.Text; //'1234.56'
B := MyFloatField.DisplayText; //'$1,234.56', depends on your locale
end;
Both above properties can be customized writing a OnGetText event handler where you can write custom logic to convert the value to a string representation. The DisplayText parameter indicates if the wanted string is meant to represent the value for edit or not.
On the other hand, the AsString property uses a more plain conversion between the base data type and string. Each TField descendant implements the virtual GetAsString method using functions from the RTL to perform that representation. Following the TFloatField example, this class calls FloatToStr() for that purpose.
All this said, the answer to your question is: AsString returns the same string as the Text property if there's no OnGetText event handler, but it may be different if there's a event handler or a non-standard TField descendant.
I can't tell what is more appropriate for you, because it depends on what's the intended use for the returned value, but if you're using it to display the values to the user in the UI (as your code example), I advise you to use the DisplayText property.

AsString contains field's value as string.
Text contains the string to display in a data-aware control when the field is in edit mode.
So in your case I think you should use AsString.

Related

TValueListEditor with more columns

I'm using a TValueListEditor and i would like to make an IDE property editor like.
Each line can have few kinds of informations :
String
Date
Number
ComboList
I'm wondering how can i store the type of element and the format for each line.
I try to add 2 columns. But its not working
ValueListEditor.ColCount := 4;
ValueListEditor.Cell[4,2] := 'Test';
It shows the value Test in the value column (the second column instead of 4).
I was thinking to embeded the values that i need in an object that i will link (add) to each line. But i didnt' find the way to do so. Even i don't know if it's possible.
Does anyone have an idea ?
TValueListEditor is specifically designed to handle name=value pairs only. You can't add additional columns to it. However, you can specify the type of editor used for editing the value column. That is handled by the TItemProp.EditStyle property for each pair:
For String input, set the TItemProp.EditStyle to esSimple.
For ComboList input, set the TItemProp.EditStyle to esPickList, and then use the TItemProp.PickList property, or the TValueListEditor.OnGetPickList event.
For Numeric input, set the TItemProp.EditStyle to esSimple, and set the TItemProp.EditMask and TItemProp.MaxLength as needed. You can then convert the user's entered value to an Integer when needed.
For Date input, you are best off setting the TItemProp.EditStyle to esEllipsis and then use the TValueListEditor.OnEditButtonClick event to display your own TForm that has a TDateTimePicker on it. You could do something similar for numeric input if desired, using a TSpinEdit or similar component.

How to get a shortcut string from TShortCut in an action?

I'm using a TActionManager, where each action has a keyboard ShortCut assigned to it. I would like to display text which represents the keyboard shortcut to the user. For example, F4 or Ctrl+F or Ctrl+Shift+S. However, the TShortCut is defined as:
type
TShortCut = Low(Word)..High(Word);
How can I obtain a user-readable string which represents the shortcut assigned to an action?
I'm answering my own question Q/A style.
There's a built-in function for this called ShortCutToText() which converts any given TShortCut into a readable representation. On the other hand, there's also TextToShortCut() which works the other way around, converting any string into a TShortCut, given it's a valid shortcut value.

lazarus - TMemo text property

I am using a TMemo box intead of a TEdit box simply because of the multi-line ability.
I was guessing and I used the .Text property to assign a value at run-time. But then I realised that there was no Text property at design time. Rather I found the Lines property.
I checked this page: http://lazarus-ccr.sourceforge.net/docs/lcl/stdctrls/tmemo.html for more info.
I found out that there is no Text property but only a Lines property in that documentation.
Is my use of .Text correct or is it a problem using this?
Here's my code:
if dlgSave.Execute Then
begin
txtSaveName.Text := dlgSave.FileName;
end;
txtSaveName : TMemo
dlgSave : TSaveDialog
Thanks for any inputs.
Lines is a TStrings, a class that is basically a wrapper for an array of strings.
Text is a simple property that when read concatenates the strings together (with lineseparators between them), and when assigned too parses the single string into multiple strings.
I wouldn't worry too much about it, just think twice before using it for huge strings (think hundreds of MBs and bigger), since all the copying done by this highlevel functionality will eat quite some memory.
Another (minor) reason not to use it is if you want reading and writing to be binary the same. Assume you have a text with mixed line endings and you assign it to text and read it back, then the mixed lineendings will be uniform now.

Convert Text value to Integer using IValue converter

In my application i am binding a integer to a gridview column.
Scenario : In the cell edit mode of gridview, if the user types some string values like A+,A. i want text to convert it automatically to integer value.
I am having a collection where each string value will be having a integer assigned.
In the converter i want to check for that and show its corresponding integer value.
Can it be done using IValueConverter
Yes, it makes sense to do this in a value converter.
Out-of-the-box, .NET does not provide any classes or methods for parsing the expression into its numerical equivalent, but you should be able to use numerical parsing libraries like for example NCalc or Simple Math Parser to "do the job" for you.
I honestly don't know if these libraries are immediately available for Silverlight, but if not it is probably worth the effort to port them to SL yourself, rather than writing your own math parser.

Delphi TDataSet filter case insensitive not working

I have an application with lots of datasets created at run-time. Some of this datasets are filtered. As filterOptions I've set foCaseInsensitive
dataset.FilterOptions := [foCaseInsensitive];
that means that 'foCaseInsensitive - Ignore case when comparing strings. ' according to Delphi help.
The dataset.filter is changed on user input.
At begging it shows all values:
when user type something is changed:
The problem is that when I'm typing the last character, it shows nothing
Problem does not appear when I'm typing on upper-case
how can I fix this, to show correct the values when user input is only in lower case?
LE: the component is a standard TDataSet from Delphi.
Filter is assigned with 'Like %value_entered%'.
LE1: I'm using a in-house component which has a DataSet property. After I'm setting the filter, I set the property Filtered to true. So, it is a TDataSet property on what I'm talking about.

Resources