Jump to content
IElite

How to change the properties of a control in a form from another form

Recommended Posts

Your link is broke.

 

You can assign an event to form2. So when you check the Checkbox you can invoke that event.

//Form1

var fm := TForm2.Create(self);
fm.OnCheckboxChecked := procedure(Sender: TObject) 
begin
  //...do some action
end; 

Have a look int the Frames example (Forms & Components)

Share this post


Link to post
Share on other sites

Hi,

 

No, that is not what I am attempting to do. I need to change the checkbox from the other form. Checkbox resides on form1

 

procedure TForm2.W3Button2Click(Sender: TObject);
begin
 if form1.W3CheckBox1.Checked then
  form1.W3CheckBox1.Checked:= false
 else
  form1.W3CheckBox1.Checked:= true;
end;

 

and at this point, it doesn't know what W3Checkbox1 is, even though form1, and Smart.Controls.CheckBox are in the uses clause

 

 

Your link is broke.

 

You can assign an event to form2. So when you check the Checkbox you can invoke that event.

//Form1

var fm := TForm2.Create(self);
fm.OnCheckboxChecked := procedure(Sender: TObject) 
begin
  //...do some action
end; 

Have a look int the Frames example (Forms & Components)

Share this post


Link to post
Share on other sites

In Form1:

procedure Form1.CreateForm2;
begin
  var fm := TForm2.Create(self);
  fm.OnCheckboxAction := procedure 
    begin
      if W3CheckBox1.Checked then //form1.W3CheckBox1
        W3CheckBox1.Checked:= false
      else
        W3CheckBox1.Checked:= true;
    end;
  
end;

In Form2:

procedure TForm2.W3Button2Click(Sender: TObject);
begin
  if assigned(FOnCheckoxAction) then
    FOnCheckboxAction;  //sets the checkbox in form1
end:

Share this post


Link to post
Share on other sites

You could use the Assembly language

 

mov dx, 0
mov ax, 1234
mov bx, 10
div bx ; Divides 1234 by 10. DX = 4 and AX = 123

 

...but there's another simple and incredibly quick way

 

{ FORM1 }

procedure TForm1.InitializeObject;

begin
  inherited;
  {$I 'Form1:impl'}
  W3CheckBox1.Handle.id := 'checkbox1';
 
{ FORM2 }
var
  HND: boolean;
begin
  HND:= BrowserAPI.Window.document.getElementById('checkbox1').childNodes[1].checked;
  BrowserAPI.Window.document.getElementById('checkbox1').childNodes[1].checked := not HND;
 

Share this post


Link to post
Share on other sites

Wow, why can't one form see and change properties of controls in another form by just adding the form to the uses clause

 

SMS is suppose to work like delphi :)   - just kidding

 

Really, it it that cumbersome to change properties of controls between forms?????

Share this post


Link to post
Share on other sites

Probably there's another better approach, for instance, for more high level, would be create a global intermediate variable, you can use this variable to get/set values between forms.

{ FORM1 }
type
  TForm1 = class(TW3Form)
    class var state: boolean;
  ...
  protected
    procedure FormActivated; override;

procedure TForm1.InitializeForm;
begin
  inherited;
  // this is a good place to initialize components
  state := W3CheckBox1.Checked; // get current status
end;

procedure TForm1.FormActivated;
begin
  inherited;
  W3CheckBox1.Checked := state;  // set value
end;
//--------------------------------------------------
{ FORM2 }
procedure TForm2.btn3Click(Sender: TObject);
begin
  Form1.TForm1.state := not Form1.TForm1.state; // Toggle checkbox value
end;

Share this post


Link to post
Share on other sites

This is my way, I have not found better way so far. I tried to access directly to BtnGiris but I got "Syntax Error: Member symbol "BtnGiris" is not visible from this scope [line: 91, column: 51, file: miniClientUnit]"

 

By the way, I'd like to thank you @ielite, he opened the topic for me.

 

{Form 1}

unit FrmMain;
 
interface
 
uses 
  SmartCL.System, SmartCL.Graphics, SmartCL.Components, SmartCL.Forms, 
  SmartCL.Fonts, SmartCL.Borders, SmartCL.Application, SmartCL.Controls.Panel,
  SmartCL.Controls.EditBox, SmartCL.Controls.Label, SmartCL.Controls.Button;
 
type
  TFrmMain = class(TW3Form)
    procedure BtnGirisClick(Sender: TObject);
  public
    procedure MakeBtnGirisEnable;
  private
    {$I 'FrmMain:intf'}
  protected
    procedure InitializeForm; override;
    procedure InitializeObject; override;
    procedure Resize; override;
  end;
.....
procedure TFrmMain.MakeBtnGirisEnable;
begin
  BtnGiris.Enabled := True;
end;
 
 
{Form2 or another unit}
.....
procedure TminiClient.SocketMessage(sender: TW3WebSocket; Message: TWebSocketMessageData);
begin
   ......
      TFrmMain(Application.FormByName('FrmMain')).MakeBtnGirisEnable;
end;
 
 

Share this post


Link to post
Share on other sites
 

{ SOLUTION I } // use raw smart-javascript

  HND:= BrowserAPI.Window.document.getElementById('checkbox1').childNodes[1].checked;

  BrowserAPI.Window.document.getElementById('checkbox1').childNodes[1].checked := not HND;

 

{ SOLUTION II } // use an intermediate global variable to get/set

  Form1.TForm1.state := not Form1.TForm1.state;

 

{ SOLUTION III } // use W3CheckBox1 component as public (global)

  TForm1(Application.FormByName('Form1')).W3CheckBox1.Checked :=

        not TForm1(Application.FormByName('Form1')).W3CheckBox1.Checked;

Share this post


Link to post
Share on other sites
 
{ SOLUTION III } // use W3CheckBox1 component as public (global)
  TForm1(Application.FormByName('Form1')).W3CheckBox1.Checked :=
        not TForm1(Application.FormByName('Form1')).W3CheckBox1.Checked;

 

Hi, how can I declare W3CheckBox1 as public?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×