This is what I came up with. It works like a normal event. To use it, you do this (onLoopRegionChanged is the function that gets triggered when the user makes/changes the loop region - you can of course attach as many methods as you want to the event):
LoopRegionChangedEventHandler myLoopRegionHandler = new LoopRegionChangedEventHandler(myVegas);
myLoopRegionHandler.LoopRegionChanged +=
new LoopRegionChangedEventHandler.LoopRegionChangedHandler(onLoopRegionChanged);
And here is the class:
class LoopRegionChangedEventHandler
{
public delegate void LoopRegionChangedHandler();
public event LoopRegionChangedHandler LoopRegionChanged;
Timer myTimer;
internal Vegas myVegas;
Timecode curLoopStart = new Timecode();
Timecode curLoopLength = new Timecode();
internal LoopRegionChangedEventHandler(Vegas vegas)
{
myVegas = vegas;
curLoopLength.Nanos = myVegas.Transport.LoopRegionLength.Nanos;
curLoopStart.Nanos = myVegas.Transport.LoopRegionStart.Nanos;
myTimer = new Timer();
myTimer.Interval = 200;
myTimer.Tick += new EventHandler(myTimer_Tick);
myTimer.Start();
}
Hi,
I’m very interested about your code but unfortunately I can’t get it to work.
1/ there is a syntax error (onLoopRegionChanged > OnLoopRegionChanged)... not a big deal ;)))
2/After correcting the first error, I get this compilation error:
The name 'onLoopRegionChanged' does not exist in the current context
It's not a syntax error but it is definitely confusing because of all the similar names. I didn't know a way to write it that would be more clear though without the var names getting even longer.
It sounds like you are running the code without creating a method to be triggered. The method triggered is defined here:
myLoopRegionHandler.LoopRegionChanged +=
new LoopRegionChangedEventHandler.LoopRegionChangedHandler(onLoopRegionChanged);
The way the code above is written you need to make sure you have a method called onLoopRegionChanged declared somewhere.
private void onLoopRegionChanged()
{
/// code to trigger
MessageBox.Show("Loop region changed!");
}
Then you could add more methods just like any other event:
myLoopRegionHandler.LoopRegionChanged +=
new LoopRegionChangedEventHandler.LoopRegionChangedHandler(anotherActionToTakeWhenLoopRegionChanged);
If you registered the event handler in your DockableControl, you can remove it using the -= operator in the OnClosed() method. Add something like this to your DockableControl:
I usually register my event handlers during OnLoad() and unregister them in OnClose(). It makes it very easy to keep track of everything that way. I cut and aste from one to the other and change += to -= and I'm done. Hope this helps.
BTW, ScriptAddict nice piece of code. Thanks for sharing.
Thx JohnnyRoy,
This is what I’m trying to do but with no result… :((
Here part of my code:
public void HandleInvoked(Object sender, EventArgs args)
{
if (!myVegas.ActivateDockView("TL Cursor Position change monitor"))
{
DockableControl dockView = new DockableControl("TL Cursor Position change monitor");
dockView.DefaultFloatingSize = new Size(640, 480);
dockView.DefaultDockWindowStyle = DockWindowStyle.Docked;
CursorPositionMonitorForm = new Extension_Vegas1.UserControl1(myVegas);
dockView.Controls.Add(CursorPositionMonitorForm);
dockView.Closed += new EventHandler(dockView_Closed);
myVegas.LoadDockView(dockView);
cursorPositionChangedEventHandler myCursorPositionHandler = new cursorPositionChangedEventHandler(myVegas);
myCursorPositionHandler.cursorPositionChanged += new cursorPositionChangedEventHandler.cursorPositionChangedHandler(CursorPositionMonitorForm.onCursorPositionChanged);
}
}
void dockView_Closed(Object sender, EventArgs args)
{
// HERE THE CODE I'M LOOKING FOR
}
Glad you figured it out. I would have never seen that because I was thrown by your naming convention. I see that "cursorPositionChangedEventHandler" is a class. Generally it has been established that all class names should start with a capital letter so I immediately see lower case as a variable when I read OO code. You might want to change that to make it easier for others to read your code.