SmartPhone KeyPress events not firing after showing a form

G

Guest

My application handles KeyPress events just fine. Until I show another form
(Form.Show or Form.ShowDialog, it doesn't matter which).

After the other form is closed and my main form is once again displayed, the
KeyPress event does not fire.

How can I fix this?
 
G

Guest

Thanks for the reply, but I should have noted that I'm capturing keypress
events on the main form, not a control on the form.

I've tried calling this.Focus() in "protected override OnActivated", and
I've also tried it in the Activated event handler, but I still don't get the
KeyPress events after another windows has been shown.
 
D

Dinesh Bajaj

Try doing this:

Store the instance of the main form in a global variable, and then
write these lines where you close the form:

g_MainForm.Show
Me.Close
g_MainForm.Focus
 
G

Guest

Thanks again for the reply, but the result is the same. No keypress events
are fired after the other form is closed. I've verified in the debugger that
the various Show and Focus methods are being called, but they do not solve
the problem.
 
G

Guest

ok, more information. I started with a fresh project and empty forms, and
the keypress event worked fine. So there is something speicific about my
project that is breaking the keypress eventing mechanism.

I brought my second form into the fresh project and the keypress event
handling broke. By process of elimination, I determined that the problem was
that a control in the second form also handled the KeyPress event. I was
able to fix it by removing the event handler when I close the form:

this.TextBox1.KeyPressed -= ...

However, when I made this same change in my main project, it remained
broken. So there must be something else about the main form that's breaking
the KeyPress handling as well. To be continued...
 
D

Dinesh Bajaj

So far it is also working all well in the project I created at my end.

Waiting for more details from you.
 
G

Guest

I've narrowed the second problem down to a custom control. If I add a custom
control to the main form, the KeyPress event breaks as I described earlier.

Below is some code to reproduce similar behavior with a single form and
custom control. If you set a breakpoint at "base.OnKeyPress(e)", it will
never be hit. If you comment out "this.Controls.Add()", it will be hit as
expected.

namespace Test
{
using System;
using System.Windows.Forms;

public class Control1 : System.Windows.Forms.Control
{
public Control1()
{
}
}

public class Form1 : System.Windows.Forms.Form
{
public Form1()
{
this.Controls.Add(new Control1()); // breaks KeyPress event
handling
}

static void Main()
{
Application.Run(new Form1());
}

protected override void OnKeyPress(KeyPressEventArgs e)
{
base.OnKeyPress(e); // this line is never executed
}
}
}
 
G

Guest

I found that, if I hide all my custom controls in the main form Activated
event (.Visible = false), call this.Focus, and then show all my controls
(.Visible = true), I can workaround the problem.

Is there any way for me to write the custom control so it won't steal the
focus and keypress events from the main form?

Or a way to implement KeyPreview on the main form so it will always get
first crack at the keypresses before child controls?

Thanks!
 
J

Joseph Geretz

I'm having the same problem but having nothing to do with a custom control.
I'm finding that by adding a check box onto the form, I'm no longer trapping
KeyPress. Simply commenting / uncommenting out the following line either
restores or suppresses the KeyPress event.

this.Controls.Add(this.chkPowerOff);

Pretty lame. I'll try your workaround.

- Joe Geretz -
 
G

Guest

Implement an IMessageFilter. You can intercept all keypresses (and really
any message you'd like) that way.

-Chris
 
D

Dinesh Bajaj

Use this.Focus in the control's GotFocus event-handler to shift the
focus away from control to the form.

You can alternatively set the control's Enabled property to false, if
you really not want focus on it.
 
G

Guest

I already tried this.Focus in GotFocus, but the focus comes right back to the
control in an infinite loop.

I use the Enabled property of the control for other purposes. I cannot
leave it disabled.

What I really need is a way to implement some kind of KeyPreview so the form
gets keypress events whether or not some other control has the focus.

Is this a possibility?
 
G

Guest

That would be great, if only the Compact Framework supported IMessageFilter.

I need a solution that will work on the SmartPhone 2003 platform.
 
G

Guest

I had exactly the same problem!

Today I found the best solution...!?

Created a TextBox control with width = 0 so the TextBox is not visibible to
the user (even the Visible property is set to true) --> TextBox can receive
the focus! So I doing everything in TextBox_KeyUp handler. Works perfectly in
every situation needed!!

The only problem:
Works with the emulator (WM2003 SE) but not on the device :-( ...!

Why does the emulator such a different behaviour...!! F*** thing!

I'll try something similar so that it also works on the device!

....and why can't set the focus to the main form!? After startup the main
form has the focus, but loses it in "some situations"! Using this.Focus()
does not work (checking the this.Focused property after calling
this.Focus()...)

Kind Regards, hfr
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top