Recent

Author Topic: Adding Multiple Columns of Data to Listbox  (Read 27942 times)

Gizmo

  • Hero Member
  • *****
  • Posts: 831
Adding Multiple Columns of Data to Listbox
« on: March 25, 2015, 04:19:11 pm »
I just know the answer to this is going to be obvious, but I can't see it!

Listbox's - they can have multiple columns. For a single column listbox, adding a string is :

Code: [Select]
var
  Firstname, Surname : string;
begin
  Firstname := 'Gizmo';
  Surname := 'Gremlin';
  listbox1.Items.Add('Candidate Name : ' + Firstname + '  ' + Surname);
end;

But I have a Listbox where I have set it's column properties to '2'. According to a Delphi page I read (http://docwiki.embarcadero.com/Libraries/XE7/en/Vcl.StdCtrls.TCustomListBox.Items), in Delph the columns are split by using the |^, e.g.

listbox1.Items.Add(Firstname |^ Surname)" 

but that doesn't work with FPC.

How do I populate Column1, Row 1 with "Firstname" and Column2, Row1 with "Surname" and so on row after row for say a list of 100 names that are in a stringlist?

(NB : please don't suggest StringGrid. FVor various reasons, I don't want to use that)

« Last Edit: March 25, 2015, 04:21:46 pm by Gizmo »

eny

  • Hero Member
  • *****
  • Posts: 1658
Re: Adding Multiple Columns of Data to Listbox
« Reply #1 on: March 25, 2015, 05:00:59 pm »
^I is the tab character (or #9).
Try to use that as a separator.
Remember to put it outside the quotes.
All posts based on: Win11; Lazarus 4_4  (x64) 12-02-2026 (unless specified otherwise...)

zeljko

  • Hero Member
  • *****
  • Posts: 1874
    • http://wiki.lazarus.freepascal.org/User:Zeljan
Re: Adding Multiple Columns of Data to Listbox
« Reply #2 on: March 25, 2015, 05:36:25 pm »
I just know the answer to this is going to be obvious, but I can't see it!

Listbox's - they can have multiple columns. For a single column listbox, adding a string is :

Code: [Select]
var
  Firstname, Surname : string;
begin
  Firstname := 'Gizmo';
  Surname := 'Gremlin';
  listbox1.Items.Add('Candidate Name : ' + Firstname + '  ' + Surname);
end;

But I have a Listbox where I have set it's column properties to '2'. According to a Delphi page I read (http://docwiki.embarcadero.com/Libraries/XE7/en/Vcl.StdCtrls.TCustomListBox.Items), in Delph the columns are split by using the |^, e.g.

listbox1.Items.Add(Firstname |^ Surname)" 

but that doesn't work with FPC.

How do I populate Column1, Row 1 with "Firstname" and Column2, Row1 with "Surname" and so on row after row for say a list of 100 names that are in a stringlist?

(NB : please don't suggest StringGrid. FVor various reasons, I don't want to use that)

Why don't U simple use TListView with Style = vsReport ?
Item := ListView.Items.Add;
Item.Caption := Name;
Item.Subitems.Add(Surname);
Item.Subitems.Add(Birthday );
 .......

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Adding Multiple Columns of Data to Listbox
« Reply #3 on: March 25, 2015, 06:31:17 pm »
Zeljko's suggestion also gives you a nice column header with style vsReport.

If you're fixed on using TListBox, you can use the OnDrawItem event with the Style set to lbOwnerDrawFixed (or lbOwnerdrawVariable) to parse each Item and redraw it in columns (I can dig out and post some old code if you want a compilable example).

Or (Windows-only solution), the windows listbox widget has built-in support for tabbed columns, so on Windows if you override the CreateParams method of TListBox you can set your own tab column widths with an appropriate Perform() method. I don't think GTK or QT support this, but I may be wrong.

Gizmo

  • Hero Member
  • *****
  • Posts: 831
Re: Adding Multiple Columns of Data to Listbox
« Reply #4 on: March 25, 2015, 06:47:17 pm »
Quote
Why don't U simple use TListView with Style = vsReport ?

I don't have that Style option. Only Standard, Virtual, OwenerDrawFixed and OwnerDrawVariable. How did you get vsReport? Newer version of Lazarus? (I using 1.2.6)

Howard - don't worry yourself to that extent. My request was more of a desirable than an essential. I had assumed it was easier than what it clearly is. I just didn't want to not split my data into columns if it was as easy as listbox1.Column[1].Add or something. But clearly it isn't.

« Last Edit: March 25, 2015, 06:50:07 pm by Gizmo »

zeljko

  • Hero Member
  • *****
  • Posts: 1874
    • http://wiki.lazarus.freepascal.org/User:Zeljan
Re: Adding Multiple Columns of Data to Listbox
« Reply #5 on: March 25, 2015, 08:16:43 pm »
Quote
Why don't U simple use TListView with Style = vsReport ?

I don't have that Style option. Only Standard, Virtual, OwenerDrawFixed and OwnerDrawVariable. How did you get vsReport? Newer version of Lazarus? (I using 1.2.6)

Howard - don't worry yourself to that extent. My request was more of a desirable than an essential. I had assumed it was easier than what it clearly is. I just didn't want to not split my data into columns if it was as easy as listbox1.Column[1].Add or something. But clearly it isn't.

Sorry, it's not Style but ViewStyle property.

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Adding Multiple Columns of Data to Listbox
« Reply #6 on: March 25, 2015, 08:17:18 pm »
Zeljko's suggestion was not to use TListBox at all, but to use TListView (Common Controls tab), which does have a vsReport ViewStyle.

madref

  • Hero Member
  • *****
  • Posts: 1116
  • ..... A day not Laughed is a day wasted !!
    • Nursing With Humour
Re: Adding Multiple Columns of Data to Listbox
« Reply #7 on: March 26, 2015, 12:06:24 am »
Try something like:
Code: [Select]
ComboBox_RefereeSeizoen.Items.AddObject(lkq.FieldByName('SEI_Periode').AsString,
      TObject(lkq.FieldByName('Sei_ID').AsInteger))
this is for a combobox but should work on a list item too
You treat a disease, you win, you lose.
You treat a person and I guarantee you, you win, no matter the outcome.

Main Platform:
--------------
Mac OS X Tahoe 26.2
Lazarus 4.99 (rev main_4_99-3149-g7867f6275c) FPC 3.3.1 x86_64-darwin-cocoa

Windows 10 Pro
Lazarus 3.99 (rev cbfd80ce39)

RegNatarajan

  • New Member
  • *
  • Posts: 28
Re: Adding Multiple Columns of Data to Listbox
« Reply #8 on: June 17, 2017, 12:27:38 pm »
Two years later, this thread saved me hours.  Thanks!

bytebites

  • Hero Member
  • *****
  • Posts: 778
Re: Adding Multiple Columns of Data to Listbox
« Reply #9 on: June 17, 2017, 01:38:49 pm »
GTK2 supports multicolumn listbox and combobox.

ladislav220

  • Newbie
  • Posts: 1
Re: Adding Multiple Columns of Data to Listbox
« Reply #10 on: October 04, 2017, 02:14:55 pm »
Try something like:
Code: [Select]
ComboBox_RefereeSeizoen.Items.AddObject(lkq.FieldByName('SEI_Periode').AsString,
      TObject(lkq.FieldByName('Sei_ID').AsInteger))
this is for a combobox but should work on a list item too

Can you help me how to extract value from Object? I filled DBLookupListBox but I can't use him.

My function:

procedure TForm6.Button5Click(Sender: TObject);
var
  i: integer;

begin
  i := 0;

  with DBLookupListBox2 do
  begin
    if Items.Count > 0 then
    begin
      for i := 0 to (Items.Count - 1) do
      begin
        ShowMessage(Format(
          'INSERT INTO ContractsToAssesment(ContractAssesmentRequest_ID,Contract_ID) VALUES (%d,%d)',
          [9999, ?]));
      end;
    end;
  end;
end; 

----
I can't find way how to fill KeyValue from my DBLookupListBox2 into Contract_ID field.

 

TinyPortal © 2005-2018