Clear textfields with Client Event Script

Hello.

I would like to ask for your help with this issue. I’m trying to create a SCRIPT to clear a group of textfields when a Client Tag changes its value. Basically I have the following code in the Client Tag change properties:

qv = system.tag.read("[client]ClearFieldsBit.Value")
if qv.value == 1 :
event.source.parent.getComponent(‘ntf_ControlLimitId’).intValue = 0
event.source.parent.getComponent(‘ntf_ControlLimitId’).intValue = 0
event.source.parent.getComponent(‘txf_Label’).text = ‘’
event.source.parent.getComponent(‘ntf_RevisionId’).intValue = 0
event.source.parent.getComponent(‘txf_Units’).text = ‘’
event.source.parent.getComponent(‘ntf_SetPoint’).intValue = 0
event.source.parent.getComponent(‘ntf_LowerAlarmLimit’).intValue = 0
event.source.parent.getComponent(‘ntf_UpperAlarmLimit’).intValue = 0
event.source.parent.getComponent(‘ntf_LowerControlLimit’).intValue = 0
event.source.parent.getComponent(‘ntf_UpperControlLimit’).intValue = 0
event.source.parent.getComponent(‘ntf_LowerSpecLimit’).intValue = 0
event.source.parent.getComponent(‘ntf_UpperSpecLimit’).intValue = 0
event.source.parent.getComponent(‘ntf_LowerReactionLimit’).intValue = 0
event.source.parent.getComponent(‘ntf_UpperReactionLimit’).intValue = 0
event.source.parent.getComponent(‘ntf_FirstThresholdValue’).intValue = 0
event.source.parent.getComponent(‘ntf_SecondThresholdValue’).intValue = 0
event.source.parent.getComponent(‘ntf_ThirdThresholdValue’).intValue = 0
event.source.parent.getComponent(‘Container’).getComponent(‘txf_KPVCategory’).text
event.source.parent.getComponent(‘Container’).getComponent(‘txf_KPVCategory’).text = ‘’
event.source.parent.getComponent(‘Container’).getComponent(‘txf_KPVDescription’).text = ‘’
event.source.parent.getComponent(‘Container’).getComponent(‘txf_ResourceDescription’).text = ‘’
event.source.parent.getComponent(‘Container’).getComponent(‘txf_KPVVariableType’).text = ‘’

At this point I can change tha value from 0 to 1 by clicking on a button. If I change the code justo to show a message box it works perfectly but if i try to delete the textfields and numeric fields when I click on the button nothing happens. Maybe I need to specify in which forms are the textfields located but I don’t know how.

Any comment, idea or advice is welcoming.

Best regards

Are the bold lines a copy paste mistake or is this the actual script?

1 Like

Hello…

I think is a copy paste mistake. Please, see the picture below.

Look at line 20, it isn’t doing anything

1 Like

I already change the line with the same results. It just don’t do anything. If I copy and paste this code into a button within the form I’m using the code works properly.

It looks like you are trying to do this with a client event script on tag change. Try adding

Import system
qv = system.tag.read("[client]ClearFieldsBit.Value")
...

EDIT
No I don’t think that will work, try this

qv = system.tag.read("[client]ClearFieldsBit.Value")
if qv.value == 1 :
  main = system.gui.getWindow('yourWindowName')
  main.getRootContainer().getComponent('ntf_ControlLimitId').intValue = 0
…
1 Like

Hello.

I tried both solutions but unfortunately I could’n make the text field change it’s value. Any other idea?

Is the intent to clear these fields when you tap the “Clear all fields” button in your first screen clipping? If so, consider doing this directly in the actionPerformed script on the button, rather than in a tag event script on a tag set by the button.

I agree with @witman, but if you are dead set on the tag change event, shouldn’t the tag information be in the tag tab of the client script window? Then the clearing script is the only thing needed in the script tab. At least that is the way it is in v7.9

Your using a tag change script to try to clear components on a screen? Unless you tell it in your script what screen it needs to make the changes on, it won’t work. By what it looks like your trying to do, thats more complicated than it needs to be. It looks like your just trying to clear it when the button is pressed, if that is true, then I would drop the tag change script all together and do it like @witman mentioned. If you do it that way, your script doesn’t need the if or system.tag.read(), in the action performed section of your script you just need to set the values. I would also use system.gui.confirm() to verify the user really wants to clear everything before you do it.

To explain it a little better, the client tag change script is being triggered by a client tag, the client tag isn’t linked to a specific screen, even if you only trigger it from one screen the “event.source.parent.getComponent” lines you have, have no way to know where they would need to target to make the change. If you want to do it through a tag change script like this, all of those values would have to be in tags somewhere doing writes to them, or you would have to give it a full path doing something like:

win = system.gui.getWindow("your window path")
win.rootContainer.getComponent('your component name').initValue = 0

@dkhayes117 showed the same thing above on his edit. If your button is what changes the client tag value though, I would do it from the button instead of the extra scripting. Also if you trigger it and you don’t have the screen open that it is referencing then the script will fail. Same if you navigate away from the screen before the script completes. If your doing it that way, I would set your tag using the button and reset it at the end of your script by doing a tag write. If your determined to reset it from the client tag change script then I think you should do it against tags instead of trying to change values on screen objects.

2 Likes

Hello guys:

After analize your comments and make some test finally I’m able to change the text field values running a Scrip when an specific tag change its state. The code that I used is the following:

import system
qv = system.tag.read("[client]ClearFieldsBit.Value")
if qv.value == 1 :
	selwindow = system.gui.getWindow('Main Windows/Control de procesos Windows/ControlLimits')
	selwindow.getRootContainer().getComponent("Container").getComponent('Label1').text = 'Buenas'
	

Now, the problem is that I can’t change the values in numeric text fields. I tried to use the following line of code:

selwindow.getRootContainer().getComponent("Container").getComponent('ntf_ControlLimitId').intValue = 0

I tried by change the .intValue for .value, .floatValue, .text and always the same result. The whole designer freezes and I need to restart the program.

If I enter the code in the SCRIPT CONSOLE I have the same results. If I remove the " =0", I can see the information within the numeric text field number in the script console.

Any advice or idea is welcoming.

Regards.

Assuming the paths in your original code are correct relative paths to your input components from your “Clear all fields” button,
image
place the below code in the action performed event of the “Clear all fields” button


and it should do what you are looking for.

event.source.parent.getComponent('ntf_ControlLimitId').intValue = 0
event.source.parent.getComponent('ntf_ControlLimitId').intValue = 0
event.source.parent.getComponent('txf_Label').text = ''
event.source.parent.getComponent('ntf_RevisionId').intValue = 0
event.source.parent.getComponent('txf_Units').text = ''
event.source.parent.getComponent('ntf_SetPoint').intValue = 0
event.source.parent.getComponent('ntf_LowerAlarmLimit').intValue = 0
event.source.parent.getComponent('ntf_UpperAlarmLimit').intValue = 0
event.source.parent.getComponent('ntf_LowerControlLimit').intValue = 0
event.source.parent.getComponent('ntf_UpperControlLimit').intValue = 0
event.source.parent.getComponent('ntf_LowerSpecLimit').intValue = 0
event.source.parent.getComponent('ntf_UpperSpecLimit').intValue = 0
event.source.parent.getComponent('ntf_LowerReactionLimit').intValue = 0
event.source.parent.getComponent('ntf_UpperReactionLimit').intValue = 0
event.source.parent.getComponent('ntf_FirstThresholdValue').intValue = 0
event.source.parent.getComponent('ntf_SecondThresholdValue').intValue = 0
event.source.parent.getComponent('ntf_ThirdThresholdValue').intValue = 0
event.source.parent.getComponent('Container').getComponent('txf_KPVCategory').text = ''
event.source.parent.getComponent('Container').getComponent('txf_KPVCategory').text = ''
event.source.parent.getComponent('Container').getComponent('txf_KPVDescription').text = ''
event.source.parent.getComponent('Container').getComponent('txf_ResourceDescription').text = ''
event.source.parent.getComponent('Container').getComponent('txf_KPVVariableType').text = ''
2 Likes

Hello Bpreston. My intention was to create a script to clear all the fields in different situations not just when I press the button. As you can see there are lots of text fields and I didn’t wanted to type the same lineas in different scripts. As I mention in my last post, finally and thanks to your answer I could make a script to clear the text fields but not to clear the numeric fields. For the moment due to timing reasons I had to put all the lines in the different conditions and situations in which I need to clear the fields, but, I still looking for a solution to clear or modify the numeric text fields in the same scripts. I really appreciate your support guys.

Best regards.

Hello Witman.

At the moment I had to use your solution. My intention was to create a script to clear all the fields in different situations not just when I press the button. As you can see there are lots of text fields and I didn’t wanted to type the same lineas in different scripts. For the moment due to timing reasons I had to put all the lines in the different conditions and situations in which I need to clear the fields, but, I still looking for a solution to clear or modify the numeric text fields in the same scripts. I really appreciate your support guys.

By the way, in the past I used to program in visual studio (vb.net) and there was a function to get the name of all the components of the same sort in a form and put it in a variable (array). After that I was able to clear or modify all the components just by mention this variable. If any of you has an idea of how to do that would be great.

Best regards.

You could put that script in a custom method on the clear all fields button (or on another component, like the root container)

(replace logic with the code above, noting that self replaces event.source and then call the custom method from everywhere that needs to clear the fields. For example, if you want a tag change to call this, bind the tag to a custom property in the window and put a property change script on that custom property that calls this method when the property changes to true.

if event.newValue:
	# Modify this to match the path to the component with the custom method.
	event.source.parent.getComponent("ClearFieldsButton").clearAllFields()

If you put this code in a custom method on the clear all fields button, the actionPerformed event script on the button calls its own custom method like this:

event.source.clearAllFields()