Recent

Author Topic: TComboBox multiple columns  (Read 8556 times)

xinyiman

  • Hero Member
  • *****
  • Posts: 1991
    • Lazarus and Free Pascal italian community
TComboBox multiple columns
« on: July 15, 2010, 09:53:58 am »
How can a TComboBox use two or more columns in one row?
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

JuhaManninen

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3648
  • I like bugs.
Re: TComboBox multiple columns
« Reply #1 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

xinyiman

  • Hero Member
  • *****
  • Posts: 1991
    • Lazarus and Free Pascal italian community
Re: TComboBox multiple columns
« Reply #2 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!
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

JuhaManninen

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3648
  • I like bugs.
Re: TComboBox multiple columns
« Reply #3 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

zeljko

  • Hero Member
  • *****
  • Posts: 1087
    • http://wiki.lazarus.freepascal.org/User:Zeljan
Re: TComboBox multiple columns
« Reply #4 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.

fredycc

  • Sr. Member
  • ****
  • Posts: 265
Re: TComboBox multiple columns
« Reply #5 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

Jordi March

  • New member
  • *
  • Posts: 7
Re: TComboBox multiple columns
« Reply #6 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.
It contains 3 new components (TBufDatasetSort, TComboBoxLkpDB and TDBComboBoxLkpDB; that you can install in a new package) and one example.

howardpc

  • Hero Member
  • *****
  • Posts: 3198
Re: TComboBox multiple columns
« Reply #7 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.

Jordi March

  • New member
  • *
  • Posts: 7
Re: TComboBox multiple columns
« Reply #8 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?

Ericktux

  • Full Member
  • ***
  • Posts: 196
Re: TComboBox multiple columns
« Reply #9 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.  :)
« Last Edit: March 09, 2019, 06:19:05 am by Ericktux »

Jordi March

  • New member
  • *
  • Posts: 7
Re: TComboBox multiple columns
« Reply #10 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).

howardpc

  • Hero Member
  • *****
  • Posts: 3198
Re: TComboBox multiple columns
« Reply #11 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;    ...

Jordi March

  • New member
  • *
  • Posts: 7
Re: TComboBox multiple columns
« Reply #12 on: March 09, 2019, 08:34:02 pm »
I change it. The new version, also in 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) 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): "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). Why?
Very thank you.
Jordi.

xinyiman

  • Hero Member
  • *****
  • Posts: 1991
    • Lazarus and Free Pascal italian community
Re: TComboBox multiple columns
« Reply #13 on: March 11, 2019, 08:45:06 am »
Error, not found StrUtil0 library.
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

Jordi March

  • New member
  • *
  • Posts: 7
Re: TComboBox multiple columns
« Reply #14 on: March 11, 2019, 11:21:25 am »
Sorry, solved! :-\