Recent

Author Topic: string grid resize specific columns  (Read 15136 times)

jack616

  • Sr. Member
  • ****
  • Posts: 268
string grid resize specific columns
« on: December 03, 2013, 01:56:02 pm »

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

  • Hero Member
  • *****
  • Posts: 1021
Re: string grid resize specific columns
« Reply #1 on: December 03, 2013, 03:21:44 pm »
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.
« Last Edit: December 04, 2013, 02:13:55 pm by Avishai »
Lazarus Trunk / fpc 2.6.2 / Win32

jack616

  • Sr. Member
  • ****
  • Posts: 268
Re: string grid resize specific columns
« Reply #2 on: December 03, 2013, 03:47:06 pm »
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

  • Hero Member
  • *****
  • Posts: 1021
Re: string grid resize specific columns
« Reply #3 on: December 03, 2013, 03:51:40 pm »
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.
Lazarus Trunk / fpc 2.6.2 / Win32

jack616

  • Sr. Member
  • ****
  • Posts: 268
Re: string grid resize specific columns
« Reply #4 on: December 03, 2013, 03:55:10 pm »
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

  • Hero Member
  • *****
  • Posts: 1021
Re: string grid resize specific columns
« Reply #5 on: December 03, 2013, 04:24:07 pm »
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
Lazarus Trunk / fpc 2.6.2 / Win32

jack616

  • Sr. Member
  • ****
  • Posts: 268
Re: string grid resize specific columns
« Reply #6 on: December 04, 2013, 01:15:33 pm »
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

  • Sr. Member
  • ****
  • Posts: 268
Re: string grid resize specific columns
« Reply #7 on: December 04, 2013, 01:40:01 pm »
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

  • Hero Member
  • *****
  • Posts: 1021
Re: string grid resize specific columns
« Reply #8 on: December 04, 2013, 02:32:04 pm »
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.
Lazarus Trunk / fpc 2.6.2 / Win32

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: string grid resize specific columns
« Reply #9 on: December 05, 2013, 08:16:37 am »
I 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...
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

 

TinyPortal © 2005-2018