How to override the Width and Height Properties of a User Control?

dotnetman

Newcomer
Joined
Jan 4, 2005
Messages
19
Hi friends,
I am having trouble overriding properties of a user control I have created.
I have a panel within the control that acts as the control's border. I would want it to resize accordingly as the control's size changes. It does it fine at run-time. But whenever, I do drag-n-drop at design-time, it does not resize automatically. What am I doing wrong?
This is what I am doing.

Code:
namespace XYZButtonPane
{
	/// <summary>
	/// Summary description for XYZButtonPane.
	/// </summary>
	public class XYZButtonPane : System.Windows.Forms.UserControl
	{
		private System.ComponentModel.Container components = null;
		private System.Windows.Forms.Panel pnlBorder;

		#region Member Variables

		private int m_intWidth = 100;
		private int m_intHeight = 200;
		#endregion

		#region Properties

		new public int Width
		{
			get
			{
				return m_intWidth;
			}

			set
			{
				m_intWidth = value;
				this.pnlBorder.Width = value;
			}
		}

		new public int Height
		{
			get
			{
				return m_intHeight;
			}
			set
			{
				m_intHeight = value;
				this.pnlBorder.Height = value;
			}
		}
		#endregion
		public XYZButtonPane()
		{
			InitializeControls();
		}

		private void InitializeControls()
		{
			this.pnlBorder = new System.Windows.Forms.Panel();
			this.SuspendLayout();
			// 
			// pnlBorder
			// 
			this.pnlBorder.Location = new System.Drawing.Point(0, 0);
			this.pnlBorder.Name = "pnlBorder";
			this.pnlBorder.BorderStyle = BorderStyle.FixedSingle;
			//
			// XYZButtonPane
			// 
			this.Name = "XYZButtonPane";
			this.Size = new Size(this.m_intWidth, this.m_intHeight);
			this.pnlBorder.Size = new Size(this.m_intWidth, this.m_intHeight);
			this.Resize += new System.EventHandler(this.XYZButtonPane_Resize);
			this.Controls.Add(this.pnlBorder);
			this.Name = "XYZButtonPane";
			this.ResumeLayout(false);
		}
		
		private void XYZButtonPane_Resize(object sender, System.EventArgs e)
		{
			this.pnlBorder.Height = this.Height;
			this.pnlBorder.Width = this.Width;
		}
   }
}
 
You are not overriding the functions, you are hiding it (in VB its called shadowing, i don't think C# uses this term). In C# the new keyword, when used with a function or property, causes the function or property by the same name and with the same signature in the base class to be hidden. The UserControl.Width and UserControl.Height properties still exist in your user control, but are hidden by your new Width and Height properties.

These shadowed, or hidden, properties can still be accessed, however, and the designer is setting the UserControl.Width property instead of the XYZButtonPane.Width property.

I don't know if this is the best way to do what you want to do, but you could just resize pnlBorder in a Resize event handler of your base class.
 
Back
Top