Recent

Author Topic: Adding comboboxes to cells in an FPSpreedsheet  (Read 2943 times)

jdaz2000

  • New Member
  • *
  • Posts: 13
Adding comboboxes to cells in an FPSpreedsheet
« on: July 08, 2023, 12:43:31 am »
Does anyone know a way that I can make all the cells in a FPSpreadsheet column comboboxes with each combobox have the same items?  I want to limit the selection choice for a particular column.

wp

  • Hero Member
  • *****
  • Posts: 12456
Re: Adding comboboxes to cells in an FPSpreedsheet
« Reply #1 on: July 08, 2023, 01:15:50 am »
The TsWorksheetGrid is a TCustomGrid after all, and thus many of the techniques used for TCustomGrid (or TStringGrid) may work for TsWorksheetGrid, too. The Grids-wiki describes how a different cell editor can be selected (https://wiki.lazarus.freepascal.org/Grids_Reference_Page#Grid_Cell_Editors) - do the same with the worksheet grid:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.sWorksheetGrid1SelectEditor(Sender: TObject; aCol,
  2.   aRow: Integer; var Editor: TWinControl);
  3. begin
  4.   if (aCol = 3) then
  5.   begin
  6.     Editor := sWorksheetGrid1.EditorByStyle(cbsPickList);
  7.     with TPickListCellEditor(Editor) do
  8.     begin
  9.       Style := csDropDownList;
  10.       Items.StrictDelimiter := true;
  11.       Items.CommaText := 'Africa,Asia,Europe,North America,Oceania,South America';
  12.     end;
  13.   end;
  14. end;
  • This code is the handler for the grid's OnSelectEditor event.
  • The line "Editor := sWorksheetGrid1.EditorByStyle(cbsPickList)" selects a picklist cell editor (combobox) for the cells defined in the preceeding "if" condition (here: all cells of column 3).
  • "TPickListCellEditor(Editor).Items.CommaText" defines the items in the combobox as a comma-separated list, here: the earth's continents. Since the items "North America" and "South America" contain spaces we must switch StrictDelimiter to true, otherwise the ComboBox would split these items at the space and add "North", "South" and "America" as separate items. Depending on the leading "if" condition you can use different item lists for specific cells.
  • "Style := csDropdownList" disables direct typing for the user.
If you don't like the thick border of the selected cell interfering with the combobox you could additionally call "sWorksheetGrid1.SelectionPen.Style := psClear" in the OnSelectEditor handler - this hides the selection rectangle while the combobox is visible. The counter-part,  "sWorksheetGrid1.SelectionPen.Style := psSolid", then should be called in the OnEditingDone event of the grid in order to restore the selection rectangle.

« Last Edit: July 08, 2023, 01:27:03 am by wp »

jdaz2000

  • New Member
  • *
  • Posts: 13
Re: Adding comboboxes to cells in an FPSpreedsheet
« Reply #2 on: July 08, 2023, 03:41:29 pm »
Thanks for your help.  I really appreciate it.

jdaz2000

  • New Member
  • *
  • Posts: 13
Re: Adding comboboxes to cells in an FPSpreedsheet
« Reply #3 on: July 08, 2023, 06:43:48 pm »
I rookie question but I can't find an answer.  I created a test app and placed a single TsWorkSheetGrid control on Form1. 

On the Events tab in Object Inspector I have sWorksheetGrid1SelectEditor showing the OnSelectEditor line.

However, when I run it and click on any cell in the grid the procedure TForm1.sWorksheetGrid1SelectEditor is never triggered.

Can't figure out what I'm doing wrong.

wp

  • Hero Member
  • *****
  • Posts: 12456
Re: Adding comboboxes to cells in an FPSpreedsheet
« Reply #4 on: July 08, 2023, 06:46:11 pm »
Did you activate the goEditing option? By default, the LCL grids are read-only, and the cell editor will not show. You must set this flag to make them editable.

jdaz2000

  • New Member
  • *
  • Posts: 13
Re: Adding comboboxes to cells in an FPSpreedsheet
« Reply #5 on: July 08, 2023, 07:12:07 pm »
That was it, thanks so much. You are very kind to help.

Allam

  • Newbie
  • Posts: 3
Re: Adding comboboxes to cells in an FPSpreedsheet
« Reply #6 on: July 08, 2023, 07:19:27 pm »
The best option is to install Rx lib from onlinepackagemanager  . it contain FPSpreedsheet and ZeosDB
with Rx you can use dbgridview with auto filter like Excel and Export the dbgrid to Excel in a second ....
it is very very usefull
I am sory for my English

jdaz2000

  • New Member
  • *
  • Posts: 13
Re: Adding comboboxes to cells in an FPSpreedsheet
« Reply #7 on: July 08, 2023, 07:25:45 pm »
Thanks for the suggestion.  I'll take a look at it.

wp

  • Hero Member
  • *****
  • Posts: 12456
Re: Adding comboboxes to cells in an FPSpreedsheet
« Reply #8 on: July 08, 2023, 07:27:26 pm »
Since a WorksheetGrid just placed on a form has the default property values inherited you probably should adjust also other settings which I had listed in the wiki article about the grid: https://wiki.lazarus.freepascal.org/TsWorksheetGrid#Recommended_property_values


jdaz2000

  • New Member
  • *
  • Posts: 13
Re: Adding comboboxes to cells in an FPSpreedsheet
« Reply #9 on: July 08, 2023, 07:54:28 pm »
Great help! Thanks very much.

jdaz2000

  • New Member
  • *
  • Posts: 13
Re: Adding comboboxes to cells in an FPSpreedsheet
« Reply #10 on: July 10, 2023, 07:52:15 pm »
Not having much luck finding documentation for TsWorkSheets with comboboxes so here is another question.

Can someone tell me how to set the item index value for a ComboBox in a particular Cell in a TsWorkSheetGrid?




jdaz2000

  • New Member
  • *
  • Posts: 13
Re: Adding comboboxes to cells in an FPSpreedsheet
« Reply #11 on: July 10, 2023, 08:41:43 pm »
Figured it out.

      MyPickListCellEditor := TPickListCellEditor(sWorkSheetGrid1.EditorByStyle(cbsPickList));
      sWorkSheetGrid1.Cells[1,1]:=MyPickListCellEditor.Items[1];

jwdietrich

  • Hero Member
  • *****
  • Posts: 1236
    • formatio reticularis
Re: Adding comboboxes to cells in an FPSpreedsheet
« Reply #12 on: July 11, 2023, 08:32:44 am »
In the SimulaBeta project, you may find working examples that don't require external libraries for the GUI. They are implemented in the sequencer window (sequencer.pas and sequencer.lfm).

The source code is available from SourceForge and GitHub.
function GetRandomNumber: integer; // xkcd.com
begin
  GetRandomNumber := 4; // chosen by fair dice roll. Guaranteed to be random.
end;

http://www.formatio-reticularis.de

Lazarus 3.4.0 | FPC 3.2.2 | PPC, Intel, ARM | macOS, Windows, Linux

 

TinyPortal © 2005-2018