Recent

Author Topic: TStringGrid Entry set focus Prob;ems  (Read 4937 times)

JLWest

  • Hero Member
  • *****
  • Posts: 1293
TStringGrid Entry set focus Prob;ems
« on: January 22, 2019, 06:09:30 am »
I have a form with two TStringGrids.

I would like to set the Focus on Column 0, Row 1 of each grid. Here is what I found on the org and tried and none seem to work.

G1301Grid.Row:=1;
G1301Grid.Col:=0;
G1301Grid.SetFocus;

//G1301Grid.Row :=1;
//G1301Grid.Col:=0;
//G1301GridSelectCell(self,1,0,cansel);     // Won't compile so I added a period
//G1301Grid.SelectCell(self,1,0,cansel);     // Won't Compile
//G1301Grid.SetFocus;

//G1301Grid.Row:=1;
//G1301Grid.SetFocus;              // I can understand why this one doesn't work
//G1300Grid.Row:=1;              // as it doesn't have a column specified
//G1300Grid.SetFocus;           
FPC 3.2.0, Lazarus IDE v2.0.4
 Windows 10 Pro 32-GB
 Intel i7 770K CPU 4.2GHz 32702MB Ram
GeForce GTX 1080 Graphics - 8 Gig
4.1 TB

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: TStringGrid Entry set focus Prob;ems
« Reply #1 on: January 22, 2019, 03:28:28 pm »
If you have two string grids, Grid1 (TabOrder=0) & Grid2 (TabOrder=1) then the following code will programmatically put the focus on Grid2, with the cursor in the cell you want, and typing will immediately show in your desired Grid2 cell, with no need to click the grid first. Of course the grids' Options need to be set so they are AutoEdit with goEditing etc.
Code: Pascal  [Select][+][-]
  1.   TForm1 = class(TForm)
  2.    Grid1: TStringGrid;
  3.    Grid2: TStringGrid;
  4.     procedure FormActivate(Sender: TObject);
  5.     procedure Grid2Enter(Sender: TObject);
  6.     procedure Grid1Enter(Sender: TObject);
  7.   end;
  8.  
  9. var
  10.   Form1: TForm1;
  11.  
  12. implementation
  13.  
  14. procedure TForm1.Grid1Enter(Sender: TObject);
  15. begin
  16.   Grid2.Col := 0;
  17.   Grid2.Row := 1;
  18. end;
  19.  
  20. procedure TForm1.Grid2Enter(Sender: TObject);
  21. begin
  22.   Grid1.Col := 0;
  23.   Grid1.Row := 1;
  24. end;
  25.  
  26. procedure TForm1.FormActivate(Sender: TObject);
  27. begin
  28.   Grid1.PerformTab(True);
  29. end;
                           

JLWest

  • Hero Member
  • *****
  • Posts: 1293
Re: TStringGrid Entry set focus Prob;ems
« Reply #2 on: January 22, 2019, 04:16:33 pm »
@howardpc

Thanks for the reply.

Maybe what I'm trying to do can't be done.

I wasn't going to allow direct edit of the grids cells. It's both an accuracy and speed issue. On the form I was going to add three listboxes populated with valid data for the column entries of the grid cells.

I would like to move thru the grid columns with the left and right arrow keys. When a cell is highlighted a listbox with the valid entries for that cell would be available.

You would move up and down the rows with the up and down arrows.

I'm writing a little test program to see if it will work. I'll test your code to see how it works and if I can adapt it to fit.

Thanks.
FPC 3.2.0, Lazarus IDE v2.0.4
 Windows 10 Pro 32-GB
 Intel i7 770K CPU 4.2GHz 32702MB Ram
GeForce GTX 1080 Graphics - 8 Gig
4.1 TB

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: TStringGrid Entry set focus Prob;ems
« Reply #3 on: January 22, 2019, 09:05:31 pm »
The attached project may give you some pointers.Its based on a nonsense sum-of-integer-digits test to discriminate between 'numeric' data in the stringgrid cells.I also has only one grid.

I know your grids have meaningful strings of names or codes in each cell. But not being familiar with your actual data I substituted something simple.
 So you'll need to adapt the ideas here. But hopefully you'll have a working code skeleton for what you want to do.

JLWest

  • Hero Member
  • *****
  • Posts: 1293
Re: TStringGrid Entry set focus Prob;ems
« Reply #4 on: January 22, 2019, 09:15:36 pm »
Thanks I'll take a look.

Just fdinshed the demp which will explain a lot. Will post it after I get back from an errand.

May place it on my gmail drive and post a url for download.
FPC 3.2.0, Lazarus IDE v2.0.4
 Windows 10 Pro 32-GB
 Intel i7 770K CPU 4.2GHz 32702MB Ram
GeForce GTX 1080 Graphics - 8 Gig
4.1 TB

jamie

  • Hero Member
  • *****
  • Posts: 6090
Re: TStringGrid Entry set focus Prob;ems
« Reply #5 on: January 23, 2019, 12:21:08 am »
I think what you are looking for is the PickList to popup when you select a CELL.
The only true wisdom is knowing you know nothing

JLWest

  • Hero Member
  • *****
  • Posts: 1293
Re: TStringGrid Entry set focus Prob;ems
« Reply #6 on: January 23, 2019, 12:34:25 am »
Yea Something Like that.
https://drive.google.com/open?id=1k36AHCUk86Iu3K_2rQ1GCZ5jFuqBnS6b

Two files:

Readme.txt and the Grids Function.7z
FPC 3.2.0, Lazarus IDE v2.0.4
 Windows 10 Pro 32-GB
 Intel i7 770K CPU 4.2GHz 32702MB Ram
GeForce GTX 1080 Graphics - 8 Gig
4.1 TB

jamie

  • Hero Member
  • *****
  • Posts: 6090
Re: TStringGrid Entry set focus Prob;ems
« Reply #7 on: January 23, 2019, 02:58:18 am »
implement the OnSelectEditor event

Code: Pascal  [Select][+][-]
  1. procedure TForm1.StringGrid1SelectEditor(Sender: TObject; aCol, aRow: Integer;
  2.   var Editor: TWinControl);
  3. begin
  4.    Editor := TWinControl(TPickListCellEditor.Create(Self));
  5. end;                                                                      
  6.  

and use the OnPickList select...

P.S.
  You need to populate the list of course...

 also this is just a test code, you need to create PickListCellEditor elsewhere
because each time this event gets called it will keep adding another instance to the list, and this is going to
eat memory on you..
create the PickListCellEditor during the startup, place it in your form and use the OnCreate to initiate it.

 But in the SelectEditor event, this is where you populate the list each time a cell is selected for edit, each
cell maybe different.
  Pickeditor.Items.Clear;
 Pickeditor.AddItem('someText', nil); etc..

« Last Edit: January 23, 2019, 03:39:51 am by jamie »
The only true wisdom is knowing you know nothing

JLWest

  • Hero Member
  • *****
  • Posts: 1293
Re: TStringGrid Entry set focus Prob;ems
« Reply #8 on: January 23, 2019, 03:42:12 am »
I'm assuming the 'OnSelectEditor' event is an event on the TstringGrid.

And then you code it with:  Editor := TWinControl(TPickListCellEditor.Create(Self));

However, the folloing Var declaration doesn't look right to me. There should be two of the ')' or none I think;

var Editor: TWinControl);

On picklist select?

I'll try it and see what happens.
FPC 3.2.0, Lazarus IDE v2.0.4
 Windows 10 Pro 32-GB
 Intel i7 770K CPU 4.2GHz 32702MB Ram
GeForce GTX 1080 Graphics - 8 Gig
4.1 TB

jamie

  • Hero Member
  • *****
  • Posts: 6090
Re: TStringGrid Entry set focus Prob;ems
« Reply #9 on: January 23, 2019, 11:58:07 pm »
Look, don't create the Control in the OnSelecEditor event, because its going to keep creating new instances of
it with the way I showed you, that was just a quick and simple test not intended for actual use..

 Create a instance of the Editor elsewhere like in the Form.Create one time and then each time
the OnSelectEditor is triggered you set it and populate it with the correct content that fits the current
situation.
The only true wisdom is knowing you know nothing

wp

  • Hero Member
  • *****
  • Posts: 11856
Re: TStringGrid Entry set focus Prob;ems
« Reply #10 on: January 24, 2019, 01:00:32 am »

JLWest

  • Hero Member
  • *****
  • Posts: 1293
Re: TStringGrid Entry set focus Prob;ems
« Reply #11 on: January 24, 2019, 01:48:57 am »
@WP

I think I understand this a lot better now. How you highlight the cell. The picklist would work for some cells but some have multi-select entries and need to be formatted a specific way.

But thank you for the simple demo. I'll try working with this.

FPC 3.2.0, Lazarus IDE v2.0.4
 Windows 10 Pro 32-GB
 Intel i7 770K CPU 4.2GHz 32702MB Ram
GeForce GTX 1080 Graphics - 8 Gig
4.1 TB

jamie

  • Hero Member
  • *****
  • Posts: 6090
Re: TStringGrid Entry set focus Prob;ems
« Reply #12 on: January 24, 2019, 02:17:06 am »
Although you can do as WP showed you and it makes it easier of course however, if  you write a app like I
am currently hacking with, I preload picklist and have several of them on a row when I select a ROW.

 I do this so I can drop down one list, pick something out of it and then move over to another cell on the
same row and drop that one and then insert that item. This way I have different Picklist editor instances.
 
 I know I can also do this in the background of course with stringlist but this means I have to keep reloading
a single picklist editor.

 But you can code the way you wish, that is what makes it so wonderful.  :D
The only true wisdom is knowing you know nothing

 

TinyPortal © 2005-2018