Use event or call a method from another class

A forum devoted to the discussion of all topics having to do with scripting and other advanced programming using iX Developer.
Post Reply
vilius
Posts: 10
Joined: Mon Feb 06, 2012 8:36 am

Use event or call a method from another class

Post 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!

mark.monroe
Posts: 824
Joined: Tue Mar 13, 2012 9:53 am

Re: Use event or call a method from another class

Post 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
Snap 2012-04-06 at 08.20.21.jpg (62.99 KiB) Viewed 10189 times
Best Regards,
Mark Monroe

Beijer Electronics, Inc. | Applications Engineer

vilius
Posts: 10
Joined: Mon Feb 06, 2012 8:36 am

Re: Use event or call a method from another class

Post 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?

mark.monroe
Posts: 824
Joined: Tue Mar 13, 2012 9:53 am

Re: Use event or call a method from another class

Post 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;
			}		
		}
    }
}
Best Regards,
Mark Monroe

Beijer Electronics, Inc. | Applications Engineer

BobLivingston
Posts: 19
Joined: Tue Jan 17, 2012 8:57 am

Re: Use event or call a method from another class

Post 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;
}
Bob Livingston
Sr Controls Engineer
FlexEnergy Energy Systems
30 New Hampshire Ave
Portsmouth, NH 03801
USA

Post Reply