Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / lazarus / 2010-09

String Grid MouseToCell seems inconsistent when deleted some columns

5 messages · 3 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Lukasz Sokol

Hi Group,

would there be a problem with StringGrid on

Lazarus 0.9.28.2 r22279 FPC 2.2.4 i386-win32-win32/win64

that is preventing MouseToCell from working correctly if some columns have been deleted ?

(snippet)
{ put a StringGrid1 on the form }
{have one row fixed FixedRows := 1}

{hook this to the OnShow of the Form}
procedure TForm1.FormShow(Sender: TObject);
var column, line : integer;
begin
  StringGrid1.RowCount := 10;
  StringGrid1.ColCount := 10;

{this is just for filling the grid with demo data, the real project was reading it from file, using 2 StringLists on the way, to determine how many rows and cols there shall be}

  for line := 0 to StringGrid1.RowCount-1 do
    for column := 0 to StringGrid1.ColCount-1 do
      begin
        StringGrid1.Cells[column,line] := Format('Column %d',[column]);
      end;
  {now merge content of sequent even and odd columns
  for line := 0 to StringGrid1.RowCount-1 do
    for column := 0 to StringGrid1.ColCount-2 do
      begin
        StringGrid1.Cells[column,line] := StringGrid1.Cells[column,line] + ' '+
					  StringGrid1.Cells[column+1,line];
      end;
  {now delete the odd columns }
  {result will be 'Column 0 Column1|Column 2 Column 3|Column 4 Column 5 (and so on) }
  { does not matter if there is Begin/EndUpdate surrounding all string grid operations }
  for column := StringGrid.ColCount-1 downto 1 step 2 do
    StringGrid1.DeleteColRow(true,column);
  // or just plain
  // StringGrid1.DeleteColRow(true, 9);
  // StringGrid1.DeleteColRow(true, 7);
  // StringGrid1.DeleteColRow(true, 5);
  // StringGrid1.DeleteColRow(true, 3);
  // StringGrid1.DeleteColRow(true, 1);
  {you should end up with 5 columns on the grid}
end;

{ put this on OnClick of the stringgrid}

procedure TForm1.StringGrid1Click(Sender: TObject);
var gridcell : TPoint;
begin
  gridcell := StringGrid1.MouseToCell(Mouse.CursorPos);
  StringGrid1.Cells[gridcell.x,0] := IntToStr(gridcell.x);
  {what text do you get in fixed row of the visible column you've just clicked ?}
  {my project got 0, 2, 4, 6, 8 }
  {and I want 0,1,2,3,4 actually}

end;

My project is a little more complicated than above (the above has not been compile tested, it is just an idea of POC), but involves reading data from file ( space separated columns where it is data-dependent how many should be merged but still supporting name="value" convention) ).

Would that be TStringGrid bug or my thinko ?

Thanks,
Lukasz

Reply by email

faber
to Lukasz Sokol

2010/9/7 Lukasz Sokol :

Show quoted text 10 lines
procedure TForm1.StringGrid1Click(Sender: TObject); var gridcell : TPoint; begin gridcell := StringGrid1.MouseToCell(Mouse.CursorPos); StringGrid1.Cells[gridcell.x,0] := IntToStr(gridcell.x); {what text do you get in fixed row of the visible column you've just clicked ?} {my project got 0, 2, 4, 6, 8 } {and I want 0,1,2,3,4 actually} end;

try with StringGrid1.MouseToCell(ScreenToClient(Mouse.CursorPos)) or better use X,Y with OnMouseDown:
StringGrid1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);

best regards
faber

Reply by email

Lukasz Sokol
to faber

Hello faber,

On 07/09/2010 15:45, faber wrote:
[...]

try with StringGrid1.MouseToCell(ScreenToClient(Mouse.CursorPos))

Oh... yes that solved the issue. Sorry for the noise :) i was searching for the ScreenToClient, but $Deity knows why I was looking for it as procedure of the StringGrid itself :)

Show quoted text 5 lines
or better use X,Y with OnMouseDown: StringGrid1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);

umm but I am actually doing that one on context menu popup :) to add menu options to filter out certain values; that is why I use Mouse object.

Lukasz

Show quoted text 10 lines
best regards faber -- _______________________________________________ Lazarus mailing list http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus

Reply by email

Jesus Reyes
to Lukasz Sokol

--- El mar 7-sep-10, Lukasz Sokol escribi?:

Show quoted text 11 lines
De: Lukasz Sokol Asunto: Re: [Lazarus] String Grid MouseToCell seems inconsistent when deleted some columns A: "Lazarus mailing list" Fecha: martes, 7 de septiembre de 2010, 10:01 Hello faber, On 07/09/2010 15:45, faber wrote: [...] > > try with StringGrid1.MouseToCell(ScreenToClient(Mouse.CursorPos))

[...]

Show quoted text 6 lines
Oh... yes that solved the issue. Sorry for the noise :) i was searching for the ScreenToClient, but $Deity knows why I was looking for it as procedure of the StringGrid itself :)

Indeed it should, and the correct translation of coordinates should be made with StringGrid1

StringGrid1.MouseToCell(StringGrid1.ScreenToClient(Mouse.CursorPos))

otherwise MouseToCell would get parent form coordinates.

Jesus Reyes A.

Reply by email

Lukasz Sokol
to Jesus Reyes
Jesus Reyes wrote 33 lines
--- El mar 7-sep-10, Lukasz Sokol escribi?: > De: Lukasz Sokol > Asunto: Re: [Lazarus] String Grid MouseToCell seems inconsistent when deleted some columns > A: "Lazarus mailing list" > Fecha: martes, 7 de septiembre de 2010, 10:01 > Hello faber, > > On 07/09/2010 15:45, faber wrote: > [...] >> >> try with > StringGrid1.MouseToCell(ScreenToClient(Mouse.CursorPos)) [...] > Oh... yes that solved the issue. Sorry for the noise :) i > was searching for > the ScreenToClient, but $Deity knows why I was looking for > it as procedure of > the StringGrid itself :) > Indeed it should, and the correct translation of coordinates should be made with StringGrid1 StringGrid1.MouseToCell(StringGrid1.ScreenToClient(Mouse.CursorPos)) otherwise MouseToCell would get parent form coordinates. Jesus Reyes A.

Bingo again :)
for some reason StringGrid1.ScreenToClient did not show in code completion (tried a few times then decided there isn't any...) i guess what saved me was that the whole of form is filled with the grid.

Lukasz

Show quoted text 9 lines
-- _______________________________________________ Lazarus mailing list http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus

Reply by email