Reputation: 383
I have a form which has a TDBLookupComboBox
on it.
The TDBLookupComboBox
is displaying a list of records from within a database table.
At the point of the forms OnShow
event I would like the TDBLookupComboBox
to already display one of the strings in the list.
I have done this so far...
procedure TfrmMain.FormShow(Sender: TObject);
begin
dblucbox.Text := Username;
end
Username is a string for one of the records already in the list.
At the point of compiling, I get an error saying
Cannot assign to a read only property
I'm a bit stuck with this so any help would be appreciated.
Upvotes: 1
Views: 2189
Reputation: 1838
You're going about this backwards. To be more precise, the DBLookupCombo is reflecting the state of the database table. So you want to be manipulating the table, not the combobox.
In other words, the OnShow event needs to open the table that's the object of the DBLookupCombo (if it's not already open) and then position the current record to be the one you want displayed as the default.
Upvotes: 2
Reputation: 136391
Don't try to modify the Text
property, instead if you want set the TDbLookUpComboBox
in a particular item you must use the the KeyValue
property which will try to locate the record in the underlying TDataSet.
So you if you have Key Value of the user you can use something like this
dblucbox.KeyValue := UserId;
Otherwise you can use the Locate method of the underlying TDataSet, to find the match and the LookUp control will be refreshed automatically
Upvotes: 3