简体   繁体   中英

Inheriting Windows Form Classes with Visual Studio 2010

I'm using Visual Studio 2010 to create an application. I have a class derived from the System.Windows.Forms.Form control. This class, called CViewport, is a standard for all forms in my application( ie. I intend to derive other forms from this class).

After deriving a class from CViewport, I go to the designer to edit the 2nd generation form and in the designer the form is not displayed correctly. It appears transparent if I'm not mistaken...the previous window shines through the client area of the form as if the backbuffer has not been drawn to by the designer...

...Also, after changing the size of the 2nd gen form, the designer resets the size to arbitrary dimensions...

Why might this be happening? Here is Form derived class:

namespace MyApp
{
    public partial class CViewport : Form
    {
        public CViewport()
        {
            InitializeComponent();

        }
    }
}

here is CViewport.Designer.cs:

namespace MyApp
{
    partial class CViewport
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (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()
        {
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(GDEViewport));
            this.SuspendLayout();
            // 
            // CViewport
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(800, 600);
            this.ControlBox = false;
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Sizable;
            this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            this.Name = "GDEViewport";
            this.ShowIcon = false;
            this.ShowInTaskbar = false;
            this.SizeGripStyle = System.Windows.Forms.SizeGripStyle.Hide;
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
            this.Activated += new System.EventHandler(this.ViewportForm_Activated);
            this.Deactivate += new System.EventHandler(this.ViewportForm_Deactivate);
            this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.OnFormClosing);
            this.ResizeBegin += new System.EventHandler(this.ViewportForm_ResizeBegin);
            this.ResizeEnd += new System.EventHandler(this.ViewportForm_ResizeEnd);
            this.Paint += new System.Windows.Forms.PaintEventHandler(this.ViewportForm_Paint);
            this.Resize += new System.EventHandler(this.MainViewport_UserResized);
            this.ResumeLayout(false);

        }

        #endregion
    }
}

here is 2nd gen form derived from CViewport

namespace MyApp
{
    public partial class CMainViewport: CViewport
    {
        public CMainViewport()
        {
            InitializeComponent();
        }
    }
}

here is CMainViewport.Designer.cs:

namespace MyApp
{
    partial class CMainViewport
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (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.SuspendLayout();
            // 
            // CMainViewport
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(1218, 573);
            this.ControlBox = true;
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Sizable;
            this.MaximizeBox = true;
            this.MinimizeBox = true;
            this.MinimumSize = new System.Drawing.Size(800, 600);
            this.Name = "TestForm";
            this.RightToLeft = System.Windows.Forms.RightToLeft.No;
            this.ShowIcon = true;
            this.ShowInTaskbar = true;
            this.SizeGripStyle = System.Windows.Forms.SizeGripStyle.Auto;
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.Text = "TestForm";
            this.PauseRendering += new System.EventHandler<System.EventArgs>(this.ViewportForm_PauseRendering);
            this.ResumeRendering += new System.EventHandler<System.EventArgs>(this.ViewportForm_ResumeRendering);
            this.SystemResume += new System.EventHandler<System.EventArgs>(this.MainViewport_SystemResume);
            this.SystemSuspend += new System.EventHandler<System.EventArgs>(this.MainViewport_SystemSuspend);
            this.UserResized += new System.EventHandler<System.EventArgs>(this.MainViewport_UserResized);
            this.Activated += new System.EventHandler(this.ViewportForm_Activated);
            this.Deactivate += new System.EventHandler(this.ViewportForm_Deactivate);
            this.ResizeBegin += new System.EventHandler(this.ViewportForm_ResizeBegin);
            this.ResizeEnd += new System.EventHandler(this.ViewportForm_ResizeEnd);
            this.Paint += new System.Windows.Forms.PaintEventHandler(this.ViewportForm_Paint);
            this.Resize += new System.EventHandler(this.ViewportForm_Resize);
            this.ResumeLayout(false);

        }

        #endregion
    }
}
  this.ResizeBegin += new System.EventHandler(this.ViewportForm_ResizeBegin);
  this.ResizeEnd += new System.EventHandler(this.ViewportForm_ResizeEnd);
  this.Paint += new System.Windows.Forms.PaintEventHandler(this.ViewportForm_Paint);
  this.Resize += new System.EventHandler(this.MainViewport_UserResized);

The designer runs the code in the base class to provide the WYSIWYG view. That's normally the Form class, it doesn't have any bugs. Now it is your CViewPort class. It has bugs. In one those event handlers that we cannot see.

Like the Paint event handler, strong lead to "is not displayed correctly". You have a handler for Resize, strong lead to "resets the size to arbitrary dimensions". Having these events also run at design time is what is getting you into trouble. And you are doing it wrong, a base class for a Form should override OnPaint() and OnResize() so the order in which code runs is predictable and controllable.

Having code run at design-time requires deeper insight in the way Winforms works, getting guidance from a book is rather important. You are not there yet, you didn't realize that posting the code for these event handlers was even relevant. There's a keep-out-of-trouble approach that gives you time to read the book, add this line of code to every event handler you added to your CViewPort class:

  if (this.DesignMode) return;

The answer posted by Hans Passant was very helpful...the problem though, was that I was setting the DockStyle property of the base class to 'Fill' in the base form's constructor. Removing this line of code fixed the problem...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM