TDBLookupComboBox/fr: what changed
Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.
Title: TDBLookupComboBox/fr
| Old | New | Line | |
|---|---|---|---|
| 1 | + | **TDBLookupComboBox**  est un [[TComboBox/fr|TComboBox]] lié aux données (doublement). Il peut être trouvé sur l'[[Data Controls tab/fr|onglet Data Controls]] de la [[Component Palette/fr|palette de composants]]. | |
| 2 | + | ||
| 3 | + | Le contrôle TDBLookupCombobox obtient une liste de valeur depuis sa *ListSource* qui représente la donnée d'une autre table (p.ex. des produits). Alors, il | |
| 4 | + | ||
| 5 | + | - affiche les valeurs dans la **ListField** (p.ex. le champ "NomDeProduit") | |
| 6 | + | - se rappelle des valeurs dans la propriété **KeyField** (p.ex. un champ "ID") | |
| 7 | + | ||
| 8 | + | Le ComboBox stocke le résultat (i.e. la valeur **KeyField**) dans la propriété **DataField**, p.ex. le champ "ProduitID" dans la propriété **DataSource** (p.ex. une table "Commandes"). | |
| 9 | + | ||
| 10 | + | La différence avec le [[TDBComboBox/fr|TDBComboBox]] est que le TDBComboBox est un "contrôle individuellement lié aux données" (en quelque sorte). | |
| 11 | + | ||
| 12 | + | ## Utilisation sans liaison | |
| 13 | + | ||
| 14 | + | Vous pouvez obtenir la combobox pour rechercher des valeurs d'une table sans enregistrer les résultats dans une autre en laissant les propriétés *DataSource* et *KeyField* 'vides. | |
| 15 | + | ||
| 16 | + | L'extrait de code exemple suivant : | |
| 17 | + | ||
| 18 | + | - permettra à un TDBLookupComboBox d'être configuré en mode non lié (i.e. sans aucun changement porté dans la base de données) | |
| 19 | + | - Assurera que les données visibles dans le TDBLookupComboBox sont issues de l'enregistrement sélectionné dans le DataSet (i.e. en choisissant une nouvelle valeur dans le TDBLookupComboBox défilera le DataSet vers l'enregistrement correct) | |
| 20 | + | - Définira la valeur initiale affichée dans le TDBLookupComboBox et l'enregistrement sélectionné dans le DataSet pour les valeurs précédemment mémorisées. | |
| 21 | + | ||
| 22 | + | L'extrait de code exemple suivant NE : | |
| 23 | + | ||
| 24 | + | - mettra PAS à jour automatiquement la valeur du TDBLookupComboBox si la DataSet est défilé (vous avez besoin d'ajouter votre propre gestionnaire au Dataset.OnScroll pour accomplir ceci) | |
| 25 | + | ||
| 26 | + | ``` pascal | |
| 27 | + | interface | |
| 28 | + | type | |
| 29 | + | TForm1 = class(TForm) | |
| 30 | + | cboLookup: TDBLookupComboBox; | |
| 31 | + | dsSource: TDatasource; | |
| 32 | + | dsetSource: TDataset; | |
| 33 | + | ... | |
| 34 | + | protected | |
| 35 | + | FDisplayField: string; | |
| 36 | + | FKeyField: string; | |
| 37 | + | FPreviousKeyValue: string; // May be any datatype - see below | |
| 38 | + | ... | |
| 39 | + | end; | |
| 40 | + | ||
| 41 | + | ... | |
| 42 | + | ||
| 43 | + | implementation | |
| 44 | + | ||
| 45 | + | ... | |
| 46 | + | // Remember previous value | |
| 47 | + | FPreviousKeyValue:= load_from_settings_in_your_preferred_way_; | |
| 48 | + | ||
| 49 | + | ... | |
| 50 | + | // Configure DBLookupCombo | |
| 51 | + | dsSource.Dataset := dsetSource; | |
| 52 | + | cboLookup.ListSource := dsSource; | |
| 53 | + | ||
| 54 | + | cboLookup.ScrollListDataset := True; // This ensures that changing the ComboBox will automatically scroll | |
| 55 | + | // the dataset | |
| 56 | + | cboLookup.Style := csDropDownList; | |
| 57 | + | ||
| 58 | + | cboLookup.KeyField := FKeyField; | |
| 59 | + | cboLookup.ListField := FDisplayField; // This is the field that will appear in the contents of the ComboBox | |
| 60 | + | ||
| 61 | + | dsetSource.Open; // The ComboBox should now be populated, however intially no text is displayed | |
| 62 | + | // So right now there is no relationship between the selected record in the dataset | |
| 63 | + | // and the contents of the ComboBox | |
| 64 | + | ||
| 65 | + | // cboLookup.KeyValue reads and writes a Variant, so you may actually define | |
| 66 | + | // FPreviousKeyValue as any data type you feel is appropriate. | |
| 67 | + | ||
| 68 | + | // Remember a previous value | |
| 69 | + | if FPreviousKeyValue <> '' then | |
| 70 | + | begin | |
| 71 | + | dsetSource.Locate(FKeyField, FPreviousKeyValue, []); // Note: This will fail to correctly set the ComboBox, | |
| 72 | + | // But will set the Datatset to the correct record. | |
| 73 | + | ||
| 74 | + | // Now the dataset is on the correct record, lets ensure the DBLookupCombo | |
| 75 | + | // is showing the same value as the selected record | |
| 76 | + | If (cboLookup.KeyValue = Null) And (dsetSource.RecordCount > 0) Then | |
| 77 | + | cboLookup.KeyValue := dsetSource.FieldByName(FKeyField).AsVariant; | |
| 78 | + | end; | |
| 79 | + | ||
| 80 | + | ... | |
| 81 | + | // Save current value for next time | |
| 82 | + | FPreviousKeyValue:= cboLookup.KeyValue; | |
| 83 | + | write_to_settings_in_your_preferred_way_(FPreviousValue); | |
| 84 | + | ``` | |
| 85 | + | ||
| 86 | + | ## Bugs | |
| 87 | + | ||
| 88 | + | Au moins pour TDBLookupComboBox, il y a un bug avec FPC 2.6.0 (utilisé dans Lazarus 1.0, 1.0.2 etc) qui nécessite que la **listfield** aussi bien que la DataSource soit présents. | |
| 89 | + | ||
| 90 | + | Solution de contournement : vous pouvez passez outre ceci en déclarant un champ calculé avec le même nom que la ListField dans la DataSource du DataSet qui ne fait rien. | |
| 91 | + | ||
| 92 | + | ## Contrôle alternatif | |
| 93 | + | ||
| 94 | + | Les contrôles Rx (dans le paquet RxNew) ont le RxDBLookupCombobox qui dispose de fonctionnalités supplémentaires ; il permet p.ex. d'afficher de multiples champs/colonnes à côté l'un de l'autre (un peu comme les ComboBox MS Access) : | |
| 95 | + | ||
| 96 | + | ``` pascal | |
| 97 | + | RxDBLookupCombo.LookupDisplay = 'field1;field2'; //takes a semicolon-delimited list of fields | |
| 98 | + | ``` | |
| 99 | + | ||
| 100 | + | Il a en plus une propriété DisplayAllFields (soit afficher tous les champs). | |
| 101 | + | ||
| 102 | + | ## Voir aussi | |
| 103 | + | ||
| 104 | + | - [[doc:lcl/dbctrls/tdblookupcombobox.html|Doc. TDBLookupComboBox]] | |
| 105 | + | - [[TComboBox/fr|TComboBox]] | |
| 106 | + | - [[TDBComboBox/fr|TDBComboBox]] | |
| 107 | + | ||
| 108 | + | --- | |
| 109 | + | *From the Free Pascal wiki, page "TDBLookupComboBox/fr", as it stood on 2020-02-29 after a change by Trev, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).* |
