TTMSFNCComboBox data aware

Hello,

Do you have an example of using the TTMSFNCComboBox as a data aware component to lookup a table with a code and description so that the code is stored in my table?

Thanks,

Erik Fidlers

Hi,

You could use TTMSFNCDataBinder for this. Here you can find the documentation guides: Data Binding | TMS Software Documentation
For programmatic hookup you can do the following:

procedure TForm1.FormCreate(Sender: TObject);
begin
  TMSFNCDataBinder1.ConnectList(TMSFNCComboBox1, DataSource1, 'Items', 'FieldName');
end;

Alternatively you can also connect everything at design-time, you'll need to add an item to the TTMSFNCDataBinder, set the BindComponent to TTMSFNCComboBox, the BindType to list, the PropertyName to Items and the FieldName to your field name.

Hello,

Does it also apply when i have a field in my table that is called CODE and the TMSFNCComboBox1 shows only descriptions? the Items are fille programmatically from a table with 2 fields (CODE and DESCRIPTION). How to link it in the TMSFNCDataBinder1?

Thanks,

Erik

Sorry, it's a bit unclear. Do you want the TTMSFNCComboBox to show the DESCRIPTION only but also return the CODE on selection? That won't work because TTMSFNCComboBox.Items is a TStringList so there's no way to couple to another field without displaying it.

You can either display both fields by using the HTMLTemplate property (for example: <#CODE> - <#DESCRIPTION>), or you'll need to use the TTMSFNCComboBox.OnItemSelected event to check the dataset directly. That is assuming you don't have duplicate descriptions and you are free to move the cursor in the dataset:

if TMSFNCDataSet1.Locate('DESCRIPTION', AText, []) then
  Label1.Text := TMSFNCDataSet1.FieldByName('CODE').AsString;

Seems like what you need is to bind the combobox to a LookUp Field (you'll need a second dataset).
I haven't used FNC Data Binder yet. Maybe supports handling a lookupfield, I really don't know.

Anyway, if you don't really need automatic binding, there's another technique to handle code/description joining when using list-like controls based on TStrings. You set the Item string as the Description field or any text composition from other fields, and then set the TStrings.Items.Objects[xx] to an underlying instance with all the propertis you could need further, like ID or CODE. Then you just hace to access YourCombo.Items.Objects[YourCombo.ItemIndex] as TYouClass.

Ex:

// Filling combo items
ComboUsers.Items.Clear;
for LUser in FUserList do
  ComboUsers.Items.AddObject(LUser.Name, LUser);


// Using combo selected item
LUser := nil;
if ComboUsers.ItemIndex > -1 then
  if ComboUsers.Items.Objects[ComboUsers.ItemIndex] is TUserClass then
    LUser := (ComboUsers.Items.Objects[MyCombo.ItemIndex] as TUserClass);
if Assigned(LUser) then begin
  // do whatever you need with LUser
end;


Thanks for the replies but it is not what I was hoping for. I hoped to have an easier way to handle this kind of functionality in Delphi. I'll have to write a component that does it easier for me.

Hi!
The easiest way to do this in Delphi is using a lookup field. "CODE" field assignment will bu automatic. Before writing a new component, please just check if fnc data binder supports it.