Application loses focus when closing a Form - but only on one Form it happens...

Hi all,

I have this strange problem/issue that I have had bothering me for a while now. I have an application where the users browses through Forms to perform various bits of functionality, and then closes the form to return back to the previous form (the Form from which it was openned), and the Focus is right back with the Form that openned the closed Form (if you know what I mean :)). Now this all works fine in my application apart from in one place, where when the called Form is closed, the calling form (or my Application even) doesn't get focus back until the 2nd tap on the application with the pen. The Windows start button and Input control on the MainMenu are focused and can be activated from the first click on them, but not my application. On this one Form it always requires two clicks.

Now in my calling Form I have the following code...

using (Form frm = new TestFocus())
{
if (frm.ShowDialog() == DialogResult.OK)
this.Focus();
}

and in the called Form, I have an event on a simple button that has the following code...

private void button1_Click(object sender, EventArgs e)
{
DialogResult = DialogResult.OK;
}

Now this has worked normally in every other part of my application, but not on this one Form. I have compared the Form's properties with all other Forms and they are the same. There is no events attached the Form (both of them) apart from the Load_Event.

Does anyone know why this strange incident is happening and how I can solve it - its been bugging me for some time now.

Thanks



Answer this question

Application loses focus when closing a Form - but only on one Form it happens...

  • TechMate

    But its not just the position of the SIP, is also the look and feel of the Main Menu. The one Form has this bright blue (slightly gradiented) Menu, while all the others have a duller solid grey/blue Menu.


  • Pramod S Kumar

    Are you adding/removing menu items to your main menu after the main menu has been added to the form

    With a little experimentation, I've found that if you start with a main menu with <= 2 items on the form, you get the WM5 menu style.

    If, then, you dynamically add another (third) menu item to your menu, the menu style reverts to the older, left-justified style.

    If, then, you dynamically remove that (third) menu item from your menu, the menu style does not revert back to the WM5 style.

    If, then, you remove the main menu from your form and re-add it to your form, you get the WM5 style back again.

    If you want all of your forms to have the old, pre-WM5 menu style, you need to make sure your menus have > 2 menu items on them when added to the form. You can then (OnFormLoad ) remove the "dummy" menu items from your form and still retain the old menu style.



  • cwilkins59

    there isn't really a differentiation between WM5 and WM4.2 forms. If your app is running on a WM5 device, it is WM5.

    The position of the SIP icon has to do with how many menu items the main menu of your form has.



  • Joseph Stalin

    Try the following:

    if (frm.ShowDialog(this) == ...

    You shouldn't need the "this.Focus();" line after that, since the form that launched the dialog is now the parent form. It should automatically receive focus once the child dialog is dimissed (unless some other code is routing the focus elsewhere).



  • tash12457769

    yes, that is correct. if the menu has <= 2 items and we are running on a WM5 device, it is converted to the WM5 menubar. That way you can use the two hardware keys for the left and right menu items. Once there are more than 2 items, it goes back to the old-style menu that you need to tap on the screen to access.

    I am not aware of a way to force a menu with 2 or less buttons to behave like the old-style menu.



  • ShadowOfTheBeast

    Hi all,

    I have a strange issue in that my application, which has many forms, where each form has a MainMenu control attached to the bottom of the form. All but one Form have the MainMenu Control that comes from the CF 2.0 (Keyboard Input control is aligned to the right on the MainMenu), where as the one Form has the MainMenu Control but the Keyboard Input Control is centralised on the Main Menu - does this mean that I have a MainMenu Control that is from the CF 1.0

    The Form that has this random MainMenu is causing me some Form Focusing issues when returning to this Form so need to know if this is the cause, and if so, how can I change this MainMenu Control to a CF 2.0 MainMenu Control, or should I just delete it, and drag another onto the Form from the ToolBox.

    Thanks


  • Kannan.B

    That doesn't seem to be the case with my application on both the Symbol MC70 and a K-JAM i-mate - both of which have WM5. I have almost 60% of Forms with one or two buttons on them, there are aligned to the left of the Form. They don't seem to be reverting to being placed either side of the SIP. How could this be


  • Stefander

    Hi all,

    I have had this issue hanging over me for over a month now, and still can't seem to resolve it :(. I have a Form that has a MainMenu Control on it, which it naturally has the SIP control on it too. Now, when using a Symbol MC50 (that has WM 4.*) the MainMenu on this Form looks the same as the all the other Forms in that the SIP is aligned to the right of the MainMenu. But on this one Form, when I close the Child Form that was called, the focus is on the SIP (which I believe isn't directly part of the application). So it therefore takes me two click on the screen to get the application back into focus (SIP expands on the first click).

    Now when I run this same application on the Symbol MC70 with WM 5.0, I can see that all the MainMenu controls are the same as that on the MC50 apart from the Form that I am having problems with, in that it doesn't regain focus when closing its childs Forms. This MainMenu is the one where the SIP is based in the center of the MainMenu - why is the MainMenu on this particalar Form different I have tried copying a MainMenu Control from other Forms, and then adjusting it, but that will then revert to the WM 5.0 MainMenu Control. I have even tried creating a new Form, and copying across a MainMenu from a Form that has Focus straight away when closing its Child Form, and when I run the app on the device the Form still has the WM 5.0 MainMenu. The problem is still there on the MC70 in that the Form doesn't have focus (the SIP) when I close its child Form.

    I have even tried the following code that I got via these Forums, but the SIP still shows in the MainMenu, although I set it to not display.

    SHFullScreenHelper.ShowSIPButton(this, false);

    public class SHFullScreenHelper
    {
    private const Int32 SHFS_SHOWSIPBUTTON = 0x0004;
    private const Int32 SHFS_HIDESIPBUTTON = 0x0008;
    private const Int32 SHFS_SHOWSTARTICON = 0x0010;
    private const Int32 SHFS_HIDESTARTICON = 0x0020;

    private const string formWindowClassName = "#NETCF_AGL_BASE_";

    [DllImport("aygshell.dll")]
    private static extern Int32 SHFullScreen(IntPtr hWnd, Int32 dwState);

    [DllImport("coredll.dll")]
    private static extern IntPtr FindWindow(string className, string windowName);

    [DllImport("coredll.dll")]
    private static extern Int32 GetClassName(IntPtr hWnd, StringBuilder className, int maxCount);

    public static bool ShowStartIcon(Form f, bool bShow)
    {
    Int32 dwFlag = bShow SHFS_SHOWSTARTICON : SHFS_HIDESTARTICON;
    return SHFullScreen(GetWindowHandle(f), dwFlag) != 0;
    }

    public static bool ShowSIPButton(Form f, bool bShow)
    {
    Int32 dwFlag = bShow SHFS_SHOWSIPBUTTON : SHFS_HIDESIPBUTTON;
    return SHFullScreen(GetWindowHandle(f), dwFlag) != 0;
    }

    private static IntPtr GetWindowHandle(Form f)
    { return FindWindow(formWindowClassName, f.Text); }
    }

    I would be extremely greatful if someone can help me out on this one, as I am literally stumpted on it :(

    Tryst


  • mukthi

    ok - done what you stated, and if I dynamically put 2+ menu items on the Main Menu Control for this one specific Form it does resort back to the WM4 style menu. But, it still remains that I have Main Menu controls throughout the application that still have one and two MainMenuItems yet it still remains on the WM4 style Menu.


  • Coach24

    the positioning of the SIP on WM5 depends upon how many menu items are in your main menu. If you have 2 or less, it goes in the middle of the screen. If you have >2 it will live off to the right.

  • Dinis

    But my problem is that on this one Form I am using a WM5 form where as in the rest of the application, they are (i guess) WM 4.2 Forms - which is what I want.

    To get around this should I just copy an old Form and edit, as if I create a new Form it will give me the WM 5.1 Form


  • Ferdinand Kuiper

    which version of windows mobile are you running on

    how many menu items are in your menus (the ones which "work" vs. the ones that don't)



  • zoomCrypt

    the only way I've found to do that is where at some point in the MainMenu's lifetime, while attached to a Form, it has > 2 MenuItems. As I stated previously, removing the extra MenuItems won't cause the MainMenu to revert to the pre-WM5 behavior. Perhaps you are doing something different in your application to cause the change in behavior. If you figure out what it is, please let us know.

    Regardless, it sounds like the original problem reported is resolved or at least worked around.



  • xluna

    Hi,

    putting 'this' as a parameter of ShowDialog(this) is not valid, it throws errors.

    I'm assuming its probs some other code thats running that keeps the focus from returning to the parent form, but I can't spot anything :(


  • Application loses focus when closing a Form - but only on one Form it happens...