Skip to main content
4-Participant
October 30, 2025
Solved

Advanced Controls: TextBox reset in case of undefined variable

  • October 30, 2025
  • 1 reply
  • 319 views

Hello

I am using advanced control text box with python to do conditional formatting. Unfortunately if the variable is undefined I cannot reset the formatting to warn the user something is wrong with the control which results in previous value being displayed. Is there any way to to change the text string/background color  in case any variable is undefined?

MK_14262786_0-1761823027339.png


My code below:

def TextBoxEvent_Start():
# TODO: Add your code here
 
pass
 
def TextBoxEvent_Exec(Inputs, Outputs):
 
Outputs[0]['Value'] = TextBox.Text
S = Inputs[0]['Value']
A = Inputs[1]['Value']
 
stringText = TextBox.Text
TextBox.Text = str(round(A/S,2))
 
if S>A:
BackColor = TextBox.BackColor
TextBox.BackColor = "#FF0000"
else:
BackColor = TextBox.BackColor
TextBox.BackColor = "#92D050"
pass
 
def TextBoxEvent_Stop():
# TODO: Add your code here
pass


Thank you
Best regards 

Michal

Best answer by Werner_E

As far as I am aware there is no way to automatically determine if a variable is defined and act accordingly.

But it sure would be a nice to have.

You may consider to post a feature request at PTC support or an "idea" here -> Mathcad Ideas - PTC Community

1 reply

Werner_E25-Diamond IAnswer
25-Diamond I
October 30, 2025

As far as I am aware there is no way to automatically determine if a variable is defined and act accordingly.

But it sure would be a nice to have.

You may consider to post a feature request at PTC support or an "idea" here -> Mathcad Ideas - PTC Community

4-Participant
October 31, 2025

Thank you for the reply.

I agree that it is somewhat critical to know if the variable is undefined in the scripts.

Do you know what happens if the variable is undefined? Is the script in the control widget not executing at all? 
Unfortunately it seems like I do not have access MathCad Ideas part of the forum so I cannot post it.

25-Diamond I
October 31, 2025

Do you know what happens if the variable is undefined? Is the script in the control widget not executing at all? 

I don't know for sure but I guess that a scripted control is seen as a math region and so isn't evaluated at all if an undefined variable is used as input.