Topic: Conditional enable / disable

Hi Guys,


This is for questionnaire type of situations. For example, I have question "Do you smoke?" If user answers yes, then following field (memo, textbox etc) to question becomes editable (active), if answered no, then field becomes disabled. How can I do it?


For the question, radio button component would be ideal, since we don't have it in MVD yet, I'm thinking to use checkbox component. Would that be correct or should I use combo with yes and no?

Adam
God... please help me become the person my dog thinks I am.

Re: Conditional enable / disable

Quick reply :


You can create radioButtons by script, the only problem is that I haven't figured out yet how to associate an event when one of the radiobutton is checked.


What I do known, is how to make sure that only one checbox is checked on a from, acting kind of like the radiobuttons.


In the attached project, you'll see how :
- how to create radiobuttons
- how to associate onClick event with checkboxes to only have one checked.


Have fun and... a good week-end


Cheers


Mathias

Post's attachments

Attachment icon RadioButtons.zip 332.82 kb, 464 downloads since 2016-10-22 

I'm a very good housekeeper !
Each time I get a divorce, I keep the house

Zaza Gabor

Re: Conditional enable / disable

Hi Mathias,


Thanks a lot for the sample file......


Nice example for using checkboxes as radio buttons...


I wanted to do this but couldn't get it working:

procedure Form1_Memo1_OnClick (Sender: string; var Cancel: boolean);
begin
    Form1.Memo1.Enabled := False;
end;

procedure Form1_CheckBox1_OnClick (Sender: string);
begin
    if Form1.CheckBox1.Checked = true then Form1.Memo1.Enabled := True;
end;
Adam
God... please help me become the person my dog thinks I am.

Re: Conditional enable / disable

Hello AD1408

you could inspire you the code I use to enable (or disable) the 3D display of the graph in my application

procedure Form1_CheckBox1_OnClick (Sender: string);
begin
    If not Form1.CheckBox1.Checked then
      ChartLine.View3D := False    //  On désactive la fonction 3D
    Else
    If Form1.CheckBox1.Checked then
       ChartLine.View3D := True;    //  On active la fonction 3D;

Hope this helps you

JB

Re: Conditional enable / disable

Re hello AD1408

An another point to you (If I've well understood what you wish).

Instead using Form1.Memo1.Enabled, I would choose Form1.Memo1.Visible;  (False or True)

Therefore, the memo is hidden but remains available making it visible again

JB

Re: Conditional enable / disable

Thanks a lot JB.......


Unless I'm doing something wrong, your code with "if then else" returns error.


I know about hiding (thanks to Derek), but on this occasion it is not suitable as it'd leave some blank spaces on the form.

Adam
God... please help me become the person my dog thinks I am.

Re: Conditional enable / disable

Hello AD1408

What says your error message ?
Can you show me the code you use from mine and adapted to your projets ?

Thanks
JB

Re: Conditional enable / disable

Hello Adam, Jean, Mathias,
As always, I try to keep things simple (so I can understand them!!).  So I would...
1.  use a combobox;  it forces the user to make just one choice (like a radio button) but you don't have to add any script.
2.  use a groupbox to hold edit and memo fields so you only need to set the enabled property once (for the groupbox, not for each field).
Also, i would colour code the edit and memo fields as a visual reminder to the user that they were / were not available depending on the combobox value.
In the attached example, I've used different drop down values for the combobox response depending on the question - it would be easier just to use 'yes/no' but it makes it a bit more specific.
Hope this gives you some ideas.
Derek.

Post's attachments

Attachment icon adhquestionnaire.zip 338.02 kb, 456 downloads since 2016-10-22 

Re: Conditional enable / disable

Hi JB,

Error message was due to my typo. The script below doesn't return error but still didn't work. I'm sure it's due to my incorrect implementation.

procedure Form1_Memo1_OnClick (Sender: string);
begin
    If not Form1.CheckBox1.Checked then
      Form1.Memo1.enabled := False
    Else
    If Form1.CheckBox1.Checked then
       Form1.Memo1.enabled := True;
 end;

-------------------------------------------


Hi Derek,


Thanks a lot for the working sample...........


I love your ingenious approaches and solutions. Brilliant mind. I bet your IQ is well over 200.

Adam
God... please help me become the person my dog thinks I am.

Re: Conditional enable / disable

Hello AD1408

Yes, you have to trigger this code not behind Form1_Memo1_OnClick but behind this one which has to
display the Memo.
If you launch the memo whuen you click on an image : Form1_Image1_OnClick
If you launch it from a checkbox, : Form1.CheckBox1_OnClick ans so on whatever the trigger

JB

Re: Conditional enable / disable

Hi Again,
What I forgot to do (duh!) was to set the 'first item empty' object property to 'false' for the comboboxes in my earlier example;  it's not a show-stopper, but nevertheless, apologies.
Derek.

Post's attachments

Attachment icon adhquestionnaire.zip 338.16 kb, 468 downloads since 2016-10-22 

Re: Conditional enable / disable

Thanks a lot guys..................................

Adam
God... please help me become the person my dog thinks I am.