【问题标题】:Margin is not being applied未应用保证金
【发布时间】:2020-05-14 17:03:14
【问题描述】:

我有一个需要在运行时手动调整大小的用户控件,但我希望应用边距。我从下面的链接 1 中发现,要使边距正常工作,需要将 AutoSize 属性设置为 true。虽然,由于我需要在运行时手动调整控件的大小,但 AutoSize 属性在我的理解中需要是错误的。

用户控件 (Accordion) 包含一个水平拆分面板,其中第一个面板包含一个按钮,第二个面板包含一个 Panel 控件。在下图中,您可以看到多个“手风琴”,在图片中,它们是按钮之一。 Accordions 停靠在集装箱的顶部。目的是当我单击按钮时,它会调整大小并显示“内容面板”和内容面板的控件。我的问题是设置用户控件的边距属性不会导致它们自动与其他控件或它们所在的容器保持距离。

链接1:WinForm Bottom Margin Property Doesn't Do Anything

希望我正在使用的一些有用的部分。

namespace CustomControls
{
partial class Accordion
{
    /// <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 Component 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.split = new System.Windows.Forms.SplitContainer();
        this.ToggleCollapse = new System.Windows.Forms.PictureBox();
        this.MainButton = new System.Windows.Forms.Button();
        this.contentsPanel = new System.Windows.Forms.Panel();
        ((System.ComponentModel.ISupportInitialize)(this.split)).BeginInit();
        this.split.Panel1.SuspendLayout();
        this.split.Panel2.SuspendLayout();
        this.split.SuspendLayout();
        ((System.ComponentModel.ISupportInitialize)(this.ToggleCollapse)).BeginInit();
        this.SuspendLayout();
        // 
        // split
        // 
        this.split.Dock = System.Windows.Forms.DockStyle.Fill;
        this.split.FixedPanel = System.Windows.Forms.FixedPanel.Panel1;
        this.split.IsSplitterFixed = true;
        this.split.Location = new System.Drawing.Point(0, 0);
        this.split.Margin = new System.Windows.Forms.Padding(3, 3, 3, 10);
        this.split.Name = "split";
        this.split.Orientation = System.Windows.Forms.Orientation.Horizontal;
        // 
        // split.Panel1
        // 
        this.split.Panel1.Controls.Add(this.ToggleCollapse);
        this.split.Panel1.Controls.Add(this.MainButton);
        this.split.Panel1MinSize = 0;
        // 
        // split.Panel2
        // 
        this.split.Panel2.Controls.Add(this.contentsPanel);
        this.split.Size = new System.Drawing.Size(663, 488);
        this.split.SplitterDistance = 40;
        this.split.TabIndex = 0;
        // 
        // ToggleCollapse
        // 
        this.ToggleCollapse.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
        this.ToggleCollapse.BackColor = System.Drawing.SystemColors.ActiveCaption;
        this.ToggleCollapse.Enabled = false;
        this.ToggleCollapse.Image = global::CustomControls.Properties.Resources.TrianglePointer;
        this.ToggleCollapse.Location = new System.Drawing.Point(621, 13);
        this.ToggleCollapse.Name = "ToggleCollapse";
        this.ToggleCollapse.Size = new System.Drawing.Size(39, 24);
        this.ToggleCollapse.SizeMode = System.Windows.Forms.PictureBoxSizeMode.CenterImage;
        this.ToggleCollapse.TabIndex = 0;
        this.ToggleCollapse.TabStop = false;
        // 
        // MainButton
        // 
        this.MainButton.AutoEllipsis = true;
        this.MainButton.BackColor = System.Drawing.SystemColors.ActiveCaption;
        this.MainButton.Dock = System.Windows.Forms.DockStyle.Fill;
        this.MainButton.FlatAppearance.BorderColor = System.Drawing.Color.FromArgb(((int)(((byte)(192)))), ((int)(((byte)(255)))), ((int)(((byte)(192)))));
        this.MainButton.FlatAppearance.BorderSize = 3;
        this.MainButton.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
        this.MainButton.Font = new System.Drawing.Font("Verdana", 18F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
        this.MainButton.ImageAlign = System.Drawing.ContentAlignment.MiddleRight;
        this.MainButton.Location = new System.Drawing.Point(0, 0);
        this.MainButton.Name = "MainButton";
        this.MainButton.Size = new System.Drawing.Size(663, 40);
        this.MainButton.TabIndex = 0;
        this.MainButton.Text = "Accordion Activator";
        this.MainButton.TextAlign = System.Drawing.ContentAlignment.BottomLeft;
        this.MainButton.UseVisualStyleBackColor = false;
        this.MainButton.MouseDown += new System.Windows.Forms.MouseEventHandler(this.MainButton_MouseDown);
        this.MainButton.MouseEnter += new System.EventHandler(this.MainButton_MouseEnter);
        this.MainButton.MouseLeave += new System.EventHandler(this.MainButton_MouseLeave);
        this.MainButton.MouseUp += new System.Windows.Forms.MouseEventHandler(this.MainButton_MouseUp);
        // 
        // contentsPanel
        // 
        this.contentsPanel.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
        this.contentsPanel.Dock = System.Windows.Forms.DockStyle.Fill;
        this.contentsPanel.Location = new System.Drawing.Point(0, 0);
        this.contentsPanel.Name = "contentsPanel";
        this.contentsPanel.Size = new System.Drawing.Size(663, 444);
        this.contentsPanel.TabIndex = 0;
        this.contentsPanel.MouseDown += new System.Windows.Forms.MouseEventHandler(this.Panel_MouseDown);
        this.contentsPanel.MouseLeave += new System.EventHandler(this.Panel_MouseLeave);
        this.contentsPanel.MouseMove += new System.Windows.Forms.MouseEventHandler(this.Panel_MouseMove);
        this.contentsPanel.MouseUp += new System.Windows.Forms.MouseEventHandler(this.Panel_MouseUp);
        // 
        // Accordion
        // 
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Inherit;
        this.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
        this.Controls.Add(this.split);
        this.DoubleBuffered = true;
        this.Margin = new System.Windows.Forms.Padding(3, 3, 3, 10);
        this.Name = "Accordion";
        this.Size = new System.Drawing.Size(663, 488);
        this.split.Panel1.ResumeLayout(false);
        this.split.Panel2.ResumeLayout(false);
        ((System.ComponentModel.ISupportInitialize)(this.split)).EndInit();
        this.split.ResumeLayout(false);
        ((System.ComponentModel.ISupportInitialize)(this.ToggleCollapse)).EndInit();
        this.ResumeLayout(false);

    }

    #endregion

    private System.Windows.Forms.SplitContainer split;
    private System.Windows.Forms.Button MainButton;
    private System.Windows.Forms.PictureBox ToggleCollapse;
    protected System.Windows.Forms.Panel contentsPanel;
}
}

这是我手动调整大小的时间和方式

       public bool Collapsed
       {
        get
        {
            return split.Panel2Collapsed;
        }
        set
        {
            Freeze(true);
            if (Dock == DockStyle.Fill && this.DesignMode)
            {
                if (firstResize)
                {
                    if (value)
                        this.Parent.Height = MainButtonHeight;
                    else
                        this.Parent.Height = ExpandedHeight;
                }
                else
                {
                    if (value)
                        CollapseAnimation(1);
                    else
                        ExpandAnimation(5);
                }
            }
            else
            {
                if (firstResize)
                {
                    if (value)
                        this.Height = MainButtonHeight;
                    else
                        this.Height = ExpandedHeight;
                    if (loadBegan)
                        firstResize = !firstResize;
                }
                else
                {
                    if (value)
                        CollapseAnimation(1);
                    else
                        ExpandAnimation(5);
                }
            }
            split.Panel2Collapsed = value;
            ToggleCollapse.Image = toggleImageNeutral;
            Freeze(false);
        }
    }

【问题讨论】:

    标签: c# winforms user-controls


    【解决方案1】:

    不确定我是否完全理解您的问题,但也许这会有所帮助。如果您希望在调整大小后保持固定的边距(由于 SplitContainer 面板扩展/折叠),您可以试试这个:

    • 在您的空白用户控件上拖动一个 Panel 控件并将其 Margin 设置为 (0,0,0,0) 并将 Padding 设置为 (x,x,x,x),其中 x 是您想要的边距宽度。

    在计算子控件的高度以调整控件大小时要小心,如果您在某些子控件上设置了内边距和边距,请务必考虑它们。

    更新:

    如此处所述:https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.control.margin?view=netframework-4.8

    “在停靠控件上设置 Margin 属性不会影响控件与其容器边缘的距离。”

    所以,我认为您必须选择一个特定的容器,例如 TableLayoutPanel 或 FlowLayoutPanel。尝试使用 FlowLayoutPanel 并将其设置为:

    • AutoSize = true;
    • BorderStyle = FixedSingle(只是为了查看边距是否保持正确)
    • FlowDirection = 自上而下

    然后在其中拖动一些控件,其中 AutoSize 属性设置为 false 并根据需要设置 Margin。你应该得到这样的东西:

    注意:您的控件的高度可能仍会更改(设置最小控件高度可能会有所帮助),但是一旦您设置了它们的宽度,FlowLayoutPanel 控件将无法剪切控件,因为边距。

    【讨论】:

    • 有趣的概念,我认为它肯定可以解决。我唯一的问题是它使用填充来创建边距(这似乎是对填充的不当使用)并且面板会阻挡容器的背景。如果它有助于您理解我的问题,我已经更新了这个问题。谢谢!
    • @Rigatoni 感谢您的澄清。我更新了答案。我希望这会有所帮助。
    猜你喜欢
    • 1970-01-01
    • 2020-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-10
    • 2016-05-14
    相关资源
    最近更新 更多