Recent

Author Topic: Select cell in listview / stringgrid  (Read 1103 times)

Jonny

  • Full Member
  • ***
  • Posts: 104
Select cell in listview / stringgrid
« on: January 20, 2025, 07:48:55 pm »
This is probably simple, but I cannot find any way to do it.

I have a listview with several columns and several rows.

How to know which cell (column/row) was clicked?
« Last Edit: January 21, 2025, 02:32:58 pm by Jonny »

ASerge

  • Hero Member
  • *****
  • Posts: 2376
Re: Select cell in listview
« Reply #1 on: January 20, 2025, 08:37:19 pm »
This is probably simple, but I cannot find any way to do it.
I have a listview with several columns and several rows.
How to know which cell (column/row) was clicked?
Not simple.
In Windows:
Code: Pascal  [Select][+][-]
  1. uses CommCtrl;
  2.  
  3. procedure TForm1.ListView1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
  4. var
  5.   R: TLVHITTESTINFO;
  6. begin
  7.   Caption := 'nil';
  8.   FillChar(R, SizeOf(R), 0);
  9.   R.pt.X := X;
  10.   R.pt.Y := Y;
  11.   if ListView_SubItemHitTest(ListView1.Handle, @R) <> -1 then
  12.     Caption := Format('Row=%d, Col=%d', [R.iItem, R.iSubItem]);
  13. end;

dsiders

  • Hero Member
  • *****
  • Posts: 1345
Re: Select cell in listview
« Reply #2 on: January 20, 2025, 08:42:03 pm »
This is probably simple, but I cannot find any way to do it.

I have a listview with several columns and several rows.

How to know which cell (column/row) was clicked?

TListView is not a grid. It knows about the Selected item on the control, or Selections when MultiSelect is enabled. There is an OnColumnClick event... but it applies top clicks on the column header. 

Sounds like you really want a grid control.
Preview the next Lazarus documentation release at: https://dsiders.gitlab.io/lazdocsnext

Jonny

  • Full Member
  • ***
  • Posts: 104
Re: Select cell in listview
« Reply #3 on: January 20, 2025, 10:54:08 pm »
Thank you both for your quick responses..

Quote from: ASerge
Not simple.
In Windows:

Alas, you are correct, not simple, and probably not cross-platform.

Quote from: dsiders
Sounds like you really want a grid control.

Ah yes, a StringGrid does indeed seem like a more suitable solution.


Now is it possible to have another component within a StringGrid cell? Specifically looking to have a checkbox in one cell and a combobox in another.

dsiders

  • Hero Member
  • *****
  • Posts: 1345
Re: Select cell in listview
« Reply #4 on: January 21, 2025, 12:39:56 am »
Thank you both for your quick responses..

Quote from: ASerge
Not simple.
In Windows:

Alas, you are correct, not simple, and probably not cross-platform.

Quote from: dsiders
Sounds like you really want a grid control.

Ah yes, a StringGrid does indeed seem like a more suitable solution.

Now is it possible to have another component within a StringGrid cell? Specifically looking to have a checkbox in one cell and a combobox in another.

Short answer: Sure.

Longer answer: You need to spend some time with the docs at : https://lazarus-ccr.sourceforge.io/docs/lcl/grids/tstringgrid.html. Specifically the Options property and event handlers like OnSelection, OnSelectCell, and OnSelectEditor.
Preview the next Lazarus documentation release at: https://dsiders.gitlab.io/lazdocsnext

Jonny

  • Full Member
  • ***
  • Posts: 104
Re: Select cell in listview
« Reply #5 on: January 21, 2025, 02:32:19 pm »
The documentation links were a great help and I managed to implement checkboxes and comboboxes using ButtonStyle = cbsCheckboxColumn / cbsPickList.

One little outstanding issue is that to get the pick list to display its items requires several clicks - usually 3 clicks, one to select the cell, then another to select the control, then another to show the dropdown list.

The OnSelectEditor event does expose the relevant TWinControl so is there a way to reduce these clicks?

The following does not seem to work:

Code: Pascal  [Select][+][-]
  1. TPickListCellEditor(Editor).DroppedDown:=True;

I am developing on Linux but would like the result to be cross platform. Simple project is attached. Any suggestions please?

morknot

  • Jr. Member
  • **
  • Posts: 52
  • still learning
Re: Select cell in listview / stringgrid
« Reply #6 on: January 22, 2025, 11:33:17 am »
This works for me
Code: Pascal  [Select][+][-]
  1. procedure TForm1.StringGrid1SelectEditor(Sender: TObject; aCol, aRow: Integer;
  2.   var Editor: TWinControl);
  3.  
  4. begin
  5.  If (acol=1) then
  6.  begin
  7.  
  8. Editor := StringGrid1.EditorByStyle(cbsPickList);
  9.  
  10. With Editor as TCustomComboBox do
  11. begin
  12.   Style:=csDropDownList;
  13.  
  14.  If Items.Count=0 then
  15.      Items.CommaText:='Hello,Goodbye';
  16.  
  17. Visible:=True;
  18.  DroppedDown:=True;
  19.  
  20.   end;
  21.  end;
  22.  
  23.  
  24. end;        
  25.  
  26.  
« Last Edit: January 22, 2025, 11:36:27 am by morknot »

Jonny

  • Full Member
  • ***
  • Posts: 104
Re: Select cell in listview / stringgrid
« Reply #7 on: January 22, 2025, 02:34:09 pm »
Quote from: morknot
This works for me

Thanks @morknot but clicking on a picklist cell causes the dropdown menu to quickly flash - is there a way to prevent it?

 

TinyPortal © 2005-2018