My Purpose is to reduce Opacity onDeactivate event because the form has the setting "Stay on Top".
I tried to do this :
protected override void OnDeactivate(EventArgs e)
{
base.OnDeactivate(e);
Opacity = 0.7;
}
All works perfectly (I click on another window and my form's opacity reduces) till I close the form. If I close the form, I get this error :
System.ComponentModel.Win32Exception (0x80004005): Parametro non corretto in System.Windows.Forms.Form.UpdateLayered() in System.Windows.Forms.Form.set_Opacity(Double value) in Hazar.Form1.OnDeactivate(EventArgs e) in C:\Users\lrena\source\repos\Hazar\Hazar\Form1.cs:riga 44 in System.Windows.Forms.Form.set_Active(Boolean value) in System.Windows.Forms.Form.WmActivate(Message& m) in System.Windows.Forms.Form.WndProc(Message& m) in Hazar.Form1.WndProc(Message& m) in C:\Users\lrena\source\repos\Hazar\Hazar\Form1.cs:riga 81 in System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) in System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) in System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
You are getting this exception because you are trying to access a property of a control which is in the process of disposing. The Deactivate
event is raised also after the FormClosed
event. Meaning, you need to check the Control.Disposing
property in the OnDeactivate
delegate before setting or calling any property or method:
protected override void OnDeactivate(EventArgs e)
{
base.OnDeactivate(e);
if (!this.Disposing)
Opacity = 0.7;
}
Also you might benefit from the Control.IsDisposed
property which returns true
if the control (the Form in your case) has been disposed of. Use it to check the global/class variables if you have any.
User contributions licensed under CC BY-SA 3.0