Recent

Author Topic: TStringGrid vs. With  (Read 8498 times)

asdf

  • Sr. Member
  • ****
  • Posts: 310
TStringGrid vs. With
« on: October 27, 2010, 03:23:44 pm »
Procedure TForm1.LightGrid;
  begin
  i:= Form1.ControlCount;
  for ii := 0 to i - 1 do
  if Form1.Controls[ii] is TStringGrid then
  showmessage(Form1.Controls[ii].Name); This line worked well.
    begin
    with Form1.Controls[ii] do  What should I do with this line?
    begin
    Flat:= true;   Error message - Identifier 'Flat' not found.
    borderstyle:= bsnone;
    Options:= Options - [goVertLine];   Error message - Identifier 'Options' not found.
    end; //with
    end; //if
end;     //procedure       
Lazarus 1.2.4 / Win 32 / THAILAND

typo

  • Hero Member
  • *****
  • Posts: 3051
Re: TStringGrid vs. With
« Reply #1 on: October 27, 2010, 03:38:05 pm »
Code: [Select]
if Form1.Controls[ii] is TStringGrid then
  with TStringGrid(Form1.Controls[ii]) do
« Last Edit: October 27, 2010, 03:40:57 pm by typo »

asdf

  • Sr. Member
  • ****
  • Posts: 310
Re: TStringGrid vs. With
« Reply #2 on: October 27, 2010, 04:52:51 pm »
LightGrid;

This worked with only TStringGrids exactly on Form1.
But in Form1, there are lots of TPageControl.
And each TpageControl, there are lots of  TTabSheets.

"LightGrid;" didn't work with TStringGrids on every TTabSheet.

I would like "LightGrid;" to work with all TStringGrids in everywhere they are in.
What code should be written more?
Lazarus 1.2.4 / Win 32 / THAILAND

ivan17

  • Full Member
  • ***
  • Posts: 173
Re: TStringGrid vs. With
« Reply #3 on: October 27, 2010, 05:37:58 pm »
Code: [Select]
Procedure TForm1.LightGrid;
  begin
  for ii := 0 to Form1.ControlCount - 1 do  //  no need for a variable - limit isn't recalculated
  if Form1.Controls[ii] is TStringGrid then begin            // <<< begin here
  showmessage(Form1.Controls[ii].Name); [b][color=red]This line worked well.[/color][/b]
  // that's  because any control has a name. cast Controls[ii] to stringgrid to access the rest
    with Form1.Controls[ii] as TStringGrid do begin
      Flat:= true;   
      BorderStyle:= bsnone;
      Options:= Options - [goVertLine];   
    end; //with
  end; //if
end;     //procedure       

asdf

  • Sr. Member
  • ****
  • Posts: 310
Re: TStringGrid vs. With
« Reply #4 on: October 28, 2010, 03:11:46 am »
It still worked with only TStringGrid exactly on TForm.
But it did not touch any TStringGrid in TPageControl or TTabSheet.

.ControlCount didn't count TStringGrid in TPageControl or TTabSheet.

I saw an idea; "visible".

How can I apply this property to such procedure?
Lazarus 1.2.4 / Win 32 / THAILAND

ivan17

  • Full Member
  • ***
  • Posts: 173
Re: TStringGrid vs. With
« Reply #5 on: October 28, 2010, 04:58:25 am »
It still worked with only TStringGrid exactly on TForm.
But it did not touch any TStringGrid in TPageControl or TTabSheet.
controls array lets you traverse direct children of a control. recurse through any container control like you did through form.

I saw an idea; "visible".
How can I apply this property to such procedure?
like any other property. try it inside the with-block.

Zoran

  • Hero Member
  • *****
  • Posts: 1829
    • http://wiki.lazarus.freepascal.org/User:Zoran
Re: TStringGrid vs. With
« Reply #6 on: October 28, 2010, 07:53:16 am »
controls array lets you traverse direct children of a control. recurse through any container control like you did through form.

Yes, so this is what you need:
Code: [Select]
procedure TForm1.LightGrid;

  procedure DoLightGrid(const C: TWinControl);
  var
    II: Integer;
  begin
    if C is TStringGrid then begin
      showmessage(C.Name);
      with TStringGrid(C) do begin
        Flat:= true;
        BorderStyle:= bsnone;
        Options:= Options - [goVertLine];
      end;
    end;
    for II := 0 to C.ControlCount - 1 do
      if C.Controls[II] is TWinControl then
        DoLightGrid(TWinControl(C.Controls[II]));
  end;

begin
  DoLightGrid(Self);
end;

Now it works for all StringGrids on Form1, doesn't it? ;)

asdf

  • Sr. Member
  • ****
  • Posts: 310
Re: TStringGrid vs. With
« Reply #7 on: October 29, 2010, 04:33:10 am »
Dear Zoran,

OMG! It's really great, thank you so much.

I'd like the user to look at it as if it is like a note book paper.

I'm now just starting reading www.delphibasics.co.uk.
Is this matter/technique described in such website?
Lazarus 1.2.4 / Win 32 / THAILAND

Zoran

  • Hero Member
  • *****
  • Posts: 1829
    • http://wiki.lazarus.freepascal.org/User:Zoran
Re: TStringGrid vs. With
« Reply #8 on: October 29, 2010, 07:49:56 am »
Dear Zoran,

OMG! It's really great, thank you so much.

I'd like the user to look at it as if it is like a note book paper.

You are welcome!

I'm now just starting reading www.delphibasics.co.uk.
Is this matter/technique described in such website?

I don't know.

This basic lesson about recursion form Tao Yue's Pascal tutorial might be helpful to you.

Then try to solve "Towers of Hanoi" problem yourself, before you see the solution.

 

TinyPortal © 2005-2018