Reputation: 6710
I have a rather strange issue about which I am having trouble finding documentation. I have a Delphi program that uses Delphi's built-in TDBMemo
component, as the component needs to pull data from a field in a database. The user must also be able to edit this information, namely add information to the component's field. Text typed to the field is visible and does not disappear; however, that is not my problem.
The form gives the user the option to post pre-defined comments from a list, accessed through the equivalent of a pop-up triggered by a TBitButton
; however, once the selected text is added to the TDBMemo
and the user clicks anywhere, the added values disappear - again, not the typed text.
Here is the code for the assignment:
var NoteString: String;
if DBMemo1.Text <> '' then
begin
NoteString := frmSelectNoteCodeView.GetTextfromField + ' - ' + User.ID
+ ' on ' + FormatDateTime('mm/dd/yyyy', Now);
DBMemo1.Text := dbedComments.Text + #13#10 + NoteString;
end;
This is purposefully a code fragment (if the field is blank, the value is just assigned). I am posting this code fragment as I believe this is where the issue is, i.e. that a regular assignment cannot be used with TDBMemo?
Here's the thing: there are no events dealing with user clicks. Any ideas as to why the posted text disappears?
Upvotes: 3
Views: 1213
Reputation: 30745
As I said in my comment, the TDBMemo is a data-aware control, meaning that it's set up to display the text of the associated field in the dataset; you can't just stuff a value into its text property, because any time the TDBMemo is told to refresh itself (via its TDatalink), it retrieves the text from the field, overwriting whatever you thought you'd assigned to it.
I'd do something like this
var
ADataSet : TDataSet;
begin
ADataSet := DBMemo1.DataSource.DataSet; //just to reduce typing
if not (ADataSet.State in [dsInsert, dsEdit) then
ADataSet.Edit;
ADataSet.FieldByName(DBMemo1.FieldName).AsString := 'your text goes here';
ADataSet.Post;
end;
Or you could leave it to the user to call Post.
Might be better if the .Post were in a finally block.
Btw, your problem arises from the tight control that Delphi's db-aware framework exerts over the on-screen display of db-aware controls. Generally, this will fight you trying to directly alter what these controls display; if you want to change dataset data, then change it, not what an associated control displays.
Upvotes: 7