'Issue with property setter with an object and toolbox
This code works fine, I can check if a timecode is valid when the
property changes its value. But the problem is that I cannot set the
value of this property in the toolbox, because of the new ValidTimeCodeEventArgs.
Is there a way to fix this so I could change the value in the toolbox and check if it is correct?
[Browsable(true)]
public event EventHandler<ValidTimeCodeEventArgs> ValidTimeCode;
private string strTimeCode;
[Category("Custom")]
[Browsable(true)]
[DefaultValue(typeof(string), "00:00:00:00")]
public string StrTimeCode {
get {
return strTimeCode;
}
set {
if (value != null) {
ValidTimeCode(this, new ValidTimeCodeEventArgs(IsValidTimeCode(value, FrameRate, IsDropFrame)));
strTimeCode = value;
initValue = strTimeCode;
Invalidate();
}
}
}
public class ValidTimeCodeEventArgs : EventArgs {
public ValidTimeCodeEventArgs(bool isValid) {
IsValid = isValid;
}
public bool IsValid { get; }
}
Solution 1:[1]
Your ValidTimeCode event probably doesn't have any subscribers when you try to raise it. Try:
ValidTimeCode?.Invoke(this, new ValidTimeCodeEventArgs(IsValidTimeCode(value, FrameRate, IsDropFrame)));
This basically does a null check on the handler (it's null if no one subscribed to the event), then only raises it if it isn't null. See here for more info.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | HasaniH |
