Incorrect panel behavior in an inherited form of Windows?

I need a workaround for incorrect panel behavior in an inherited Windows Form warehouse. I have several buttons that should be attached to the lower right corner of the panel, but their locations change the way I can not predict inherited forms. I tried various combinations of the Dock and Anchor properties, but so far nothing has made the buttons appear where they should. All controls are marked Protected.

The behavior is correct in the most basic form. Misconduct occurs only in inherited forms; buttons are not tied to their parent panel.

If I bind the buttons in the upper left corner, I can resize the inherited form more, and in the end the buttons will be expanded, somewhere outside the size of the base form. If I snap the buttons to the lower right, I could not make the shape large enough to open the buttons.

I tried to create a custom panel that inherits from Panel and GroupBox, but they do not work differently. I had the same luck putting the buttons directly on the form, without a container.

What I need: a panel at the bottom of the resizable form with my buttons in the lower right corner. The rest of the form should be a resize for the DataGridView (I already worked with inheritance and docking problems with this>: - /) Buttons should always be in the lower right corner regardless of the size of the window. I do not want to resize the buttons.

If I need to resize the controls myself, I am ready to do it. But if the structure does it right, and I just need to learn how to use it correctly, I would prefer education.

I found this link, which seems to describe similar incorrect behavior: How to stop the movement of buttons in an inherited form . I did not answer him.


Here is the code extracted from the program I came across.

Basic form

// 
// refreshButton
// 
this.refreshButton.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.refreshButton.Location = new System.Drawing.Point(200, 4);
this.refreshButton.Name = "refreshButton";
this.refreshButton.Size = new System.Drawing.Size(75, 23);
this.refreshButton.TabIndex = 5;
this.refreshButton.Text = "&Refresh";
this.refreshButton.UseVisualStyleBackColor = true;
// 
// saveButton
// 
this.saveButton.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.saveButton.Location = new System.Drawing.Point(281, 5);
this.saveButton.Name = "saveButton";
this.saveButton.Size = new System.Drawing.Size(75, 23);
this.saveButton.TabIndex = 4;
this.saveButton.Text = "&Save";
this.saveButton.UseVisualStyleBackColor = true;
// 
// buttonPanel
// 
this.buttonPanel.Controls.Add(this.saveButton);
this.buttonPanel.Controls.Add(this.refreshButton);
this.buttonPanel.Dock = System.Windows.Forms.DockStyle.Bottom;
this.buttonPanel.Location = new System.Drawing.Point(0, 231);
this.buttonPanel.Name = "buttonPanel";
this.buttonPanel.Size = new System.Drawing.Size(360, 31);
this.buttonPanel.TabIndex = 6;

Inherited form (resize for my content)

// 
// refreshButton
// 
this.refreshButton.Location = new System.Drawing.Point(286, 7);
this.refreshButton.TabIndex = 0;
// 
// saveButton
// 
this.saveButton.Location = new System.Drawing.Point(367, 7);
this.saveButton.TabIndex = 1;
// 
// buttonPanel
// 
this.buttonPanel.Location = new System.Drawing.Point(0, 232);
this.buttonPanel.Size = new System.Drawing.Size(445, 33);

: . ​​ Visual Studio 2005 #. , ( !) . - VS.

, . , . . *.Designer.cs - , . Location refreshButton saveButton 371,7 452,7, 445 .

, , , .


: , , . , , - . , VS2005 100%.

, , , OnResize() . , .

/// <summary>
/// Must handle some layout operations manually because Visual Studio 
/// 2005 arbitrarily changes some properties of inherited controls.
/// </summary>
/// <param name="e">Data for event.</param>
protected override void OnResize(EventArgs e)
{
    base.OnResize(e);

    // Move Refresh and Save buttons to lower right corner of button panel.
    saveButton.Top = buttonPanel.Bounds.Height -
        (saveButton.Height + saveButton.Padding.Bottom + saveButton.Margin.Bottom);
    saveButton.Left = buttonPanel.Bounds.Width -
        (saveButton.Width + saveButton.Padding.Right + saveButton.Margin.Right);
    refreshButton.Top = saveButton.Top;
    refreshButton.Left = saveButton.Left -
        (refreshButton.Width + refreshButton.Padding.Right + refreshButton.Margin.Right);
}
+5
4

, . :

using System;
using System.Drawing;
using System.Windows.Forms;

class FormBase : Form
{
    public FormBase()
    {
        Panel panel;
        Controls.Add(panel = new Panel { Dock = DockStyle.Bottom, Height = 120, BackColor = Color.LightGray });
        panel.Controls.Add(new Button { Text = "Button 1", Anchor = AnchorStyles.Bottom | AnchorStyles.Right, Location = new Point(panel.ClientSize.Width - 80, panel.ClientSize.Height - 60) });
        panel.Controls.Add(new Button { Text = "Button 2", Anchor = AnchorStyles.Bottom | AnchorStyles.Right, Location = new Point(panel.ClientSize.Width - 80, panel.ClientSize.Height - 30) });
    }
}

class FormInherited : FormBase
{
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new FormInherited());
    }
}
+2

.

, , - ​​ , ( ), . ( , ?!)

+1

.
, Anchor .
BUILD → REBUILD SOLUTION.

Visual Studio.

EDIT: Resize, .

0

VS 2012, , , , ... , , , 1px 1px, , 2 1px, 2 , 3... , , ...


Kinda:

A minute ago, I checked something, I installed the modifier of the entire nested container, to some accessible thing, then the child form generated more code, the execution time was correct, but now after each compilation the designer moved my controls inside the form, so in In this regard, I also add another panel to the child form and set it to dock mode, and then bind my control to it, then it is fixed, previously even the binding was wrong ...

0
source

All Articles