Minimize the Form at Startup in Windows Mobile RRS feed

  • Question

  • I have an application in which depending on some condition I need to hide my form.
    I am using the following code to minimize the form

    public const int SW_MINIMIZED = 6;

    [DllImport( "coredll.dll" )]
    public static extern bool ShowWindow( IntPtr hWnd, int nCmdShow );

    void MinimizeForm()
        ShowWindow( Handle, SW_MINIMIZED );

    I have changed the code a little bit as per my requirements
    instead of Application.Run( new Form1() ); in the main() function of Program.cs
    I have

    Form1 form = new Form1();
    Application.Run( form );

    The definition of StartProcessing() is given below:

    public void StartProcessing()

    private void FocusTest()
        if ( m_btn != null )
          m_panel.Controls.Remove( m_btn );

        m_btn = new Button();
        m_btn.Bounds = m_panel.ClientRectangle;
        m_btn.TabIndex = 0;
        m_panel.Controls.Add( m_btn );

    Scenario 1: When the application starts executing,

    After m_btn.Focus(); statement is executed, the Activate event of the form is triggered and the form is displayed again inspite of the fact that it was minimized earlier using the MinimizeForm() method.

    This scenario occurs when the StartProcessing() method is called after the Form is already displayed.
    When FocusTest() is called after MinimizeForm() method, the Activate method of the form is called and after that the Deactivate
    method of the form is called and the form remains minimized.

    Can someone please tell me the reason for the difference in behaviour in both the scenarios?

    Tuesday, July 21, 2009 11:13 AM


  • Scenario 1 : Where is your button ? I assume its on Form , so it will trigger activate event.

    Scenario 2 : Your focusing on button so it activate form, after that you are calling MinimizeForm() so it get minimized.

    Only  m_btn.Focus() activate form in scenario 1 , where in scenario2  m_btn.Focus() executes then form get minimized.

    Hope you'll understand what I wanna say. :)

    Hope this may help you.

    Thanks Mohsin S.
    Tuesday, July 21, 2009 11:28 AM