Adding/Removing parent's keypressed handler on MDI child VisibleChange method
-
Hi, I want to disable a keypress handler of a mdi container form. I'm adding this on the container's Form_load: this.KeyPress += new KeyPressEventHandler(Form1_KeyPressed); and this on the child's VisibleChange event: if (this.Visible) { principal.KeyPress -= new KeyPressEventHandler(principal.Form1_KeyPressed); } else { principal.KeyPress += new KeyPressEventHandler(principal.Form1_KeyPressed); } where principal is a property assigned on the child's load, in order to call the objects on the parent form: principal = (FormMain)this.MdiParent; When i run this the first time works. The handler works until I turn visible the child form for the first time (which tells me the -= is working), removing the parent's handler. After i visible=false the child, the handler gets added back, but now it calls 2 times the event attached instead of 1, and so on with every on/off of the child (3, 4, 5)... why? :doh: thanks a lot for your time. daniel
-
Hi, I want to disable a keypress handler of a mdi container form. I'm adding this on the container's Form_load: this.KeyPress += new KeyPressEventHandler(Form1_KeyPressed); and this on the child's VisibleChange event: if (this.Visible) { principal.KeyPress -= new KeyPressEventHandler(principal.Form1_KeyPressed); } else { principal.KeyPress += new KeyPressEventHandler(principal.Form1_KeyPressed); } where principal is a property assigned on the child's load, in order to call the objects on the parent form: principal = (FormMain)this.MdiParent; When i run this the first time works. The handler works until I turn visible the child form for the first time (which tells me the -= is working), removing the parent's handler. After i visible=false the child, the handler gets added back, but now it calls 2 times the event attached instead of 1, and so on with every on/off of the child (3, 4, 5)... why? :doh: thanks a lot for your time. daniel
Ok: the problem is with the
if (this.Visible)
, because this will beForm1
if the child is - well - a child ofForm1
and you are handling theVisibleChanged
event there. Just replace this withchild.Visible
or(Control)sender
(I am assuming you didn't change the event-param-names for the second one. May I ask: why doing it this way? Just putif (child.Visible == true) return;
to the From1_KeyPressed - handler. -
Ok: the problem is with the
if (this.Visible)
, because this will beForm1
if the child is - well - a child ofForm1
and you are handling theVisibleChanged
event there. Just replace this withchild.Visible
or(Control)sender
(I am assuming you didn't change the event-param-names for the second one. May I ask: why doing it this way? Just putif (child.Visible == true) return;
to the From1_KeyPressed - handler.wow, faster than i could edit it. I accidentaly posted it before finishing it out. No, i'm not handling the visiblechange on form1 (parent), just on the child. this.visible actually refers to the child. i had to do it this way because the child form has a "searcher" textbox to a "SELECT blabla like" query on the textchange to a database.
-
Hi, I want to disable a keypress handler of a mdi container form. I'm adding this on the container's Form_load: this.KeyPress += new KeyPressEventHandler(Form1_KeyPressed); and this on the child's VisibleChange event: if (this.Visible) { principal.KeyPress -= new KeyPressEventHandler(principal.Form1_KeyPressed); } else { principal.KeyPress += new KeyPressEventHandler(principal.Form1_KeyPressed); } where principal is a property assigned on the child's load, in order to call the objects on the parent form: principal = (FormMain)this.MdiParent; When i run this the first time works. The handler works until I turn visible the child form for the first time (which tells me the -= is working), removing the parent's handler. After i visible=false the child, the handler gets added back, but now it calls 2 times the event attached instead of 1, and so on with every on/off of the child (3, 4, 5)... why? :doh: thanks a lot for your time. daniel
Hi. i'm not sure if you have to do it that way, but if i have to unsubscribe eventhandlers i use to keep them as member variables in my class:
public MyForm:Form
{
// some code here;
private KeyPressEventHandler myHandler;
public MyForm()
{
//...
InitializeComponent();
myHandler = new KeyPressEventHandler(Form1_KeyPressed);
subscribe();
//...
}
public void subscribe()
{
this.KeyPress += myHandler;
}
public void unsubscribe()
{
this.KeyPress -= myHandler;
}
private void Form1_KeyPressed(object sender, KeyPressEventArgs e)
{
// some stuff here..
}
}and from your MDI Child form you do simply call now the
subscribe()
andunsubscribe()
methods. hope this helps m@u -
Hi. i'm not sure if you have to do it that way, but if i have to unsubscribe eventhandlers i use to keep them as member variables in my class:
public MyForm:Form
{
// some code here;
private KeyPressEventHandler myHandler;
public MyForm()
{
//...
InitializeComponent();
myHandler = new KeyPressEventHandler(Form1_KeyPressed);
subscribe();
//...
}
public void subscribe()
{
this.KeyPress += myHandler;
}
public void unsubscribe()
{
this.KeyPress -= myHandler;
}
private void Form1_KeyPressed(object sender, KeyPressEventArgs e)
{
// some stuff here..
}
}and from your MDI Child form you do simply call now the
subscribe()
andunsubscribe()
methods. hope this helps m@u