Windows Develop Bookmark and Share   
 index > Windows Forms Designer > DesignerHost: Can you set a control to be non-resizable?
 

DesignerHost: Can you set a control to be non-resizable?

I need to add some controls that shouldn't resize, but can be moved around. Is it possible to override the resize event on specific control? The layout designer has pre-defined controls in it. It's just basically a way to re-arrange existing controls.
JRQ  Thursday, September 10, 2009 8:37 PM
Not sure what you're asking here.

Are you trying to create a control which cannot be resized or are you trying to prevent normally resizable controls from being resized within your own control?

A simplecontrol with no resize handles in the designer: (needs a reference to System.Design.dll)
    [Designer(typeof(MyDesigner))]
    public partial class UserControl1 : UserControl
    {
        public UserControl1()
        {
            InitializeComponent();
        }

    }

    public class MyDesigner : ControlDesigner
    {
        public override SelectionRules SelectionRules
        {
            get
            {
                SelectionRules rules = base.SelectionRules;
                rules &= ~SelectionRules.AllSizeable;
                return rules;
            }
        }
    }
note that there are several properties and methods which can change the size and you will need to find and code for all of them:

just a few of them are:Size, Width, Height, Bounds, SetBounds()
Mick Doherty
http://dotnetrix.co.uk
  • Marked As Answer byJRQ Friday, September 25, 2009 8:39 PM
  • Marked As Answer byAland LiMSFT, ModeratorTuesday, September 15, 2009 3:47 AM
  • Unmarked As Answer byJRQ Wednesday, September 16, 2009 10:01 PM
  •  
Mick Doherty  Sunday, September 13, 2009 11:56 AM
I haven't tried it but I think that if hide the Size property with a new Size property that does not provide a Set accessor, then the resize handles will disappear. That could be one way to go.

Another thought: I believe there is a DesignTimeOnlyAttribute attribute that marks a property as a design-time only property. Could it be that there is a RunTimeOnlyAttribute that you can apply to Size? I am too lazy to check right now. :)

Another thought (even more vague than the previous one): I suppose that you can create your own Designer that does not provide the resize handles.
MCP
webJose  Thursday, September 10, 2009 9:41 PM
Not sure what you're asking here.

Are you trying to create a control which cannot be resized or are you trying to prevent normally resizable controls from being resized within your own control?

A simplecontrol with no resize handles in the designer: (needs a reference to System.Design.dll)
    [Designer(typeof(MyDesigner))]
    public partial class UserControl1 : UserControl
    {
        public UserControl1()
        {
            InitializeComponent();
        }

    }

    public class MyDesigner : ControlDesigner
    {
        public override SelectionRules SelectionRules
        {
            get
            {
                SelectionRules rules = base.SelectionRules;
                rules &= ~SelectionRules.AllSizeable;
                return rules;
            }
        }
    }
note that there are several properties and methods which can change the size and you will need to find and code for all of them:

just a few of them are:Size, Width, Height, Bounds, SetBounds()
Mick Doherty
http://dotnetrix.co.uk
  • Marked As Answer byJRQ Friday, September 25, 2009 8:39 PM
  • Marked As Answer byAland LiMSFT, ModeratorTuesday, September 15, 2009 3:47 AM
  • Unmarked As Answer byJRQ Wednesday, September 16, 2009 10:01 PM
  •  
Mick Doherty  Sunday, September 13, 2009 11:56 AM

I'm trying to prevent a control from being resized when using the DesignSurface (IDesignerHost implementation). I'm implementing a custom form designer, but the user can only movesome pre-defined controls, but preventthem from being resized.

JRQ  Wednesday, September 16, 2009 10:01 PM

One option would be to Inherit the control which you do not wish to resize and get it to use the ControlDesigner whichI outlined above. You would also need to override the OnResize() method to make sure that the control isn't resized via some other method. You would then just need to EnableDesignMode on those controls.

Another way (which I don't really like) is to do something like:

    public class MyControlDesigner : ParentControlDesigner
    {
        private Hashtable childSizes = new Hashtable();

        public override void Initialize(IComponent component)
        {
            base.Initialize(component);

            foreach (Control child in this.Control.Controls)
            {
                this.EnableDesignMode(child, child.Name);

                if (!child.Size.IsEmpty)
                {
                    if (!childSizes.ContainsKey(child))
                        childSizes.Remove(child);
                }
                childSizes.Add(child, child.Size);

                child.Resize += new EventHandler(child_Resize);
           }
        }

        void child_Resize(object sender, EventArgs e)
        {
            if (childSizes.ContainsKey(sender))
            {
                Size sz = (Size)childSizes[sender];
                ((Control)sender).Size = sz;
            }
        }

    }


Mick Doherty
http://dotnetrix.co.uk
Mick Doherty  Sunday, September 20, 2009 9:26 AM

You can use google to search for other answers

Custom Search

More Threads

• VS 2005 (Team Ed. for SD) C# disappearing of eventhandler delegates
• How do I fix my form designer so that my form can be seen again ?
• How to make the buttons round in the VS 2008
• Designsurface Copy and Paste
• Custom Controls with Visual C++ .NET
• Custom TabControl Designer
• Longhorn style Breadcrumb Bar
• add a form to a panel
• Get click event in empty panel.
• Container Controls no longer accepting drag/dropped controls after a load