TTICombobox Binding with TStringList lazarus / 31-LCL ---------------------------------------------------------------------- Weitentaaal · 2022-08-25 07:23 Hello, i recently was searching for an example but could not find anything. Then i found this thread: https://forum.lazarus.freepascal.org/index.php?topic=46876.0 but it seems like no one knew about it/ or it just didn't work. I just wanted to ask if its Possible to Bind "Strings" Property with the Combobox. I have no idea how this could work, but i would like to bind my Stringlist to the combobox. Do i have to use an Enum for that or can i continue using my Stringlist. And i Have String Pairs in my List so i would have to use Names Property. Is this possible ? ---------------------------------------------------------------------- jamie · 2022-08-26 01:41 Why not simply use the TheCOmboBox.ITEMS.Assign(YourStringList); ---------------------------------------------------------------------- Weitentaaal · 2022-08-29 07:16 Quote from: jamie on August 26, 2022, 03:41:44 amWhy not simply use the TheCOmboBox.ITEMS.Assign(YourStringList); Heythanks, works ! But i have String Pairs. Do i have to override the Function or is there already a way to Handle  that ? ---------------------------------------------------------------------- dje · 2022-08-29 11:09 Assuming you have a TStringList containing name/value pairs, and you want a TComboBox showing just the Values, while being able to get the selected key: This will setup the values in the TComboBox: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure ComboBoxSetValues(AComboBox: TCustomComboBox; AStrings: TStrings);var LIndex: integer;begin  AComboBox.Clear;  AComboBox.Tag := PtrInt(AStrings);  for LIndex := 0 to AStrings.Count - 1 do begin    AComboBox.Items.Add(AStrings.ValueFromIndex[LIndex]);  end;end;   And, this will return the selected key: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function ComboBoxGetSelectedKey(AComboBox: TComboBox): string;begin  if AComboBox.ItemIndex < 0 then begin    Result := EmptyStr;  end else begin    Result := (TObject(AComboBox.Tag) as TStrings).Names[AComboBox.ItemIndex];  end;end; Call ComboBoxSetValues() in the TComboBox.OnGetItems event, and ComboBoxGetSelectedKey() in TComboBox.OnSelect. I have a feeling I know what you were hoping for. As far as I can tell, there is no way to intercept access to the internal TStrings class. At line 856 of customcombobox.inc a TStringListUTF8Fast is created with no way to intercept. There is also no way to intercept FItems access either, since all code accesses the private FItems field directly or the via Items property (indirectly). Ideally, the class would provide a virtual CreateStings method with which you could override and provide your own magic TStrings derived class. On top of that, TString's doesn't provide a way to intercept access either. It provides a 'get' abstract method, which TStringList thens overides and access its private Flist^[Index].FString data. ie: No way to intercept to filter strings. So, no, I dont see a way to intercept item access to filter what actually gets shown in the TComoBox. That said, you can use ownerdraw, which is what TComboBoxEx does, but then you are limited to csOwnerDrawFixed. Which means, no more text selection editing. If you look in TCustomComboBox.DrawItem, you find a call to InternalDrawItem. You can copy InternalDrawItem out, write your own super class and change the call from: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i InternalDrawItem(Self, FCanvas, ARect, Items[Index]) to Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i InternalDrawItem(Self, Canvas, ARect, ValueFromIndex[Index]) This creates a TComboBox class which will display only the Values of the internal Items. But, again, no editing. I don't see a clean solution to create filtered TComboBox.Items, without adding a virtual CreateStings method. I'm sure I can come up with more hackery. But thats all I have ATM. Feature request? A CreateStings() method with matching OnCreateStrings for TCustomComboBox? ---------------------------------------------------------------------- jamie · 2022-08-29 14:08 If u use a ownerdraw combo box then you draw the text for each item as the box is getting painted  This comes into play with the ondraw event at which point all info is provided to allow you to customize the display per index.  Simply copy the part you want displayed for the textout. ---------------------------------------------------------------------- Weitentaaal · 2022-08-30 06:16 Quote from: dje on August 29, 2022, 01:09:39 pmAssuming you have a TStringList containing name/value pairs, and you want a TComboBox showing just the Values, while being able to get the selected key: This will setup the values in the TComboBox: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure ComboBoxSetValues(AComboBox: TCustomComboBox; AStrings: TStrings);var LIndex: integer;begin  AComboBox.Clear;  AComboBox.Tag := PtrInt(AStrings);  for LIndex := 0 to AStrings.Count - 1 do begin    AComboBox.Items.Add(AStrings.ValueFromIndex[LIndex]);  end;end;   And, this will return the selected key: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function ComboBoxGetSelectedKey(AComboBox: TComboBox): string;begin  if AComboBox.ItemIndex < 0 then begin    Result := EmptyStr;  end else begin    Result := (TObject(AComboBox.Tag) as TStrings).Names[AComboBox.ItemIndex];  end;end; Call ComboBoxSetValues() in the TComboBox.OnGetItems event, and ComboBoxGetSelectedKey() in TComboBox.OnSelect. I have a feeling I know what you were hoping for. As far as I can tell, there is no way to intercept access to the internal TStrings class. At line 856 of customcombobox.inc a TStringListUTF8Fast is created with no way to intercept. There is also no way to intercept FItems access either, since all code accesses the private FItems field directly or the via Items property (indirectly). Ideally, the class would provide a virtual CreateStings method with which you could override and provide your own magic TStrings derived class. On top of that, TString's doesn't provide a way to intercept access either. It provides a 'get' abstract method, which TStringList thens overides and access its private Flist^[Index].FString data. ie: No way to intercept to filter strings. So, no, I dont see a way to intercept item access to filter what actually gets shown in the TComoBox. That said, you can use ownerdraw, which is what TComboBoxEx does, but then you are limited to csOwnerDrawFixed. Which means, no more text selection editing. If you look in TCustomComboBox.DrawItem, you find a call to InternalDrawItem. You can copy InternalDrawItem out, write your own super class and change the call from: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i InternalDrawItem(Self, FCanvas, ARect, Items[Index]) to Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i InternalDrawItem(Self, Canvas, ARect, ValueFromIndex[Index]) This creates a TComboBox class which will display only the Values of the internal Items. But, again, no editing. I don't see a clean solution to create filtered TComboBox.Items, without adding a virtual CreateStings method. I'm sure I can come up with more hackery. But thats all I have ATM. Feature request? A CreateStings() method with matching OnCreateStrings for TCustomComboBox? Thank you very much! im not realy sure if i get everything u did write but your Code did give me a push in the right direction. Quote from: jamie on August 29, 2022, 04:08:21 pmIf u use a ownerdraw combo box then you draw the text for each item as the box is getting painted  This comes into play with the ondraw event at which point all info is provided to allow you to customize the display per index.  Simply copy the part you want displayed for the textout. This did work great ! but the Text Property was still a Pair. The Items did only show the Values. i did look for another Draw event for the Text but did not find anything. ---------------------------------------------------------------------- dje · 2022-08-30 07:07 Quote from: Weitentaaal on August 30, 2022, 08:16:26 amThis did work great ! but the Text Property was still a Pair. The Items did only show the Values. i did look for another Draw event for the Text but did not find anything. ? There are a plethora of options: (Assuming ItemIndex > -1) Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   ComboBox1.Items.GetNameValue(ComboBox1.ItemIndex, LName, LValue);  ComboBox1.Items.ValueFromIndex[ComboBox1.ItemIndex];  ComboBox1.Items.Names[ComboBox1.ItemIndex]; OwnerDraw is cool, but lacks a text editor. I have another idea. Given me 10 minutes. ---------------------------------------------------------------------- Weitentaaal · 2022-08-30 07:46 i think you did not understand me correctly, please take a look at the attached Screenshot. The Code i Use: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.ComboBox5DrawItem(Control: TWinControl; Index: Integer;   ARect: TRect; State: TOwnerDrawState);begin   with Control as TComboBox do begin       Canvas.FillRect(ARect);       Canvas.TextOut(ARect.Left + 2, ARect.Top, Items.Names[Index]);   end;end;   Edit: Sorry first time i ever used a Windows Screenshot ---------------------------------------------------------------------- dje · 2022-08-30 08:01 O, right. Yes.  For some reason my eyes didn't see the csOwnerDrawEditableFixed option  csOwnerDrawFixed fixes that issue, but takes away editing. ---------------------------------------------------------------------- dje · 2022-08-30 08:03 So, ok. Back to your original request: "i would like to bind my Stringlist to the combobox". This is the only way I can think of. Use a custom proxy/adapter TStringList. USAGE: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   FMyStringList := TProxyStringList.Create(ComboBox1.Items);  FMyStringList.CommaText := 'Peter=Orange,Paul=Pear,Dave=Grapes,Bob=Apple'; I get Orange, Pear, Grapes & Apple in my TComboBox. Then if I run: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   FMyStringList.Values['Peter'] := 'Pie';  FMyStringList.Values['Frank'] := 'Pizza'; I then have "Pie, Pear, Grapes, Apple & Pizza" in my TComboBox. I think its a cool trick, but there are a number of gotchas if you arn't careful. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i unit ProxyStringList; interface uses Classes, SysUtils; type   TProxyStringList = class(TStringList)  strict private    FProxy: TStrings;  public    constructor Create(AProxy: TStrings);  protected    function FilterIndex(AIndex: integer): string; virtual;    procedure Put(AIndex: integer; const AString: string); override;  public    procedure Clear; override;    procedure InsertItem(AIndex: integer; const AString: string; AObject: TObject); override;    procedure Delete(AIndex: integer); override;  end; implementation constructor TProxyStringList.Create(AProxy: TStrings);begin  inherited Create;  Assert(Assigned(AProxy));  FProxy := AProxy;end; function TProxyStringList.FilterIndex(AIndex: integer): string;begin  Result := ValueFromIndex[AIndex];end; procedure TProxyStringList.Clear;begin  inherited;  FProxy.Clear;end; procedure TProxyStringList.Put(AIndex: integer; const AString: string);begin  inherited;  FProxy[AIndex] := FilterIndex(AIndex);end; procedure TProxyStringList.InsertItem(AIndex: integer; const AString: string; AObject: TObject);begin  inherited;  FProxy.Insert(AIndex, FilterIndex(AIndex));end; procedure TProxyStringList.Delete(AIndex: integer);begin  inherited;  FProxy.Delete(AIndex);end; end. ---------------------------------------------------------------------- Weitentaaal · 2022-08-30 08:23 Wow creative idea ! i guess this way i don't even have to use RTTI Comboboxes. i w ill  update this thread if i run into any Problems. Thank you Guys, especially @dje, for those Solutions. Glad someone could help me