简体   繁体   中英

Resizing a PropertyGrid control

I have written code that will resize a Control and all of its controls, but there's a problem with the PropertyGrid. The user interface is a GroupBox that contains the TabControl tabContAll. In tabContAll is a TabPage that contains a PropertyGrid.

private void ResizeUI () 
{
    ui.Location = new Point (this.ClientRectangle.Left, this.ClientRectangle.Top + menubar.Height);
    ui.Size = new Size (this.ClientRectangle.Width, this.ClientRectangle.Height - menubar.Height);
    ResizeControl (tabContAll, ui);
}

private void ResizeControl (Control control, Control parent) 
{
    control.Location = new Point (parent.ClientRectangle.Left, parent.ClientRectangle.Top);
    control.Size = new Size (parent.ClientRectangle.Width, parent.ClientRectangle.Height);

    foreach (Control child in control.Controls) {
        ResizeControl (child, control);
    }
}

This function is called when the form loads, and this is what it looks like compared to if I commented out the resizing in the loop so the PropertyGrid doesn't get resized:

在此处输入图片说明

在此处输入图片说明

Furthermore when it is resized, the description doesn't work. It just shows the name of the property.

I strongly recommend you to not write code for controls resizing unless you need a very very custom behaviour.

Set Control.Dock or Control.Anchor properties instead, and leave the rest to them.

For example, your case can be solved easily by setting the Dock property to DockStyle.Fill for both your TabControl and PropertyGrid (and obviously removing the custom resizing methods).

Here's a complete MSDN Walkthrough for WinForms custom controls design:

http://msdn.microsoft.com/en-us/library/6hws6h2t.aspx

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