Recent

Author Topic: Can I change propierties from components in other forms?  (Read 10439 times)

clauslack

  • Sr. Member
  • ****
  • Posts: 275
Can I change propierties from components in other forms?
« on: April 06, 2011, 09:53:29 pm »
Can I change propierties from components in other forms?

This show Access Violation
Code: [Select]
procedure SeteaFuentesEx(Form1:TForm);
var componente:Tcomponent;i:integer;
begin
  for I :=0 to Form1.ComponentCount - 1 do begin
    componente := Form1.Components[I];
  if (componente is TBitBtn) then              TBitBtn(componente).Font:=GfuenteBotones;
  end;
end;

This work ok, but I must copy this procedure in all the forms of the project.
Code: [Select]
procedure TFORMcob_cuotas.SeteaFuentes;
var componente:Tcomponent;i:integer;

begin
  for I :=0 to ComponentCount - 1 do begin
    componente := Components[I];
    if (componente is TButton) then      Tbutton(componente).Font:=GfuenteBotones;

  end;
end;
     


Regards.

typo

  • Hero Member
  • *****
  • Posts: 3051
Re: Can I change propierties from components in other forms?
« Reply #1 on: April 06, 2011, 10:12:04 pm »
Declare Unit1 on the uses section of each unit you want to have access to the components of Form1.

clauslack

  • Sr. Member
  • ****
  • Posts: 275
Re: Can I change propierties from components in other forms?
« Reply #2 on: April 06, 2011, 10:20:53 pm »
Yes, but I need a routine, for use with any form. (I have 20 forms)

For no reapeat 20 times this code in each form :)

I pass the form has parameter  SeteaFuentesEx(Form1:TForm);




typo

  • Hero Member
  • *****
  • Posts: 3051
Re: Can I change propierties from components in other forms?
« Reply #3 on: April 06, 2011, 10:52:53 pm »
If you have this routine this way you will not have Access Violation.

Code: [Select]
// uses Unit1;
procedure TForm2.SeteaFuentesEx;
var componente:Tcomponent;i:integer;
begin
  for I :=0 to Form1.ComponentCount - 1 do begin
    componente := Form1.Components[I];
  if (componente is TBitBtn) then              TBitBtn(componente).Font:=GfuenteBotones;
  end;
end;
« Last Edit: April 06, 2011, 10:56:54 pm by typo »

Blaazen

  • Hero Member
  • *****
  • Posts: 3241
  • POKE 54296,15
    • Eye-Candy Controls
Re: Can I change propierties from components in other forms?
« Reply #4 on: April 06, 2011, 11:02:27 pm »
If I understand your goal well then you need 'with' construction.
When you pass Form as parameter, then:
Code: [Select]
procedure SeteaFuentesEx(Form: TForm);
var componente:Tcomponent;i:integer;
begin
  with Form do
    begin
      for I :=0 to ComponentCount - 1 do
        begin
          componente := Components[I];
          if (componente is TBitBtn) then TBitBtn(componente).Font:=GfuenteBotones;
        end;
    end;
end;

Then you can call:
Code: [Select]
SeteaFuentesEx(Form1);and similarly for all other forms.
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

clauslack

  • Sr. Member
  • ****
  • Posts: 275
Re: Can I change propierties from components in other forms?
« Reply #5 on: April 07, 2011, 01:36:36 pm »
@Typo
procedure TForm2.SeteaFuentesEx; works, but SeteaFuentesEx
is procedure from Form2, for other forms I must copy all the code.

procedure TForm1.SeteaFuentesEx;
.....
procedure TForm2.SeteaFuentesEx;
......
procedure TForm3.SeteaFuentesEx;
.....

@Blaazen, yes

I want to call
SeteaFuentesEx(Form1);
SeteaFuentesEx(Form2);
SeteaFuentesEx(Form3);
SeteaFuentesEx(Form4);
SeteaFuentesEx(Form5);
....
SeteaFuentesEx(Form20);

Is posible ?


eny

  • Hero Member
  • *****
  • Posts: 1648
Re: Can I change propierties from components in other forms?
« Reply #6 on: April 07, 2011, 02:04:21 pm »
...
Is posible ?

Yes, put your function in a separate unit (http://wiki.lazarus.freepascal.org/Unit) and add that unit to the 'uses' clause of your forms.

All posts based on: Win10 (Win64); Lazarus 3_4  (x64) 25-05-2024 (unless specified otherwise...)

clauslack

  • Sr. Member
  • ****
  • Posts: 275
Re: Can I change propierties from components in other forms?
« Reply #7 on: April 07, 2011, 04:59:49 pm »
@Eny
Look at this example
http://www.lazarus.freepascal.org/index.php/topic,12725.msg65765.html#msg65765

Not work, show Access Violation.

Blaazen

  • Hero Member
  • *****
  • Posts: 3241
  • POKE 54296,15
    • Eye-Candy Controls
Re: Can I change propierties from components in other forms?
« Reply #8 on: April 07, 2011, 05:26:52 pm »
The code is posted works. Where (which line) you have Access violation?
And did you properly created
Code: [Select]
GfuenteBotones:=TFont.Create; ?
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

eny

  • Hero Member
  • *****
  • Posts: 1648
Re: Can I change propierties from components in other forms?
« Reply #9 on: April 07, 2011, 07:22:18 pm »
@Eny
Look at this example
http://www.lazarus.freepascal.org/index.php/topic,12725.msg65765.html#msg65765

Not work, show Access Violation.
Sorry, my crystal ball broke yesterday; still waiting for a new one...

Please provide us with the circumstances how and when the AV occurs (use the debugger to find out).

And BTW: I did start reading from the top  >:(
All posts based on: Win10 (Win64); Lazarus 3_4  (x64) 25-05-2024 (unless specified otherwise...)

clauslack

  • Sr. Member
  • ****
  • Posts: 275
Re: Can I change propierties from components in other forms?
« Reply #10 on: April 07, 2011, 09:52:04 pm »
Upload an example (similar case) blank all edit from any form.
EJ: Blanquea_Edit(Form2);

@Blaazen my problem is with form createad dinamically.
With this show Access Violation
   Form := TForm2.Create( self );
   Form.ShowModal;

If the form is auto created (Project->Project Options->Forms-Auto-Create Forms) all work ok.

Check this example test.zip

Lazarus 0.9.31 svn 29023 fpc 2.4.3
Windows 2000 SP4

Thankssss




Blaazen

  • Hero Member
  • *****
  • Posts: 3241
  • POKE 54296,15
    • Eye-Candy Controls
Re: Can I change propierties from components in other forms?
« Reply #11 on: April 07, 2011, 10:12:56 pm »
Because you call:
Code: [Select]
Blanquea_Edit(Form2);  but Form2 was never created.

Yes, you did:
Code: [Select]
procedure TForm1.Button1Click(Sender: TObject);
var form:TForm2;
begin
Form := TForm2.Create( self );
Form.ShowModal;
end;   
but 'form' is only a local variable.

Solution: Create form to global variable Form2 and use:
Code: [Select]
procedure TForm1.Button1Click(Sender: TObject);
//var form:TForm2;
begin
Form2 := TForm2.Create( self );
Form2.ShowModal;
end; 
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

clauslack

  • Sr. Member
  • ****
  • Posts: 275
Re: Can I change propierties from components in other forms?
« Reply #12 on: April 07, 2011, 11:07:52 pm »
@Blaazen

Thanks you very much

now works fine !!!  :)

 

TinyPortal © 2005-2018