c# - Disable Alt+F4 in UserControl -
i have user control , want disable alt + f4 oportunity end user. when user control shows, there opportunity close alt + f4, program goes base class in method:
protected override void onclosing(system.componentmodel.canceleventargs e) { //content = null; // remove child logical parent - reusing purposes this.removelogicalchild(content); //this works faster base.onclosing(e); { gc.collect(); }; }
what must here or somewhere else, disable user control closing on alt + f4?
to sure, question best practice. however, if want this, need prevent window containing usercontrol
closing.
the easiest way set dependencyproperty
on usercontrol
boolean
flags whether container can closed. set true when want close (you have button or using close control).
public boolean allowclose { { return (boolean)getvalue(allowcloseproperty); } set { setvalue(allowcloseproperty, value); } } public static readonly dependencyproperty allowcloseproperty = dependencyproperty.register("allowclose", typeof(boolean), typeof(myusercontrol), new uipropertymetadata(false));
then, in windows closing
event, check property set true
. if not, set e.cancel = true
;
using example:
protected override void onclosing(system.componentmodel.canceleventargs e) { if (! myusercontrol.allowclose) { messagebox.show("even though windows allow alt-f4 close, i'm not letting you!"); e.cancel = true; } else { //content = null; // remove child parent - reuse this.removelogicalchild(content); //this works faster base.onclosing(e); { gc.collect(); }; } }
Comments
Post a Comment