3
$\begingroup$

I have something like a "mortgage calculator", and I now want to colour the panel depending on the inputs that the user has made in my input fields.

This would be the code:

Panel[Column[{InputField[Dynamic[purchaseprice], Number, 
FieldHint -> "Purchase Price"], 
InputField[Dynamic[EK], Number, FieldHint -> "Equity capital"], 
InputField[Dynamic[mortgagerate], Number, 
FieldHint -> "Mortgage Rate in %"], 
InputField[Dynamic[supplymentary], Number, 
FieldHint -> "Supplementary costs per annum"], 
InputField[Dynamic[paybackperiod], Number, 
FieldHint -> "Payback Period"], 
InputField[Dynamic[grossincome], Number, 
FieldHint -> "Gross Income per annum"], 
InputField[Dynamic[calcrate], Number, 
FieldHint -> "Calculatory mortgage rate"]}]]

I tried to include something like:

Which[grossincome*0.33 > (purchaseprice - EK)*mortgagerate/100 + 
supplymentary + 
Max[{((purchaseprice - EK) - 0.66*purchaseprice)/paybackperiod, 
 0}], Background -> Green, Background -> Red]

so in the case of fullfilling of the condition the panel gets coloured green, otherwise red. However I did not manage to integrate it in the panel, given my dynamic inputs.

Any help would be much appreciated!

$\endgroup$
5
$\begingroup$

Try wrapping the Option value in Dynamic:

Panel[Column[{InputField[Dynamic[purchaseprice], Number, 
    FieldHint -> "Purchase Price"], 
   InputField[Dynamic[EK], Number, FieldHint -> "Equity capital"], 
   InputField[Dynamic[mortgagerate], Number, FieldHint -> "Mortgage Rate in %"], 
   InputField[Dynamic[supplymentary], Number, 
    FieldHint -> "Supplementary costs per annum"], 
   InputField[Dynamic[paybackperiod], Number, FieldHint -> "Payback Period"], 
   InputField[Dynamic[grossincome], Number, FieldHint -> "Gross Income per annum"], 
   InputField[Dynamic[calcrate], Number, 
    FieldHint -> "Calculatory mortgage rate"]}], 
 Background -> 
  Dynamic@If[
    grossincome*0.33 > (purchaseprice - EK)*mortgagerate/100 + supplymentary + 
      Max[{((purchaseprice - EK) - 0.66*purchaseprice)/paybackperiod, 0}], Green, 
    Red]]
$\endgroup$
  • $\begingroup$ Oh I see, you colored the background of the panel itself. I thought OP referred to each field, but upon re-reading the question, you are probably right! Maybe both approaches might be of use. (+1) $\endgroup$ – MarcoB Jun 26 '18 at 14:18
3
$\begingroup$

You can use Dynamic directives for the Background and BaseStyle options of each InputField that depend on the value of the variable set by that field:

InputField[
  Dynamic@EK, Number, FieldHint -> "Equity capital",
  Background -> Dynamic@If[EK > 0, None, Red],
  BaseStyle -> Dynamic@If[EK > 0, Black, White]
]

enter image description here

At the moment, updates happen when the user presses Return to confirm entry. You may also want to consider setting ContinuousAction -> True for each field, however: this will force updating continuously as the input is being typed in. You will want to tread carefully there though, as it can be tricky to deal with incomplete inputs as they are being typed in.

$\endgroup$
  • $\begingroup$ Thanks a lot for your answer, but I ideally would need to colour the whole panel, not just the input field, maybe its really easy and I just dont see it, but if you could point it out to me... $\endgroup$ – julsto Jun 26 '18 at 14:20
  • $\begingroup$ @julsto I think Mr. Wizard's answer above should do what you want then. If not, perhaps you could clarify what exactly is still needed, and I'll see what I can do. $\endgroup$ – MarcoB Jun 26 '18 at 14:35
  • $\begingroup$ it does exactly, what I need, but thanks for taking care. $\endgroup$ – julsto Jun 26 '18 at 14:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.