Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm General / April 2005

Tip: Looking for answers? Try searching our database.

problems with restoring minimized MDI child form

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Nadav Popplewell - 20 Apr 2005 09:11 GMT
Hi Everybody,
I've got a strange problem:

While searching the newsgroups for something else,
I've come across a post that discusses a bug with restoring MDI child forms.
(for any body who is interested, the post is in
microsoft.public.dotnet.framework.windowsforms
by  Scott Abel, the title is 'Form Message Question', from 2003-08-12
09:43:42 PST )

Briefly, the problem is that if you use Form.WindowStyle to restore a
minimized MDI child that was maximized when it was minimized, the form will
be restored as maximized (as it should) but then when you click on the
restore button of the from it will be restored to the minimized size.
That post also included a workaround for this bug (to use SendMessage to
send a SC_RESTORE message to the form instead of using Form.WindowState).

I have implemented this in my application.
However, I've run into a strange problem:
I have several child forms in my application.
Some are opened using a menu, and some have buttons in a toolbar to open them.
For some reason, this work around works when the code is called from a menu,
but does NOT work when the code is called from the event handler of a button
in the toolbar.
I even created a menu item that calls the event handler of the toolbar button.
When clicking on the menu item the form is restored, when clicking on the
button it is NOT restored.

Later on, I will try to reproduce this in a small application and will post
the code.

If anybody have any ideas why this happens or how to fix it, please let me
know.

Thanks
Nadav
Nadav Popplewell - 20 Apr 2005 09:28 GMT
OK, I've reproduced the problem in a small application (the application is
appended to the end of the post) and then took a closer look at the original
application.

The problem is NOT with a toolbar button, as I originally thought, it's with
a button that is a child of the main form (the MDI container).
this button can open the form, change it WindowState (if you uncomment that
statement) , but it seems like the SendMessage() function does not work
properly if called from the click event handler of that Button.
the SendMessage() does work if it is called from the event handler of the
menu or from the click event handle of a toolbar button (NOT included in the
appened example).

this is very strange.

Nadav

=============== appended code =============

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Runtime.InteropServices;

namespace test_MDI_bug
{
    /// <summary>
    /// Summary description for Form1.
    /// </summary>
    public class Form1 : System.Windows.Forms.Form
    {
        private System.Windows.Forms.MainMenu mainMenu1;
        private System.Windows.Forms.MenuItem menuItem1;
        private System.Windows.Forms.MenuItem menuItem2;
        private System.Windows.Forms.Button button1;
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.Container components = null;

        public Form1()
        {
            //
            // Required for Windows Form Designer support
            //
            InitializeComponent();

            //
            // TODO: Add any constructor code after InitializeComponent call
            //
        }

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        protected override void Dispose( bool disposing )
        {
            if( disposing )
            {
                if (components != null)
                {
                    components.Dispose();
                }
            }
            base.Dispose( disposing );
        }

        #region Windows Form Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.mainMenu1 = new System.Windows.Forms.MainMenu();
            this.menuItem1 = new System.Windows.Forms.MenuItem();
            this.menuItem2 = new System.Windows.Forms.MenuItem();
            this.button1 = new System.Windows.Forms.Button();
            this.SuspendLayout();
            //
            // mainMenu1
            //
            this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
                                                                                     this.menuItem1});
            //
            // menuItem1
            //
            this.menuItem1.Index = 0;
            this.menuItem1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
                                                                                     this.menuItem2});
            this.menuItem1.Text = "test";
            //
            // menuItem2
            //
            this.menuItem2.Index = 0;
            this.menuItem2.Text = "test1";
            this.menuItem2.Click += new System.EventHandler(this.menuItem2_Click);
            //
            // button1
            //
            this.button1.Location = new System.Drawing.Point(48, 8);
            this.button1.Name = "button1";
            this.button1.TabIndex = 2;
            this.button1.Text = "button1";
            this.button1.Click += new System.EventHandler(this.button1_Click);
            //
            // Form1
            //
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(292, 272);
            this.Controls.Add(this.button1);
            this.IsMdiContainer = true;
            this.Menu = this.mainMenu1;
            this.Name = "Form1";
            this.Text = "Form1";
            this.ResumeLayout(false);

        }
        #endregion

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.Run(new Form1());
        }

        //
        // In MDI Parent form class declaration
        //
        [DllImport("User32")]
        private static extern bool SendMessage(IntPtr hWnd, int msg, int
            wParam, int lParam);

        private const int SC_RESTORE = 0xF120;
        private const int WM_SYSCOMMAND = 0x0112;

        Form f=null;
        //
        //  This is the menu click event to display the form.
        //
        //
        private void Restore() {
            //
            //     If it is already loaded, set it, otherwise set it to null
            //  so we can load it next.
            //

            if (f == null) {
                f = new Form();
                f.MdiParent = this;
                f.Show();
            }

            else {
                if (f.WindowState == FormWindowState.Minimized) {
                    SendMessage(f.Handle,WM_SYSCOMMAND, SC_RESTORE, 0);
                    //f.WindowState=FormWindowState.Normal;
                }
                f.Activate();
            }

        }

        private void menuItem2_Click(object sender, System.EventArgs e) {
            Restore();       
        }

        private void toolBar1_ButtonClick(object sender,
System.Windows.Forms.ToolBarButtonClickEventArgs e) {
            Restore();
        }

        private void button1_Click(object sender, System.EventArgs e) {
            Restore();
        }
    }
}

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.