[SOLVED] 3 items in Combobox lazarus / 16-Databases ---------------------------------------------------------------------- madref · 2015-04-02 20:37 With this code i can add 2 items to 1 row in a combobox. Code: [Select]ComboBox_RefereeLicentie.Items.AddObject(lkq.FieldByName ('License').AsString,     TObject(lkq.FieldByName('Licentie_ID').AsInteger));Is it also possible to add 3 items to 1 row? ---------------------------------------------------------------------- typo · 2015-04-02 20:55 Yes, if the object associated to the item has 2 or more properties or something. ---------------------------------------------------------------------- madref · 2015-04-02 21:13 Yes but how to do that? ---------------------------------------------------------------------- Blaazen · 2015-04-02 21:23 Code: [Select]var aSL: TStringList; begin   aSL:=TStringList.Create;   aSL.Items.Add('aaaa');   aSL.Items.Add('bbbb');   ........   aSL.Items.Add('zzzz');   ComboBox_RefereeLicentie.Items.AddObject(lkq.FieldByName('License'). AsString, aSL); Do not forget to free all string lists to avoid memory leaks. ---------------------------------------------------------------------- taazz · 2015-04-02 21:43 Code: [Select]procedure AddStringListItems(cosnt aList:TstringList; aTitle:string; aItems:array of Tobject); var   vObjList:TObjectList;   vCntr:integer; begin   vObjList = TOBjectList.Create;   //vObjList.OwnsObjects := true; //make sure  you want the objects to be destroyed when the list is destroyed or set it to false to leave them in memory.   for vCntr := low(aItems) to High(aItems) do begin     vObjList.Add(aItems[vCntr]);   end;   aList.addobject(aTitle, vObjtList); end; ---------------------------------------------------------------------- madref · 2015-04-02 22:29 @taazz I really don't understand this code. how to implement it? ---------------------------------------------------------------------- taazz · 2015-04-02 22:40 you call it like Code: [Select] AddStringListItems(comboBox_RefereeLicentie, lkq.FieldByName('License').AsString, [TObject(lkq.FieldByName('Licentie_ID').AsInteger), TObject(lkq.FieldByName('Licentie_taxID').AsInteger), TObject(lkq.FieldByName('Licentie_AddressNo').AsInteger)]); you can have as many integers as you like inside []. Oh by the way make sure that you set ownsObject to false. And you retreive the items like Code: [Select]var   vTmp1, vtmp2, vtmp3 : integer; begin //                                                                                                             |-- this is the position of the field inside the [];   vTmp1 := integer(comboBox_RefereeLicentie.Items.objects[0].objects[0]); //TObject(lkq.FieldByName('Licentie_ID').AsInteger);   vTmp2 := integer(comboBox_RefereeLicentie.Items.objects[0].objects[1]); //TObject(lkq.FieldByName('Licentie_taxID').AsInteger);   vTmp3 := integer(comboBox_RefereeLicentie.Items.objects[0].objects[2]); //TObject(lkq.FieldByName('Licentie_AddressNo').AsInteger); end; ---------------------------------------------------------------------- mangakissa · 2015-04-03 08:37 @taaz if the stored fields are the same for al items, why using a complex optio like an objectlist in an oblect? @madref I'm agreed with Blaazen, but I shall create a own class for storing the values for each item. Code: [Select]//example for storing data //integer and boolean can be changed to any declaration interface type TMyCBItemsData = class   fValue1 : string;   fValue2 : integer;   fValue3 : boolean; end; inplenmentation function tform1.PutItemToCombobox(aComboItem : string; aString : string; aInteger : integer; aBoolean) : boolean; var aData : TMyCBItemsData; begin   result := true;   try     aData  := TMyCBItemsData.create;     aData.FValue1 := aString;     aData.FValue2 := aInteger;     aData.FValue3 := aBoolean;     Combobox1.items.AddObject(aComboItem,aData);   except     result := false;   end; end; procedure tform1.GetObjectData(aItems : TStrings; const aIndex : integer ) ; var aData : TMyCBItemsData; begin   try     aData  := TMyCBItemsData(aItems.objects[aIndex]);     showmessage(aData.FValue1);     showmessage(IntToStr(aData.FValue2));     showmessage(BoolToStr(aData.FValue3,true));   except     showmessage('error in object')   end; end; procedure TForm1.ComboBox1CloseUp(Sender: TObject); begin   GetObjectData(combobox1,items, combobox.itemindex) end; ---------------------------------------------------------------------- typo · 2015-04-03 09:25 aData returns nil for me on this line: Code: [Select]aData  := TMyCBItemsData(aItems.objects[aIndex]); ---------------------------------------------------------------------- mangakissa · 2015-04-03 13:07 Are you sure. There was a type error in my example Code: [Select]GetObjectData(combobox1.items, combobox.itemindex) //, must be .I tested it and it works like a charm. What do you see if you're debugging the procedure GetObjectData? ---------------------------------------------------------------------- madref · 2015-04-05 14:05 But will they also be visible in the combobox? ---------------------------------------------------------------------- mangakissa · 2015-04-05 17:43 No. TListbox has a property column which is showing multiple item separated by ^|. Tcombobox doesn't. This is a trick Code: [Select]items.add(format ('%10s %10s %10s',[item1, item2, item3])) But why are you using AddObject in your example? ---------------------------------------------------------------------- madref · 2015-04-06 20:11 I used the addObject in Delphi XE7 (excuse me for using it RVK) because i had to. P.S. i am getting frustrated with this problem, because I can't get it to work how I want it to ---------------------------------------------------------------------- skalogryz · 2015-04-06 20:25 Quote from: madref on April 02, 2015, 10:37:53 pmWith this code i can add 2 items to 1 row in a combobox. ... Is it also possible to add 3 items to 1 row? but you're already storing Licentie_ID. You could just run the query based of that ID to get more information, when combo box selection changes. Btw, you not adding 2 items to 1 row. You're specifying the text for the row and adding exactly 1 item to a row. ---------------------------------------------------------------------- mangakissa · 2015-04-07 07:03 Looking at your first post you said QuoteWith this code i can add 2 items to 1 row in a combobox.That's not true. You're placing one item on screen and another item will be stored in an object as invisible data. The solution to store more data in your 'item' is givven on post #3, #4, #6 and #7. QuoteBut will they also be visible in the combobox?This answer is put in post #11. Now you get frustrated, because you don't get what you want. Maybe you have to create an example for us for showing what you really want with a lot of comments. ---------------------------------------------------------------------- rvk · 2015-04-07 07:57 Quote from: madref on April 06, 2015, 10:11:57 pmI used the addObject in Delphi XE7 (excuse me for using it RVK) because i had to.I don't mind you using Delphi XE7 (I use Delphi XE myself daily) but you kept saying Delphi was (way) more powerful, which it isn't. It's just different on some points. (And some point a user might perceive as powerful while it's just a language add-on or function which is easy to program in FPC too.) On the point of your Combobox I agree with mangakissa. You might want to restate your questions with a lot of comment for what you really want because this seems more and more like a XY-Problem to me. You're trying to do something with the combobox (for which it's not designed) while there are (probably) much easier solutions for your problem. If you goal of storing those foreign ID's linking to another table (like Licentie_ID) in the combobox is for retrieving the details when you choose a different value in the combobox then there are easier ways to do this. Like master/details relationships between your datasets. In that case when you choose another value in your combobox all the details will be automatically retrieved in the other datasets so you don't need to do that manually. You only said you wanted 3 items in the combobox per row (for which the solution is shown here in the answers) but you didn't explain why. ---------------------------------------------------------------------- madref · 2015-04-07 09:18 So let's start over with my initial problem: I want to store the following items in one row in the combobox (if possible): License_ID, License_Name, License_Explanation. All three are store in the table tbl_License_Choice AND I want to show only the later two (License_Name and License_Explanation). That is what i am trying to do. P.S. 1:I am trying to rebuild my MS Access database to a program. Because Apple hasn't got a substitute for MS Access and i do need the data that's in this system. P.S. 2: I know i can make a virtual machine on a mac or a dual boot which runs windows. But thats not what i want. ---------------------------------------------------------------------- rvk · 2015-04-07 09:48 You still didn't say WHY you want the License_ID in the combobox. If it's to do a lookup when the user chooses a value, there are better ways to do this (i.e. TDBLookupComboBox in Unbound use). If you keep a dataset open for License-dataset and you bind other dataset (i.e. Wedstrijden etc.) to it so you don't even have to program a single line to get the complete master/details-relationship you're after. But before we can recommend how you need to do that we need to see exactly what kind of userinterface you're creating (by screenshots or clear description). But ok. If you just want to do all the programming yourself you could just do this: Code: [Select]ComboBox_RefereeLicentie.Items.AddObject(   lkq.FieldByName('License_Name').AsString + ' ' + lkq.FieldByName('License_Explanation').AsString,   TObject(lkq.FieldByName('Licentie_ID').AsInteger)); In that case you have the License_Name and License_Explanation in the dropdown and the Licentie_ID in the object. ---------------------------------------------------------------------- madref · 2015-04-07 10:49 Wow was it THAT simple