[SOLVED] How to get a dropdown history für TEdit and TLabeledEdit? lazarus / 31-LCL ---------------------------------------------------------------------- Hartmut · 2016-10-21 15:22 I want to improve some of the TEdit's and TLabeledEdit's in my program with a dropdown history capability, but I found no Property or Control for that. Is there an easy way to realize that? The history shall not be forgotton at the next program start (I have already a TIniPropStorage for a couple of items of Form1). Thanks in advance. ---------------------------------------------------------------------- Devstructor · 2016-10-21 15:34 Basically you could start with some simple components. It depends on your wanted complexity. I guess the simplest solution would be a PopupMenu. A more elegant way would be a Form. Check out some Form properties like FormStyle := fsSystemStayOnTop Maybe the code completion of SynEdit is a good example. Check out the class TSynBaseCompletionForm (unit SonCompletion). Maybe you find something interesting, especially in the constructor. Greetings Miguel ---------------------------------------------------------------------- molly · 2016-10-21 15:53 For a solution with popupmenu you could pick a TEditbutton. Perhaps more logical would be to use a combobox ? ---------------------------------------------------------------------- Hartmut · 2016-10-21 16:23 I don't understand the answer from Devstructor at all. I am a beginner to Lazarus and GUI programming, sorry... I don't understand how a PopupMenu could help. I want that a history of all entered values to a TEdit or TLabeledEdit control is automatically built. A ComboBox looks related to that what I want, but I found no automatism for that. Is it really neccessary to code everything by your own: looking if an item exists already? If not, add it to the list? If a maximum is reached, delete the oldest one? Delete an item if the user presses DEL or CTRL-D? And that for every TEdit and every TLabeledEdit? Is there not ready-out-of-the-box-solution (Property or Control) for such a common problem? ---------------------------------------------------------------------- Bart · 2016-10-21 17:10 No, there is not. My mrulists unit can be used as a tool that helps you with the adding/deleting etc. stuff. Just delete al the stuff that deals with "normalizing" filenames. Use it to populate the items of a TComboBox. It has methods for saving to file or to windows registry. It's LGPL so modify like you feel fit. Bart ---------------------------------------------------------------------- molly · 2016-10-21 17:11 Quote from: Hartmut on October 21, 2016, 06:23:54 pmI don't understand how a PopupMenu could help. I want that a history of all entered values to a TEdit or TLabeledEdit control is automatically built. A ComboBox looks related to that what I want, but I found no automatism for that. Is it really neccessary to code everything by your own: looking if an item exists already? If not, add it to the list? If a maximum is reached, delete the oldest one? Delete an item if the user presses DEL or CTRL-D? And that for every TEdit and every TLabeledEdit? Is there not ready-out-of-the-box-solution (Property or Control) for such a common problem? I am currently not aware of any out of the box components that can do that for you (i am sure they exist). A popup menu could help by using something like a MRU (Most Recently Used) list (it might even be there is such a menu component available by default). You could then attach it to the tbuttonedit button to pop up the menu for the user to select form. So yes, the solutions i am aware of requires you to do some programming  on your own. I'll have a look around and see if there is something available for Lazarus or perhaps another forum member knows of such a component. ---------------------------------------------------------------------- RAW · 2016-10-21 18:11 QuoteIs there not ready-out-of-the-box-solution... Sure there is a ready to use easy solution -> TStringlist... ---------------------------------------------------------------------- ASerge · 2016-10-21 18:52 Quote from: Hartmut on October 21, 2016, 05:22:34 pmIs there an easy way to realize that? Quote from: molly on October 21, 2016, 05:53:16 pmPerhaps more logical would be to use a combobox ? Quote from: Hartmut on October 21, 2016, 06:23:54 pmA ComboBox looks related to that what I want, but I found no automatism for that. If not, add it to the list? If a maximum is reached, delete the oldest one? Delete an item if the user presses DEL or CTRL-D? Yes, this is a simple case. You're too lazy to write code? Here is a piece of (adapted part of my Delphi code) a little harder if you don't like the combobox. Only under Windows. Component, of course, you can register and use during the design time. 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 // Copyright © ASerge unit uEditExtensions; {$mode objfpc}{$H+} // Edit box with auto complete drop-down list// Support 4 sources for auto complete list. Example: custom strings// GUI: for apply selected item from drop-down list press Enter (or standard Ctrl+Enter) interface uses Classes, Controls, StdCtrls, ShlObj, LMessages; type  TAutoCompleteSource = (    acsFileSystem,  // File system and shell (Desktop, Control Panel, etc)    acsHistory,     // User's History list    acsMRU,         // User's Recently Used list  acsItems);      // Self Items   TAutoCompleteSources = set of TAutoCompleteSource;   { TAutoCompleteEdit }   TAutoCompleteEdit = class(TEdit)  private    FAutoCompleteEnabled: Boolean;    FAutoCompleteObject: IAutoComplete2;    FAutoCompleteSources: TAutoCompleteSources;    FItems: TStrings;    procedure CMWantSpecialKey(var Msg: TCMWantSpecialKey); message CM_WANTSPECIALKEY;    procedure InitAutoComplete;    procedure ItemsChanged(Sender: TObject);    procedure SetAutoCompleteEnabled(const Value: Boolean);    procedure SetAutoCompleteSources(const Value: TAutoCompleteSources);    procedure SetItems(const Value: TStrings);  protected    procedure CreateWnd; override;    procedure DestroyWnd; override;  public    constructor Create(AOwner: TComponent); override;  published    property AutoCompleteEnabled: Boolean read FAutoCompleteEnabled      write SetAutoCompleteEnabled default True;    property AutoCompleteSources: TAutoCompleteSources read FAutoCompleteSources    write SetAutoCompleteSources default [acsFileSystem];    property Items: TStrings read FItems write SetItems;  end; implementation uses Windows, ActiveX, ComObj, SysUtils; type  // From ShlObj.h  IObjMgr = interface(IUnknown)  ['{00BB2761-6A77-11D0-A535-00C04FD7D062}']    function Append(punk: IUnknown): HRESULT; stdcall;    function Remove(punk: IUnknown): HRESULT; stdcall;  end;   // From ShObjIdl.h  IAutoCompleteDropDown = interface(IUnknown)    ['{3CD141F4-3C6A-11d2-BCAA-00C04FD929DB}']    function GetDropDownStatus(out pdwFlags: DWORD;      ppwszString: PPWideChar): HRESULT; stdcall;    function ResetEnumerator: HRESULT; stdcall;  end;   // TStrings with support of IEnumString  TEnumString = class(TInterfacedObject, IEnumString)  private    FList: TStrings;    FCurrent: Integer;  private    function Next(celt: ULong; out elt; Out pceltFetched: ULong): HRESULT; stdcall;    function Skip(celt: ULONG): HRESULT; stdcall;    function Reset: HRESULT; stdcall;    function Clone(out enm: IEnumString): HRESULT; stdcall;  public    constructor Create(AList: TStrings);  end; { TAutoCompleteEdit } constructor TAutoCompleteEdit.Create(AOwner: TComponent);var  L: TStringList;begin  inherited;  ControlStyle := ControlStyle - [csSetCaption];  L := TStringList.Create;  L.Sorted := True;  L.Duplicates := dupIgnore;  L.OnChange := @ItemsChanged;  FItems := L;  FAutoCompleteSources := [acsFileSystem];  SetAutoCompleteEnabled(True);end; procedure TAutoCompleteEdit.CMWantSpecialKey(var Msg: TCMWantSpecialKey);   function AutoSuggestOpened(const BaseInterface: IUnknown): Boolean;  const    ACDD_VISIBLE = 1;  var    Wnd: HWND;    DropDown: IAutoCompleteDropDown;    Flags: DWORD;  begin    if Supports(BaseInterface, IAutoCompleteDropDown, DropDown) then    begin      if DropDown.GetDropDownStatus(Flags, nil) = S_OK then      begin        Result := (Flags = ACDD_VISIBLE);        Exit;      end;    end;    Wnd := FindWindow('Auto-Suggest Dropdown', nil); // Empirical name    Result := (Wnd <> 0) and IsWindowVisible(Wnd);  end; begin  inherited;  if (Msg.CharCode = VK_RETURN) and AutoSuggestOpened(FAutoCompleteObject) then    Msg.Result := 1;end; procedure TAutoCompleteEdit.CreateWnd;begin  inherited;  if FAutoCompleteEnabled then    InitAutoComplete;end; procedure TAutoCompleteEdit.DestroyWnd;begin  inherited;  FAutoCompleteObject := nil;end; procedure TAutoCompleteEdit.InitAutoComplete;type  TAllSources = array [TAutoCompleteSource] of IUnknown;   function InitAllSources(var All: TAllSources; UseSet: TAutoCompleteSources): Integer;  const    CPredefinedSources: array [Low(TAutoCompleteSource)..Pred(      High(TAutoCompleteSource))] of TGUID = ( // From ShlGuid.h        '{03C036F1-A186-11D0-824A-00AA005B4383}',  // CLSID_ACListISF        '{00BB2764-6A77-11D0-A535-00C04FD7D062}',  // CLSID_ACLHistory        '{6756A641-DE71-11D0-831B-00AA005B4383}'); // CLSID_ACLMRU  var    iSource: TAutoCompleteSource;  begin    Result := 0; // Result = count of sources    for iSource := Low(CPredefinedSources) to High(CPredefinedSources) do      if iSource in UseSet then      begin        All[iSource] := CreateComObject(CPredefinedSources[iSource]);      Inc(Result);      end;    if acsItems in UseSet then    begin    All[acsItems] := TEnumString.Create(FItems);      Inc(Result);    end;  end;   function CreateSourceFromMulti(const All: TAllSources): IUnknown;  const    // From ShlGuid.h    CLSID_ACLMulti: TGUID = '{00BB2765-6A77-11D0-A535-00C04FD7D062}';  var    i: TAutoCompleteSource;    ObjMgr: IObjMgr;  begin    ObjMgr := CreateComObject(CLSID_ACLMulti) as IObjMgr;    for i := Low(All) to High(All) do      if All[i] <> nil then        ObjMgr.Append(All[i]);    Result := ObjMgr;  end;   function CreateSourceFromFirstNotNil(const All: TAllSources): IUnknown;  var    i: TAutoCompleteSource;  begin  for i := Low(All) to High(All) do      if All[i] <> nil then      begin        Result := All[i];        Exit;      end;  end; var  AllSources: TAllSources;  Source: IUnknown;begin  case InitAllSources(AllSources, FAutoCompleteSources) of    0:      Exit;  1:      Source := CreateSourceFromFirstNotNil(AllSources);  else    Source := CreateSourceFromMulti(AllSources);  end;  FAutoCompleteObject := CreateComObject(CLSID_AutoComplete) as IAutoComplete2;  OleCheck(FAutoCompleteObject.SetOptions(ACO_AUTOSUGGEST));  OleCheck(FAutoCompleteObject.Init(Handle, Source, nil, nil));end; procedure TAutoCompleteEdit.ItemsChanged(Sender: TObject);begin  if FAutoCompleteEnabled and (acsItems in FAutoCompleteSources) and    HandleAllocated then  begin    RecreateWnd(Self);  end;end; procedure TAutoCompleteEdit.SetAutoCompleteEnabled(const Value: Boolean);begin  if FAutoCompleteEnabled <> Value then  begin    FAutoCompleteEnabled := Value;    if HandleAllocated then      RecreateWnd(Self);  end;end; procedure TAutoCompleteEdit.SetAutoCompleteSources(const Value: TAutoCompleteSources);begin  if FAutoCompleteSources <> Value then  begin    FAutoCompleteSources := Value;    if FAutoCompleteEnabled and HandleAllocated then      RecreateWnd(Self);  end;end; procedure TAutoCompleteEdit.SetItems(const Value: TStrings);begin  FItems.Assign(Value);end; { TEnumString } function TEnumString.Clone(out enm: IEnumString): HRESULT; stdcall;begin  enm := TEnumString.Create(FList);  Result := S_OK;end; constructor TEnumString.Create(AList: TStrings);begin  inherited Create;  assert(AList <> nil);  FList := AList;end; function TEnumString.Next(celt: ULong; out elt; Out pceltFetched: ULong): HRESULT; stdcall;var  Fetched: Integer;begin  if (celt < 0) or (Pointer(elt) = nil) then  begin    Result := E_INVALIDARG;    Exit;  end;  Fetched := 0;  while (Fetched < celt) and (FCurrent < FList.Count) do  begin    TOleStrList(elt)[Fetched] := StringToLPOLESTR(FList[FCurrent]);    Inc(FCurrent);    Inc(Fetched);  end;  if @pceltFetched <> nil then    pceltFetched := Fetched;  if Fetched = celt then    Result := S_OK  else    Result := S_FALSE;end; function TEnumString.Reset: HRESULT; stdcall;begin  FCurrent := 0;  Result := S_OK;end; function TEnumString.Skip(celt: ULONG): HRESULT; stdcall;begin  if celt < 0 then  begin    Result := E_INVALIDARG;    Exit;  end;  Inc(FCurrent, celt);  if FCurrent < FList.Count then    Result := S_OK  else  begin    FCurrent := FList.Count;    Result := S_FALSE;  end;end; end.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 Unit1; {$mode objfpc}{$H+} interface uses  Forms, StdCtrls, Classes; type  { TForm1 }   TForm1 = class(TForm)    procedure FormCreate(Sender: TObject);  private    procedure EditDone(Sender: TObject);  public    { public declarations }  end; var  Form1: TForm1; implementation uses SysUtils, uEditExtensions; {$R *.lfm} { TForm1 } procedure TForm1.FormCreate(Sender: TObject);var  E: TAutoCompleteEdit;begin  E := TAutoCompleteEdit.Create(Self);  E.Left := 8;  E.Top := 8;  E.AutoCompleteSources := [acsItems];  E.Items.Append('First');  E.Items.Append('Second');  E.Parent := Self;  E.OnEditingDone := @EditDone;end; procedure TForm1.EditDone(Sender: TObject);var  E: TAutoCompleteEdit;begin  E := Sender as TAutoCompleteEdit;  E.Items.Append(Trim(E.Text));end; end.Oh yes, there's no code to manage the list of strings. Well have to do something yourself, it's much easier . ---------------------------------------------------------------------- wp · 2016-10-21 19:27 In the attachment you can find a little demo which uses a combobox instead of a TEdit. Using a view lines of code you can modifiy it to store the history of entered strings up to a given max count. Look at unit "historycombo" first. It implements a new TCombobox with history capabilities - whenever the focus exits the combo the current text is stored at the top of its Items list (unless it is already stored). If there are more than a given number of items the last item is deleted. Note that the new combobox class name is exactly like the standard one in unit StdCtrls. This has the advantage that you can use the standard form designer to layout your form. In order to activate the history of the combobox you must add "historycombo" to the end of the uses list of your program - this way the new combo will replace the old one at runtime. Of course you can give the new combobox class a different name, such as THistoryCombo, but then you have to create the combobox at runtime (or add it to a package and install it into the IDE) The unit historycombo contains also two simple procedures which you can use to store the current combobox items in an ini file (call this before the form closes), and to retrieve them from the ini file (to be called in FormCreate, for example). See the little attached project: type something into the combobox, focus another control. Drop the combobox down to see the typed text in the list. Close the program and re-run; drop the combo again to see the saved strings. If don't want to see the dropdown arrow of the combobox set its style to csSimple - then the combobox will exactly look like a TEdit. ---------------------------------------------------------------------- Hartmut · 2016-10-21 20:10 I started this topic because I wanted not to reinvent the wheel. I learned, that there is no ready-to-use-solution from the LCL by Properties or a Control. I tried to understand the suggestions from Molly and ASerge to extract from them what I need, but this was to difficult for me as a beginner in Lazarus. The solution from wp is much smaller and easier and I think I understood it. Ingenious to make a class with an existing name so you can a) use the standard form designer to layout your form and b) every ComboBox you use has the history-feature automatically without any extra effort :-)) Thanks a lot to everybody for your help!