string grid resize specific columns lazarus / 31-LCL ---------------------------------------------------------------------- jack616 · 2013-12-03 12:56 Before I start hacking away I thought I'd ask if anyone has done this already?: I have a Tstringgrid with 8 columns set to alClient and client width resizable using a splitter. So far so good - except when resizing the grid all columns resize equally. What I'd like to do is keep columns 1,2 and 6 at a fixed size and share the extra space between the remaining columns (ideally with extra flexibility to assign % values per column) I have tried setting the min/max widths but these get overridden to keep the canvas full by the looks of it. Has anyone done anything like this yet? TIA ---------------------------------------------------------------------- Avishai · 2013-12-03 14:21 If you look in the Property Editor you will see Property.Columns Click on 'Columns' and it will bring up a Dialog to add your columns.  Then you can specify the widths of your columns separately in the Property Editor or in code, you can adjust TStringGrid.Columns[j].Width any way you wish. If you just set TSgringGrid.ColCount:=8 I don't think you can do this. ---------------------------------------------------------------------- jack616 · 2013-12-03 14:47 Thank you avishai but this does not work. The issue is that the client width will change when the splitter resizes the width. This causes the Tstringgrid to resize the columns equally to fill the canvas no matter what settings you put in the columns settings. ---------------------------------------------------------------------- Avishai · 2013-12-03 14:51 Do you have 'AutoFillColumns' set to True.  If so then this is the correct behavior.  It is doing what it is designed to do.  If you set it to False then you can adjust the column widths as you want. ---------------------------------------------------------------------- jack616 · 2013-12-03 14:55 Yes this is what I plan to hack to enable variable fixed width selectable columns. Unfortunately simply making it false and manually setting widths raises other issues I am trying to avoid... ---------------------------------------------------------------------- Avishai · 2013-12-03 15:24 There is no need to 'hack' anything.  This code only adjusts one column but it should get you started. Code: [Select]procedure TForm1.Button1Click(Sender: TObject); begin   SG1.Width:= SG1.Width+10; end; procedure TForm1.SG1Resize(Sender: TObject); begin   SG1.Columns[4].Width:= SG1.ClientWidth-     SG1.Columns[3].Width-     SG1.Columns[2].Width-     SG1.Columns[1].Width-     SG1.Columns[0].Width-     SG1.DefaultColWidth   ; end; Note: The Grid has FixCols:= 1 ---------------------------------------------------------------------- jack616 · 2013-12-04 12:15 Hi Avishai You are absolutely right - I figured it out late last  night. I was having a problem with GDB crashing that was causeing issues even after I killed lazarus with taskmanager and restarted. This caused my system and the stringgrid to behave in odd ways that made me think it it couldnt do things and would behave oddly (losing columns etc) when I tried. It was a totally confusing situation but I now cold boot after such crashes and the thing works just fine afterwards. I seem cursed to find the oddest issues at the moment... but thanks for you patience. ---------------------------------------------------------------------- jack616 · 2013-12-04 12:40 FYI this is what I ended up with : Code: [Select]procedure sizeColumnToContent(col: integer);     // size column and fix to prevent resizing var    i, mWidth,tmp: integer; begin    mWidth := 0;    for i := 0 to (myForm.mFields.RowCount -1) do    begin       tmp := myForm.canvas.TextWidth(myForm.mFields.Cells[col,i]);   //factor this       if tmp > mWidth then mWidth := tmp;    end;    mWidth := mWidth+padding;    // padding defined in dat    with (myForm.mFields) do    begin       Columns.Items[col].maxSize := mWidth;       Columns.Items[col].minsize := mWidth;       Columns.Items[col].width := mWidth;       Columns.Items[col].sizePriority := 0;  //factor this    end; end ; ---------------------------------------------------------------------- Avishai · 2013-12-04 13:32 I'm glad you got things working the way you want.  TStringGrid isn't as advanced as some third party Grids, but you can usually get it to do what you need. ---------------------------------------------------------------------- BigChimp · 2013-12-05 07:16 Quote from: jack616 on December 04, 2013, 01:15:33 pmI was having a problem with GDB crashing that was causeing issues even after I killed lazarus with taskmanager and restarted. FYI: for me, the Reset Debugger menu item seems to fix any problems whenever gdb crashes... but I might have just been lucky...