[SOLVED] how copy a StringGrid to the clipboard incl. row 0 but without column 0 lazarus / 31-LCL ---------------------------------------------------------------------- Hartmut · 2016-10-06 10:40 I have a TStringGrid and want to copy the whole contents into the clipboard by a button. I want to include row 0 (with the titles), but not to include column 0 (which contains line numbers by Option 'goFixedRowNumbering'). I have solution #1 which includes row 0, but also includes column 0 (which shows empty line numbers): Code: [Select]procedure TForm2.Button_ClipboardClick(Sender: TObject);    begin    StringGrid1.CopyToClipboard(False);    end; I have solution #2 which includes neither row 0 nor column 0: Code: [Select]procedure TForm2.Button_ClipboardClick(Sender: TObject);    var GR: TGridRect;    begin    GR.Left:=1; {without column 0}    GR.Right:=StringGrid1.ColCount-1;    GR.Top:=0;  {include row 0, but this does NOT work}    GR.Bottom:=StringGrid1.RowCount-1;    StringGrid1.Selection:=GR;    StringGrid1.CopyToClipboard(True);    end; Is there a way to have row 0 without column 0? Thanks in advance. ---------------------------------------------------------------------- wp · 2016-10-06 11:10 Just a little programming to remove the 1st column: 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  s: String;  sl: TStringList;  i: Integer;begin  sl := TStringList.Create;  try    sl.StrictDelimiter := true;    // Get rows as tab-separated string    for i:=0 to StringGrid1.RowCount-1 do begin      sl.Delimiter := ',';      sl.Assign(StringGrid1.Rows[i]);      sl.Delete(0);  // Delete 1st column      sl.Delimiter := #9;      if i = 0 then        s := sl.DelimitedText      else        s := s + LineEnding + sl.DelimitedText;    end;  finally    sl.Free;  end;  // Assign string to clipboard  Clipboard.AsText := s;end; ---------------------------------------------------------------------- Hartmut · 2016-10-06 12:12 I implemented the solution of wp and it works excellent and is fast enough. Thanks a lot. Only I have some difficulties to understand, how it works, because I don't know TStringList already and I did not find a good documentation about so far. So if anybody can recomment a good documentation (in english or german): links are very welcome. ---------------------------------------------------------------------- wp · 2016-10-06 13:34 You can find a StringList tutorial in http://wiki.freepascal.org/TStringList-TStrings_Tutorial TStringList is a list of strings, i.e., some kind of object-oriented array which can be extended dynamically by calling its method "Add(AText: String)". Another way to populate all entries of the list in a single stroke is by calling "Assign(AList: TStrings)" with another stringlist (or its ancestor TStrings) as a parameter. In a StringGrid, all cells within the same row can be converted to a TStrings object by calling the Rows[] property of the grid. Line 13, therefore, copies all cells of the specified row into the stringlist sl. Each cell text becomes a separate item of the stringlist. This means: sl[0] is the string in the first cell, sl[1] the second cell etc. (Note: index numbers always begin with zero). Since we don't want the first cell - the auto-numbered row header - we delete the first item in the stringlist: Delete(0). The StringList has a property DelimitedText which - when reading - combines all list items into a single string separated by the "Delimiter" of the list. The Delimiter is set in line 15 to be the tab character because this is a very conventient separator for the clipboard (BTW: Ignore line 12, it is not necessary; and line 15 should be repositioned before the "for" loop, but normally you won't notice the speed penalty). Calling sl.DelimitedText therefore combines all list items (i.e. cells of the row) to a single string, and separates them by a tab. All rows are added to a single string. After each row, there is a line break. Beginning with row 0 of the grid (the header row) we repeat this with all rows. At the end, we have a string which contains all the cell strings, separated by a tab, in all rows separated by line breaks. This is format is detected by many programs when pasted from the clipboard. Copying into the clipboard occurs by using the AsText property of the Clipboard. The try-finally makes sure that the stringlist is destroyed when it is not needed any longer. Look for "exceptions". Here is a better version of my code - as noted above, the old one could be improved (although you won't notice any difference): 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  s: String;  sl: TStringList;  i: Integer;begin  sl := TStringList.Create;  try    sl.StrictDelimiter := true;    sl.Delimiter := #9;    // Get rows as tab-separated string    for i:=0 to StringGrid1.RowCount-1 do begin    sl.Assign(StringGrid1.Rows[i]);      sl.Delete(0);  // Delete 1st column      if i = 0 then        s := sl.DelimitedText      else      s := s + LineEnding + sl.DelimitedText;    end;    // Assign string to clipboard    Clipboard.AsText := s;  finally    sl.Free;  end;end; ---------------------------------------------------------------------- Hartmut · 2016-10-06 14:56 Thank you very much, wp, for that very detailed and very good explanation how your code works. Now I understand it. I will work through the tutorial you recommended, but I'm still looking for some kind of "reference book" where all procedures and functions of TStringList and its ancestors are really explained... What I know from FPC-Help is often too short for me to understand, how a procedures or function works. ---------------------------------------------------------------------- wp · 2016-10-06 15:29 I don't think that you'll find a "reference book" for TStrings and TStringList somewhere. I know people are different, but for me personally it has always been the best to read some introduction, and once I understood the basics I continued by learning by doing. See also: http://www.delphibasics.co.uk/RTL.asp?Name=tstringlist http://www.freepascal.org/docs-html/rtl/classes/tstrings.html http://www.freepascal.org/docs-html/rtl/classes/tstringlist.html From your name I guess that you might be German. Then you could also have a look at http://www.tech-ecke.de/delphi/tstringlist.htm http://wiki.freepascal.org/Stringlist/de https://www.youtube.com/watch?v=Ys60OVxtpYk ---------------------------------------------------------------------- Hartmut · 2016-10-06 16:25 Thanks a lot again to wp for that useful links. They will help me to use TStringList in my future programs.