Recent

Author Topic: [SOLVED] Loop Thru Components Setting Visibility And Enableability  (Read 12189 times)

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Loop Thru Components Setting Visibility And Enableability
« Reply #15 on: September 10, 2017, 07:32:34 am »
It is interesting your coding style of placing variables after a nested procedure.

I am self-taught as pascal goes (Apple ][ + ...) [I shall have to reply to that other new member] and learned that variables (and constants) of a mother procedure are available to a child. So I place them first. Hence my #4.

I prefer the safety of knowing that my local variables can not be changed from outside my "visible" code. It helps me find typing or logical errors in my code if I only allow access to the absolutely required resources. I prefer to have the values passed as parameters and let the inline definition optimize them as needed. It also helps if I deside that a sub procedure needs to be move in a more public section. I do not have to change it that much.

Anyway I think we've cooked it enough. I shall honour you by putting the #5 in my application and adjust the random accordingly.

B
I wouldn't add the last one, it was more of an info sharing post than a solution proposal, I would rather not have it memorized or used outside the "purely research" environment.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

Bazzao

  • Full Member
  • ***
  • Posts: 178
  • Pies are squared.
Re: Loop Thru Components Setting Visibility And Enableability
« Reply #16 on: September 10, 2017, 07:41:20 am »
taazz,

Quote
I wouldn't add the last one, it was more of an info sharing post than a solution proposal, I would rather not have it memorized or used outside the "purely research" environment.

Your request has been granted. It was not yet tested or melded.

B

Bazza

Lazarus 2.0.10; FPC 3.2.0; SVN Revision 63526; x86_64-win64-win32/win64
Windows 10.

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: Loop Thru Components Setting Visibility And Enableability
« Reply #17 on: September 10, 2017, 04:58:54 pm »
I agree with Taazz.

Just for fun, not tested:
Code: Pascal  [Select][+][-]
  1. procedure SetVisibleByTags(Parent: TWinControl; pPrivate,pTest:boolean);
  2. var
  3.   C:TComponent;
  4. begin
  5.  for C in Parent do
  6.    SetPropValue(C, 'Visible',
  7.      ((c.Tag = 22) and pPrivate) or ((c.Tag = 11) and pTest) or ((not c.Tag in [11,22]) and GetPropValue(C,'Visible')));
  8. end;
  9.  


jamie

  • Hero Member
  • *****
  • Posts: 6090
Re: Loop Thru Components Setting Visibility And Enableability
« Reply #18 on: September 10, 2017, 06:06:04 pm »
Unless it has been fixed since the last time I checked, if you ever want a callback procedure
for some function from windows to enumerate a resource, the compiler seems to ignore
the Stdcall, where as Delphi it would generate the StdCall procedure and you could keep all
code within the main body of a function to enumerate some sort of resource.

 I ported an app from Delphi and this was one of the crashes I had to fix, by placing the
callback out side the procedure body. Otherwise fpc would just generate a pascal/Register type.
The only true wisdom is knowing you know nothing

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Loop Thru Components Setting Visibility And Enableability
« Reply #19 on: September 10, 2017, 09:17:19 pm »
and the winner is (drum roll)......

Engin with  3 lines of code!! That was fun :).
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: Loop Thru Components Setting Visibility And Enableability
« Reply #20 on: September 10, 2017, 09:50:07 pm »
lol, I can't take credit for it. It is based on your awesome code, Taazz.

Bazzao

  • Full Member
  • ***
  • Posts: 178
  • Pies are squared.
Re: Loop Thru Components Setting Visibility And Enableability
« Reply #21 on: September 11, 2017, 12:34:29 am »
I have just tested engkin's:

Compile Project, Mode: Development, Target: QuickNotes1.exe: Exit code 1, Errors: 2, Hints: 1
unit1.pas(1153,4) Error: Identifier not found "SetPropValue"
unit1.pas(1154,77) Hint: The left operand of the IN operator should be byte sized
unit1.pas(1154,93) Error: Identifier not found "GetPropValue"

That means taazz's previous would have failed too.

Sorry, but back to the miniaturized drawing board for you both. :P

B
Bazza

Lazarus 2.0.10; FPC 3.2.0; SVN Revision 63526; x86_64-win64-win32/win64
Windows 10.

Bazzao

  • Full Member
  • ***
  • Posts: 178
  • Pies are squared.
Re: Loop Thru Components Setting Visibility And Enableability
« Reply #22 on: September 11, 2017, 12:39:41 am »
I found typinfo needed to be in the uses clause.

But it crashes "Unknown property: Visible" dialog box.

Back to #1 message.

B
Bazza

Lazarus 2.0.10; FPC 3.2.0; SVN Revision 63526; x86_64-win64-win32/win64
Windows 10.

Bazzao

  • Full Member
  • ***
  • Posts: 178
  • Pies are squared.
Re: Loop Thru Components Setting Visibility And Enableability
« Reply #23 on: September 11, 2017, 12:50:25 am »
engkin,

Learning from those other 2, I introduced Byte(c.Tag) to get rid of that hint.

Provided the tags are between 1 & 255. I wouldn't use 0 instead of 11 or 22 in any of the code generated in this thread.

B
Bazza

Lazarus 2.0.10; FPC 3.2.0; SVN Revision 63526; x86_64-win64-win32/win64
Windows 10.

Bazzao

  • Full Member
  • ***
  • Posts: 178
  • Pies are squared.
Re: Loop Thru Components Setting Visibility And Enableability
« Reply #24 on: September 11, 2017, 01:08:56 am »
In theory this should work, it passes syntax, but crashes with the same dialog box error:

Code: Pascal  [Select][+][-]
  1. procedure SetVisibleByTags5AndAbit(Parent: TWinControl; pPrivate,pTest:boolean);
  2. var
  3.   C:TComponent;
  4. begin
  5.  for C in Parent do
  6.    if C is TMenuItem then with C as TMenuItem do
  7.      SetPropValue(C, 'Visible',
  8.        ((c.Tag = 22) and pPrivate) or ((c.Tag = 11) and pTest) or ((not Byte(c.Tag) in [11,22]) and GetPropValue(C,'Visible')))
  9.    else if C is TControl then with C as TControl do
  10.      SetPropValue(C, 'Visible',
  11.        ((c.Tag = 22) and pPrivate) or ((c.Tag = 11) and pTest) or ((not Byte(c.Tag) in [11,22]) and GetPropValue(C,'Visible')));
  12. end;
  13.  

B

Bazza

Lazarus 2.0.10; FPC 3.2.0; SVN Revision 63526; x86_64-win64-win32/win64
Windows 10.

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: Loop Thru Components Setting Visibility And Enableability
« Reply #25 on: September 11, 2017, 01:57:21 am »
  • Ah, that is a bit-wise "Not" instead of logical.
  • When using "is" no need for "as".

Maybe this would work:
Code: Pascal  [Select][+][-]
  1. procedure SetVisibleByTags(Parent: TWinControl; pPrivate, pTest:boolean);
  2. var
  3.   C: TComponent;
  4.   mnuItem: TMenuItem absolute C;
  5.   ctrl: TControl absolute C;
  6. begin
  7.  for C in Parent do
  8.    if C is TMenuItem then
  9.      mnuItem.Visible := not (((c.Tag = 22) and pPrivate) or ((c.Tag = 11) and pTest) or (((not c.Tag in [11,22])) and mnuItem.Visible))
  10.    else if C is TControl then
  11.         ctrl.Visible := not (((c.Tag = 22) and pPrivate) or ((c.Tag = 11) and pTest) or (((not c.Tag in [11,22])) and ctrl.Visible));
  12. end;

Bazzao

  • Full Member
  • ***
  • Posts: 178
  • Pies are squared.
Re: Loop Thru Components Setting Visibility And Enableability
« Reply #26 on: September 11, 2017, 02:51:53 am »
Now that does something weird. It causes fTempMemo and  fTempPanel to be visible. Not desirable. Both have default Tags of 0 (assumed).

Code: Pascal  [Select][+][-]
  1.   {Form1}
  2.   private
  3.     { private declarations }
  4.     fTempMemo    :TMemo;
  5.     fTempPanel   :TPanel;
  6.   public
  7.     { public declarations }
  8.   end;
  9.  

Sorry, but your search for shortest code has become longer and buggy. :'(

B
Bazza

Lazarus 2.0.10; FPC 3.2.0; SVN Revision 63526; x86_64-win64-win32/win64
Windows 10.

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: Loop Thru Components Setting Visibility And Enableability
« Reply #27 on: September 11, 2017, 05:09:11 am »
You ought to put some effort as well  :P
Here you go, short, but not that buggy, code:

Code: Pascal  [Select][+][-]
  1. procedure SetVisibleByTags(Parent: TWinControl; pPrivate, pTest:boolean);
  2. var
  3.   C: TComponent;
  4. begin
  5.  for C in Parent do
  6.    if C is TMenuItem then TMenuItem(C).Visible := ((c.Tag = 22) and pPrivate) or ((c.Tag = 11) and pTest) or (((not (c.Tag in [11,22]))) and TMenuItem(C).Visible)
  7.    else if C is TControl then TControl(C).Visible := ((c.Tag = 22) and pPrivate) or ((c.Tag = 11) and pTest) or (((not (c.Tag in [11,22]))) and TControl(C).Visible);
  8. end;

Bazzao

  • Full Member
  • ***
  • Posts: 178
  • Pies are squared.
Re: Loop Thru Components Setting Visibility And Enableability
« Reply #28 on: September 11, 2017, 05:48:01 am »
I am putting in the effort. :D

I came up with Byte(C.Tag)  :D

and I am field testing it.  :D

It takes a lot to insert new code in, disable the current stuff, and go through all iterations of false truths. 8-)

BTW,

Your latest code works.  ;D

Version5Point3.

B
Bazza

Lazarus 2.0.10; FPC 3.2.0; SVN Revision 63526; x86_64-win64-win32/win64
Windows 10.

Bazzao

  • Full Member
  • ***
  • Posts: 178
  • Pies are squared.
Re: [SOLVED] Loop Thru Components Setting Visibility And Enableability
« Reply #29 on: September 22, 2017, 01:40:09 am »
I've investigated the code and the possibility of Bitwise operation where a control or menu item can be made visible by two or more settings. Eg it may be visible by either parameter, not achievable with straight equals value.

Here is the code, with the 64 bit controlling private and bit 32 the test.

Code: Pascal  [Select][+][-]
  1. procedure SetVisibleByTags2(Parent:TWinControl; pPrivate,pTest:boolean);
  2. const cPrivBit=128; cTestBit=64;
  3. var I:integer;
  4.   procedure SetVis(C:TComponent);
  5.   var vSet,vVis:boolean;
  6.   begin
  7.     if C is TMenuItem then with C as TMenuItem do
  8.       begin
  9.         vVis:=Visible;
  10.         vSet:=false;
  11.         if (Byte(Tag) and cPrivBit)>0 then
  12.           begin
  13.             vVis:=pPrivate;
  14.             vSet:=true;
  15.           end;
  16.         if (Byte(Tag) and cTestBit)>0 then
  17.           if not vSet then
  18.             vVis:=pTest;
  19.         Visible:=vVis;
  20.       end
  21.     else
  22.       if C is TControl then with C as TControl do
  23.         begin
  24.           vVis:=Visible;
  25.           vSet:=false;
  26.           if (Byte(Tag) and cPrivBit)>0 then
  27.             begin
  28.               vVis:=pPrivate;
  29.               vSet:=true;
  30.             end;
  31.           if (Byte(Tag) and cTestBit)>0 then
  32.             if not vSet then
  33.               vVis:=pTest;
  34.           Visible:=vVis;
  35.         end;
  36.   end;
  37. begin
  38.   for I:=0 to Parent.ComponentCount-1 do
  39.     SetVis(Parent.Components[I]);
  40. end;      
  41.  

I only modifies one of the solutions, the others can be easily modified (I think).

All 4 iterations tested.

B
Bazza

Lazarus 2.0.10; FPC 3.2.0; SVN Revision 63526; x86_64-win64-win32/win64
Windows 10.

 

TinyPortal © 2005-2018