[SOLVED] Force a Column Header Click from code? lazarus / 31-LCL ---------------------------------------------------------------------- Hopestation · 2020-03-07 19:50 Hi. I have a stringgrid which holds a list of books in a library. Each book will have a number, but not all the books have numbers yet, so some have the number zero. This means that the number of rows in the grid is greater than the highest allocated book number. I have set the ColumnClickSorts property to TRUE, so I can click on the columns of Number, Title, Author, etc. to locate any book easily. When I add a new book I want to assign it the next highest number in the list. I would like to force a Click on the header of the Number column, so that the grid is sorted with the last row in the grid having the last number allocated and then assign the next number to the new book. Is this possible? Thanks ---------------------------------------------------------------------- winni · 2020-03-07 20:36 Hi! Let the machine work and look for the next number: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function TForm1.GetNextNumber : Integer;var i, max : integer;     s : string;beginmax :=  0;for i := 0 to Grid.Rowcount-1 do  begin  s:=Grid.Cells[ColForNumber,i]  ;  if s <> '' then if   IntToStr(s ) > max then               max := IntToStr(s );  end; // forresult := max + 1;end;   Winni ---------------------------------------------------------------------- Hopestation · 2020-03-08 13:06 Thanks Winni. I could write that myself. The question was is there a way to use an action, e.g. stringgrid(0,0).Click I couldn't find one but I am not that familiar with stringgrids. ---------------------------------------------------------------------- wp · 2020-03-08 13:30 If you can live without the sort arrow in the header you can call the method Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TCustomDrawGrid.SortColRow(IsColumn: Boolean; index: Integer);where "index" is the index of the column to be sorted, and "IsColumn" must be true to indicate that you are sorting a column, not a row. If you need the arrow, it gets "dirty". There is a procedure "HeaderClick" which is called when the click on the header occurs. Unfortunately it is protected and thus can be called only by some hacky type-conversion: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i type  TMyStringGrid = class(TStringGrid); procedure TForm1.Button1Click(Sender: TObject);begin  TMyStringGrid(StringGrid1).HeaderClick(true, StringGrid1.Col);end;   I tried to extract the functionality into a separate method in order to avoid the type-conversion, but the property Grid.SortColumn cannot be set by code, only by a real click. ---------------------------------------------------------------------- jamie · 2020-03-08 16:17 Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.Button1Click(Sender: TObject);var  R:TRect;begin  R := StringGrid1.CellRect(0,0);  PostMessage(StringGrid1.handle, LM_LBUTTONDOWN,0,MakeLong(R.Left,R.Top));  PostMessage(StringGrid1.handle, LM_LBUTTONUP,0,MakeLong(R.Left,R.Top));end;                           I had to include the windows unit so I could use the MakeLong… I am sure it is there somewhere in some other Laz unit.. but that will give you a header click at the top left or where ever.. ---------------------------------------------------------------------- Hopestation · 2020-03-09 09:03 Thanks for your answers. Both look good and I will try them now.