Page 1 of 1
Use event or call a method from another class
Posted: Fri Apr 06, 2012 5:28 am
by vilius
Good afternoon,
i am using panel T10A and what I'm trying to do looks like this: I have one screen, Screen1, a button, Button1, in it and a tag, Tag1. I need the button to become disabled (Button1.IsEnabled = false) when Tag1 becomes equal to 1. I made two approaches to this.
First:
I created a public method (Disable) for disabling the Button1 in class Screen1 and I tried to call this method from class Tags when the the event Tag1_ValueChange detects that Tag1 is equal to 1. However, I cannot access method Disable from Tags class. This is how I try to call Disable method in class Tags:
Screen1 n = new Screen1();
n.Disable();
Second:
I tried to create an event Tag1_ValueChange in class Screen1 and disable Button1 when Tag1 == 1, however, it does nothing.
Could you help me out with this? If there's totally different solution which works to this, please let me know.
Thank you!
Re: Use event or call a method from another class
Posted: Fri Apr 06, 2012 8:24 am
by mark.monroe
The easiest way is to use button dynamics. Then you do not need to write any code. First select the button. Then go to the "Dynamics" section of the ribbon. Then click the 'General' button and go to the "IsEnabled" section. In the "IsEnabled" section you can select a tag you want to use to enable/disable the button.
- Snap 2012-04-06 at 08.20.21.jpg (62.99 KiB) Viewed 11256 times
Re: Use event or call a method from another class
Posted: Tue Apr 10, 2012 12:49 am
by vilius
Thank you, but this solution doesn't have enough functionality for my case.. Because in some cases I need a button to be enabled or disabled when Tag1 AND Tag2 is equal to 1. And I need constant monitoring of these tags state changes, so the best solution would be to use, for example, Tag1_ValueChange event handler. However, I cannot access it from Screen1 class... Could anyone help me?
Re: Use event or call a method from another class
Posted: Tue Apr 10, 2012 9:21 am
by mark.monroe
I would use the "Dynamics" I described earlier and set it to a Tag that you then change via a script depending on whether or not you want to enable the button.
You may have to turn on the "Always Active" value of the tag if you are not using that particular tag on the screen. Tags are only active when they are on a screen. The "Always Active" option is available when you select "Others" in the "Columns Visible" box on the Tags window.
The value change even can be used like this:
Code: Select all
public partial class Tags
{
void Tag1_ValueChange(System.Object sender, Neo.ApplicationFramework.Interfaces.Events.ValueChangedEventArgs e)
{
Enable_Disable();
}
void Tag2_ValueChange(System.Object sender, Neo.ApplicationFramework.Interfaces.Events.ValueChangedEventArgs e)
{
Enable_Disable();
}
void Enable_Disable()
{
if( Globals.Tags.Tag1.Value == 1 &&
Globals.Tags.Tag2.Value == 1 )
{
//Enable the button
Globals.Tags.Tag3.Value = 1;
}
else
{
//Disable the button
Globals.Tags.Tag3.Value = 0;
}
}
}
}
Re: Use event or call a method from another class
Posted: Mon May 14, 2012 1:22 pm
by BobLivingston
Vilius,
I had a similar issue. The solution I found was to add the following to the screen's script:
In the screen opened event:
Code: Select all
void Screen1_Opened(System.Object sender, System.EventArgs e)
{
Globals.Tags.Tag1.ValueChange += Enable_Button; // add ValueChange event
Enable_Button(sender, e); // initialize button state to current tag value
}
In the screen closing event:
Code: Select all
void Screen1_Closing(System.Object sender, System.ComponentModel.CancelEventArgs e)
{
Globals.Tags.Tag1.ValueChange -= Enable_Button; // remove ValueChange event
}
Finally, the value changed event handler:
Code: Select all
private void Enable_Button(System.Object sender, System.EventArgs e)
{
Button1.Enabled = Globals.Tags.Tag1.Value;
}