Recent

Author Topic: A bit of help with worksheet tabs?  (Read 1624 times)

daveinhull

  • Sr. Member
  • ****
  • Posts: 297
  • 1 divided by nothing must still be 1!
A bit of help with worksheet tabs?
« on: February 03, 2022, 02:07:54 pm »
Hi,

I've read through the help infoirmaiton and scoured the forum, but I'm struggling to get started with worksheet grids.
I've added a tab control and moved my grid onto it, but I'm not sure how to work with tabs.
Do I need to manage manually what is show on each tab, i.e. to hide and unhide controls as I move between tabs (not what I'm used to), or shoud the tab control manage the child controls on that tab?
I've created two tabs with the grid on one, but when I switch to the other tab nothing chnages.
I was looking for a way to say that my grid was a child of a particualr tab page, but can't see that.

Any help in pointing me in the right direction would be appreciated.

Many thanks
Dave
Version #:1.8.4 Date 2019-01-08 FPC Version: 3.0.4 and SVN Revision 57972 for x86_64-win64-win32/win64

wp

  • Hero Member
  • *****
  • Posts: 10297
Re: A bit of help with worksheet tabs?
« Reply #1 on: February 03, 2022, 09:47:16 pm »
Do not use a regular TTabControl, there is a specialized one for fpspreadsheet.

Here's what I would do:
  • Add a TsWorkbookTabControl to the form. Adjust its size as needed
  • Add a TsWorksheetGrid to the TsWorksheetTabControl. Set it's Align to alClient to fill the entire TabControl.
  • Add a TsWorkbookSource to the form. It is the link to the underlying workbook (a list of worksheets) and enables the cooperation between tabcontrol and grid.
  • Set the WorkbookSource property of the tabcontrol and the grid to the TsWorkbookSource component added. Now the tabcontrol displays a tab for the grid.
  • In order to add new sheets (tabs) at runtime add a button and assign the following code to its OnClick event. As you see: do not add tabs as you normally do with a TTabControl, let the WorkbookSource do the work:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.   s: String;
  4. begin
  5.   s := InputBox('Add worksheet', 'Name', '');
  6.   if s <> '' then
  7.     sWorkbookSource1.Workbook.AddWorksheet(s);
  8. end;
  • In order to change a tab caption you must change the name of the worksheet. Add another button with the following OnClick handler:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button2Click(Sender: TObject);
  2. var
  3.   s: String;
  4. begin
  5.   s := InputBox('Rename worksheet', 'New name', sWorkbookSource1.Worksheet.Name);
  6.   sWorkbookSource1.Worksheet.Name := s;
  7. end;

You may also find it interesting to have a look at the FPSpreadsheet tutorial: Writing a mini spreadsheet application.

daveinhull

  • Sr. Member
  • ****
  • Posts: 297
  • 1 divided by nothing must still be 1!
Re: A bit of help with worksheet tabs?
« Reply #2 on: February 03, 2022, 09:57:34 pm »
Thanks wp, very helpful.
I did use the tab control from the from spreadsheet menu item,  is that the one you mean?
At the moment,  I'm just using an internal workbook because the aim will be to store the contents in a database, not a workbook.
I did look at the building a mini spreadsheet,  which does indicate much the same as what you've helped with. And, apart from the reference to the underlying workbook, I've done pretty much as you suggested.
I'm still not clear how controls work on the tab, is this somehow dependent on making reference to the underlying source workbook?
Should the controls on a tab automatically change when I change tabs or do I need to manually work this. Or have I missed the point of the tabcontrol. I'm used to it being able to change the content when I change the tabs.

Sorry for being a bit slow on this, but once I grasp things away I go😁

Thanks
Dave
Version #:1.8.4 Date 2019-01-08 FPC Version: 3.0.4 and SVN Revision 57972 for x86_64-win64-win32/win64

wp

  • Hero Member
  • *****
  • Posts: 10297
Re: A bit of help with worksheet tabs?
« Reply #3 on: February 03, 2022, 10:48:13 pm »
I did use the tab control from the from spreadsheet menu item,  is that the one you mean?
What do you mean by "spreadsheet menu item"? Such an item does not exist. Or do you mean the FPSpreadsheet tab on the component palette where all the components sit? In this case it is the correct TabControl.

I'm still not clear how controls work on the tab, is this somehow dependent on making reference to the underlying source workbook?
The visual fpspreadsheet controls are just wrappers around the workbook. The workbook provides the data displayed in the grid or the tabcontrol or the other visual fpspreadsheet controls. The grid is not a TStringGrid which stores all texts internally. It is a TDrawGrid which just draws the contents of the worksheet and propagates any changes back to it.

Should the controls on a tab automatically change when I change tabs or do I need to manually work this. Or have I missed the point of the tabcontrol. I'm used to it being able to change the content when I change the tabs.
A TabControl, by definition, aways displays the same control, the worksheetgrid here. When you switch to a different tab the TabControl tells the WorkbookSource that data from that other worksheet should be displayed. The WorkbookSource tells the Grid that it should load the data for the new tab.

This works transparently, you don't have to do anything. But: was it you who asked to restrict the number of columns and rows? This could become an issue in the TabControl when the number of predefined rows and columns is supposed to change from sheet to sheet - because the TabControl always displays the same grid. And probably there are other limitations when special requirements come into play.

daveinhull

  • Sr. Member
  • ****
  • Posts: 297
  • 1 divided by nothing must still be 1!
Re: A bit of help with worksheet tabs?
« Reply #4 on: February 04, 2022, 12:20:10 am »
Thank wp, that certainly explains a lot, I get it now.

Yes, I did mean the FPSpreadsheet tab on the component palette, sorry wrong terms!
Yes I was asking about limiting the size of the grid, but as you say, this would possible casue problems.

Thanks
Dave
Version #:1.8.4 Date 2019-01-08 FPC Version: 3.0.4 and SVN Revision 57972 for x86_64-win64-win32/win64

 

TinyPortal © 2005-2018