Recent

Author Topic: [SOLVED]Enabling and Disabling Controls at Runtime if Checkbox is Checked  (Read 1082 times)

Matt723

  • New Member
  • *
  • Posts: 20
Hello,

FloatSpinEdit1 and FloatSpinEdit2 are set to Enabled := False in the designer and FloatSpinEdit3 is set to Enabled := True in the designer. I have a CheckBox that, if checked, I want FloatSpinEdit1 and FloatSpinEdit2 to Enable := True, and FloatSpinEdit3 to Enable := False.

Here is the code that I have tried:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.CheckBox1Click(Sender: TObject);
  2. begin
  3.   If Checkbox1.Checked := True Then
  4.     begin
  5.     FloatSpinEdit1.Enabled := True;
  6.     FloatSpinEdit2.Enabled := True;
  7.     FloatSpinEdit3.Enabled := False;
  8.     end
  9.   else
  10.     FloatSpinEdit1.Enabled := False;
  11.     FloatSpinEdit2.Enabled := False;
  12.     FloatSpinEdit3.Enabled := True;
  13. end;

However, although this code throws no errors, it does absolutely nothing and I can't wrap my head round why it won't.

Thanks in advance!
« Last Edit: January 23, 2020, 05:25:18 pm by Matt723 »

PaulRowntree

  • Full Member
  • ***
  • Posts: 132
    • Paul Rowntree
Re: Enabling and Disabling Controls at Runtime if Checkbox is Checked
« Reply #1 on: January 23, 2020, 05:01:42 pm »
Two things
a) You can just use  "if Checkbox1.Checked" .... since it is already a boolean valueb) the 'else' case doesn't have the expected 'begin ... end' structure

Does that make a difference?
Paul Rowntree
- coding for instrument control, data acquisition & analysis, CNC systems

Matt723

  • New Member
  • *
  • Posts: 20
Re: Enabling and Disabling Controls at Runtime if Checkbox is Checked
« Reply #2 on: January 23, 2020, 05:06:38 pm »
Hi Paul,

Cheers for the input, but the following still does nothing:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.CheckBox1Click(Sender: TObject);
  2. begin
  3.   If Checkbox1.Checked Then
  4.     begin
  5.     FloatSpinEdit1.Enabled := True;
  6.     FloatSpinEdit2.Enabled := True;
  7.     FloatSpinEdit3.Enabled := False;
  8.     end
  9.   else
  10.     begin
  11.     FloatSpinEdit1.Enabled := False;
  12.     FloatSpinEdit2.Enabled := False;
  13.     FloatSpinEdit3.Enabled := True;
  14.     end;
  15. end;  

Thank you

wp

  • Hero Member
  • *****
  • Posts: 7636
Re: Enabling and Disabling Controls at Runtime if Checkbox is Checked
« Reply #3 on: January 23, 2020, 05:14:20 pm »
This code is correct, I tested it - see attached demo.

But is the CheckBox1Click code assigned to the OnClick event of the CheckBox? In other words: when you go to the OnClick event of the Checkbox in the Object Inspector is the CheckBox1Clicked code displayed? If not click on the '...' button and select the method from the dropdown.
Mainly Lazarus trunk / fpc 3.2.0 / all 32-bit on Win-10, but many more...

Matt723

  • New Member
  • *
  • Posts: 20
Re: Enabling and Disabling Controls at Runtime if Checkbox is Checked
« Reply #4 on: January 23, 2020, 05:24:57 pm »
Thanks wp, that's fixed now. Somehow some other checkbox on the form was in the OnClick event in the properties, as you thought.

PaulRowntree

  • Full Member
  • ***
  • Posts: 132
    • Paul Rowntree
Re: Enabling and Disabling Controls at Runtime if Checkbox is Checked
« Reply #5 on: January 23, 2020, 05:40:17 pm »
Hi Paul,

Cheers for the input, but the following still does nothing:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.CheckBox1Click(Sender: TObject);
  2. begin
  3.   If Checkbox1.Checked Then
  4.     begin
  5.     FloatSpinEdit1.Enabled := True;
  6.     FloatSpinEdit2.Enabled := True;
  7.     FloatSpinEdit3.Enabled := False;
  8.     end
  9.   else
  10.     begin
  11.     FloatSpinEdit1.Enabled := False;
  12.     FloatSpinEdit2.Enabled := False;
  13.     FloatSpinEdit3.Enabled := True;
  14.     end;
  15. end;  

Thank you
Good that it now works.  if you want to simplify this, you could simply write
Code: Pascal  [Select][+][-]
  1. procedure TForm1.CheckBox1Click(Sender: TObject);
  2. begin
  3.     FloatSpinEdit1.Enabled := Checkbox1.Checked;
  4.     FloatSpinEdit2.Enabled := Checkbox1.Checked;
  5.     FloatSpinEdit3.Enabled := not Checkbox1.Checked;
  6. end;
Paul Rowntree
- coding for instrument control, data acquisition & analysis, CNC systems

Matt723

  • New Member
  • *
  • Posts: 20
Hi Paul,

Cheers for the input, but the following still does nothing:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.CheckBox1Click(Sender: TObject);
  2. begin
  3.   If Checkbox1.Checked Then
  4.     begin
  5.     FloatSpinEdit1.Enabled := True;
  6.     FloatSpinEdit2.Enabled := True;
  7.     FloatSpinEdit3.Enabled := False;
  8.     end
  9.   else
  10.     begin
  11.     FloatSpinEdit1.Enabled := False;
  12.     FloatSpinEdit2.Enabled := False;
  13.     FloatSpinEdit3.Enabled := True;
  14.     end;
  15. end;  

Thank you
Good that it now works.  if you want to simplify this, you could simply write
Code: Pascal  [Select][+][-]
  1. procedure TForm1.CheckBox1Click(Sender: TObject);
  2. begin
  3.     FloatSpinEdit1.Enabled := Checkbox1.Checked;
  4.     FloatSpinEdit2.Enabled := Checkbox1.Checked;
  5.     FloatSpinEdit3.Enabled := not Checkbox1.Checked;
  6. end;

Yeah, that looks a lot tidier now, thanks.

 

TinyPortal © 2005-2018