Jump to content


Photo

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


  • Please log in to reply
13 replies to this topic

#1 ielite

ielite
  • Members
  • 692 posts

Posted 16 February 2016 - 05:38 PM

I have two forms, form1 and form2. I am trying to check the checkbox in form1 from form2. How do I do this?

 

Here is my CODE

 

Thanx


  • two_dog_knight likes this

#2 B. SEZER

B. SEZER
  • Members
  • 14 posts
  • LocationTurkey

Posted 16 February 2016 - 05:45 PM

Hi Please have a look at my demo code.

 

http://wikisend.com/.../546832/SMS.zip



#3 ielite

ielite
  • Members
  • 692 posts

Posted 16 February 2016 - 06:40 PM

I have updated my link so it can now be seen on Git


  • two_dog_knight likes this

#4 markus_ja

markus_ja
  • Members
  • 319 posts
  • LocationAustria

Posted 16 February 2016 - 06:41 PM

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)



#5 ielite

ielite
  • Members
  • 692 posts

Posted 16 February 2016 - 06:57 PM

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)


  • two_dog_knight likes this

#6 markus_ja

markus_ja
  • Members
  • 319 posts
  • LocationAustria

Posted 16 February 2016 - 07:24 PM

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:


#7 Laksekjønn

Laksekjønn
  • Members
  • 428 posts

Posted 16 February 2016 - 09:59 PM

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;
 

  • ielite likes this

#8 ielite

ielite
  • Members
  • 692 posts

Posted 16 February 2016 - 10:02 PM

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?????


  • two_dog_knight likes this

#9 Laksekjønn

Laksekjønn
  • Members
  • 428 posts

Posted 17 February 2016 - 04:50 AM

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;

  • ielite likes this

#10 B. SEZER

B. SEZER
  • Members
  • 14 posts
  • LocationTurkey

Posted 17 February 2016 - 10:11 AM

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;
 
 


#11 Laksekjønn

Laksekjønn
  • Members
  • 428 posts

Posted 17 February 2016 - 03:20 PM

 
{ 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;

  • ielite and Nico Wouterse like this

#12 B. SEZER

B. SEZER
  • Members
  • 14 posts
  • LocationTurkey

Posted 18 February 2016 - 11:36 AM

 
{ 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?



#13 gabr42

gabr42

    Boss

  • Administrators
  • 192 posts

Posted 29 February 2016 - 07:24 PM

Solution IV: Move {$I 'FrmMain:intf'} from the 'private' section to 'public' and all components will become visible to other forms.


  • ielite, Nico Wouterse and B. SEZER like this

#14 Czar

Czar
  • Members
  • 131 posts

Posted 31 May 2017 - 04:31 AM

This was a very useful thread, we could not figure out how to read properties on other forms. 

Thanks






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users