IElite 280 Posted February 16, 2016 Report Share Posted February 16, 2016 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 1 Quote Link to post Share on other sites
bilcan 2 Posted February 16, 2016 Report Share Posted February 16, 2016 Hi Please have a look at my demo code. http://wikisend.com/download/546832/SMS.zip Quote Link to post Share on other sites
IElite 280 Posted February 16, 2016 Author Report Share Posted February 16, 2016 I have updated my link so it can now be seen on Git two_dog_knight 1 Quote Link to post Share on other sites
markus_ja 114 Posted February 16, 2016 Report Share Posted February 16, 2016 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) Quote Link to post Share on other sites
IElite 280 Posted February 16, 2016 Author Report Share Posted February 16, 2016 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 1 Quote Link to post Share on other sites
markus_ja 114 Posted February 16, 2016 Report Share Posted February 16, 2016 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: Quote Link to post Share on other sites
warleyalex 436 Posted February 16, 2016 Report Share Posted February 16, 2016 You could use the Assembly language mov dx, 0mov ax, 1234mov bx, 10div 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 1 Quote Link to post Share on other sites
IElite 280 Posted February 16, 2016 Author Report Share Posted February 16, 2016 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 1 Quote Link to post Share on other sites
warleyalex 436 Posted February 17, 2016 Report Share Posted February 17, 2016 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 1 Quote Link to post Share on other sites
bilcan 2 Posted February 17, 2016 Report Share Posted February 17, 2016 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; Quote Link to post Share on other sites
warleyalex 436 Posted February 17, 2016 Report Share Posted February 17, 2016 { 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 lynkfs 2 Quote Link to post Share on other sites
bilcan 2 Posted February 18, 2016 Report Share Posted February 18, 2016 { 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? Quote Link to post Share on other sites
Administrators gabr42 124 Posted February 29, 2016 Administrators Report Share Posted February 29, 2016 Solution IV: Move {$I 'FrmMain:intf'} from the 'private' section to 'public' and all components will become visible to other forms. bilcan, lynkfs and IElite 3 Quote Link to post Share on other sites
Czar 125 Posted May 31, 2017 Report Share Posted May 31, 2017 This was a very useful thread, we could not figure out how to read properties on other forms. Thanks Quote Link to post Share on other sites
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.