Lazarus

Programming => LCL => Topic started by: xinyiman on July 15, 2010, 09:53:58 am

Title: TComboBox multiple columns
Post by: xinyiman on July 15, 2010, 09:53:58 am
How can a TComboBox use two or more columns in one row?
Title: Re: TComboBox multiple columns
Post by: JuhaManninen on July 15, 2010, 11:06:26 am
How can a TComboBox use two or more columns in one row?

This seems to work:
Code: [Select]
  ComboBox1.Items.Add('First '#9' item');
  ComboBox1.Items.Add('Second '#9' item');
  ComboBox1.Items.Add('Third '#9' item');

Tested with GTK2 and QT bindings, Linux, Lazarus trunk.
Some 3rd party components may have more advanced features.

Juha
Title: Re: TComboBox multiple columns
Post by: xinyiman on July 15, 2010, 11:24:58 am
Windows not working properly, but then I wish the lines were aligned in various fields! is it possible? View image!
Title: Re: TComboBox multiple columns
Post by: JuhaManninen on July 15, 2010, 11:46:02 am
Windows not working properly, but then I wish the lines were aligned in various fields! is it possible? View image!

Does it happen also with the Lazarus 0.9.29 trunk version on Windows?

Juha
Title: Re: TComboBox multiple columns
Post by: zeljko on July 15, 2010, 04:39:02 pm
Under qt it's possible by adding another list class (TQtTreeWidget instead of TQtListWidget) and set number of columns, but it's hack.
Normally TComboBox does not have multicolumn support, but you can use your own drawn combobox and format lines as you wish.
Title: Re: TComboBox multiple columns
Post by: fredycc on July 16, 2010, 02:23:43 am
Hi zeljko, I have test it sucessfully under windows only , i'm sure the others platforms, check it:

http://www.lazarus.freepascal.org/index.php/topic,9397.msg46031.html#msg46031
Title: Re: TComboBox multiple columns
Post by: Jordi March on March 08, 2019, 06:08:58 pm
I make a component (TComboBoxLkpDB), a ComboBox from TCustomEditButton. A dataset (with the content of columns) is required; for simple programs you can use a table in memory, for example, TBufDataset.
You can download, provisionally, from my wiki: http://saniwiki.cat/lazarus/ListBoxLkpDB.zip (http://saniwiki.cat/lazarus/ListBoxLkpDB.zip).
It contains 3 new components (TBufDatasetSort, TComboBoxLkpDB and TDBComboBoxLkpDB; that you can install in a new package) and one example.
Title: Re: TComboBox multiple columns
Post by: howardpc on March 08, 2019, 08:04:09 pm
@ Jordi March
Thanks for sharing. However it needs more work.
You should provide a package (.lpk) for the components, otherwise other users will most likely not get paths set correctly.
Likewise, you should provide a project file (.lpi) for any example program, particularly a complex GUI such as your example, so that paths etc. are set up correctly.

A .lpr is sufficient for, say, a simple console example, but not for the sort of example you give.
You forgot to include the strutils0 unit which your example references, so it does not compile without amendment.

However, since only one concatenation routine from it is used, why not just use the FPC concat() function?
Also the example program you give has a memory leak at line 650 of the UpdateGridEdit routine in the ComboBoxLpkDB unit where you create columns.
Title: Re: TComboBox multiple columns
Post by: Jordi March on March 08, 2019, 11:49:02 pm
OK.
I update the zip file with the other required files. 
StrUtils0 unit was not necessary since was not included by directive; however, I deleted its reference.
But not understand your last paragraph. Could you specify it or, directly, correct it?
Title: Re: TComboBox multiple columns
Post by: Ericktux on March 09, 2019, 06:16:49 am
to load text + identifier in the combobox

Imagine that we want to load several items inside the combobox, but that each item has a number that identifies it, very apart from its INDEX, its use can be to load tables or fields of a BD + your ID.

here the codes:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. begin
  3.   ComboBox1.Items.AddObject('casa', TObject(3));  // you load the text + yours ID (3)
  4. end;

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button2Click(Sender: TObject);
  2. begin
  3.   //Id := Integer(Combobox1.Items.Objects[Combobox1.ItemIndex]);
  4.   ShowMessage(inttostr(Integer(Combobox1.Items.Objects[Combobox1.ItemIndex])));  // you obtain the ID as the elected item
  5. end;

to release the tobjects:

Code: Pascal  [Select][+][-]
  1. Combobox1.Items.Objects[ComboBox1.ItemIndex].Free


greetings my friends.  :)
Title: Re: TComboBox multiple columns
Post by: Jordi March on March 09, 2019, 08:05:07 am
Ericktux, my component (TComboBoxLkpDB) doesn't use Items (since it is inherited from TCustomEditButton). Only Datasource with its Dataset. Then you determine what fields are displayed (as columns) with the Field.Visible property and what Field is lookup (lkp). Obviously you can add or delete records in runtime.
Please, try use it and proof the example. The TComboBoxLkpDB functionality is similar to a ComboBox (with DropDown and DropDownList) and you can look up for other field types than the ftString or ftWideString (i.e. ftSmallint).
Title: Re: TComboBox multiple columns
Post by: howardpc on March 09, 2019, 11:28:10 am
But not understand your last paragraph. Could you specify it or, directly, correct it?
You must specifically free any previous TGridCol instances created, before clearing the list. Otherwise they are orphaned by FGridCols.Clear, and leak memory that is then inaccessible.
Add a couple of lines as follows:
Code: Pascal  [Select][+][-]
  1. procedure TComboBoxLkpDB.UpdateGridEdit;
  2. var
  3.   I: Integer;
  4.   lDlgUnits: Integer;
  5.   GridCol: TGridCol;
  6.   PriorFont: TFont;
  7. begin
  8.   for I := 0 to FGridCols.Count-1 do             //< --- add these lines
  9.     TGridCol(FGridCols.Objects[I]).Free;
  10.   FGridCols.Clear;
  11.   if not Assigned (FLkpDataLink.Field) then Exit;    ...
Title: Re: TComboBox multiple columns
Post by: Jordi March on March 09, 2019, 08:34:02 pm
I change it. The new version, also in http://saniwiki.cat/lazarus/ListBoxLkpDB.zip (http://saniwiki.cat/lazarus/ListBoxLkpDB.zip).
I had forgotten it, due to misunderstanding:
The Lazarus explanation (https://www.freepascal.org/docs-html/rtl/classes/tstrings.clear.html (https://www.freepascal.org/docs-html/rtl/classes/tstrings.clear.html)) says "Clear will remove all strings and their associated objects from the list".
The Delphi explanation is better (http://docs.embarcadero.com/products/rad_studio/delphiAndcpp2009/HelpUpdate2/EN/html/delphivclwin32/Classes_TStringList_Clear.html (http://docs.embarcadero.com/products/rad_studio/delphiAndcpp2009/HelpUpdate2/EN/html/delphivclwin32/Classes_TStringList_Clear.html)): "All references to associated objects are also removed. However, the objects themselves are not freed."
Then I think the Lazarus help should be modified to avoid errors...
Finally, the Lazarus compiler didn't (before the last correction) detect a memory leak (unlike the demo http://forum.lazarus.freepascal.org/index.php?action=dlattach;topic=31598.0;attach=14618 (http://forum.lazarus.freepascal.org/index.php?action=dlattach;topic=31598.0;attach=14618)). Why?
Very thank you.
Jordi.
Title: Re: TComboBox multiple columns
Post by: xinyiman on March 11, 2019, 08:45:06 am
Error, not found StrUtil0 library.
Title: Re: TComboBox multiple columns
Post by: Jordi March on March 11, 2019, 11:21:25 am
Sorry, solved! :-\
Title: Re: TComboBox multiple columns
Post by: xinyiman on March 11, 2019, 02:30:30 pm
Now not exist lpk file
Title: Re: TComboBox multiple columns
Post by: Jordi March on March 13, 2019, 05:17:26 pm
Solved!. An error on packing the files. %).  I add xml files for help in IDE integration... but I do not know how to make them work.
TinyPortal © 2005-2018