【问题标题】:Remove control from Form: space is still occupied从表单中删除控件:空间仍然被占用
【发布时间】:2018-05-02 19:03:46
【问题描述】:

我有一个表单,想要切换显示和隐藏RichTextBox。 如果我隐藏控件,控件应该放弃空间保留,但实际上并没有。

它已被删除,但rtbcontrol 保留的原始空间仍显示为空白的灰色空间。 看来,它只影响固定在底部的控件。

我制作了一个小样本来演示我的问题: 设计师:

namespace HideShowTest
{
    partial class Form1
    {
        /// <summary>
        /// Erforderliche Designervariable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Verwendete Ressourcen bereinigen.
        /// </summary>
        /// <param name="disposing">True, wenn verwaltete Ressourcen gelöscht werden sollen; andernfalls False.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Vom Windows Form-Designer generierter Code

        /// <summary>
        /// Erforderliche Methode für die Designerunterstützung.
        /// Der Inhalt der Methode darf nicht mit dem Code-Editor geändert werden.
        /// </summary>
        private void InitializeComponent()
        {
            this.richTextBox1 = new System.Windows.Forms.RichTextBox();
            this.button1 = new System.Windows.Forms.Button();
            this.button2 = new System.Windows.Forms.Button();
            this.button3 = new System.Windows.Forms.Button();
            this.SuspendLayout();
            // 
            // richTextBox1
            // 
            this.richTextBox1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this.richTextBox1.Location = new System.Drawing.Point(13, 126);
            this.richTextBox1.Name = "richTextBox1";
            this.richTextBox1.Size = new System.Drawing.Size(289, 80);
            this.richTextBox1.TabIndex = 0;
            this.richTextBox1.Text = "";
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(13, 13);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(92, 23);
            this.button1.TabIndex = 3;
            this.button1.Text = "top anchor";
            this.button1.UseVisualStyleBackColor = true;
            // 
            // button2
            // 
            this.button2.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
            this.button2.Location = new System.Drawing.Point(13, 75);
            this.button2.Name = "button2";
            this.button2.Size = new System.Drawing.Size(92, 23);
            this.button2.TabIndex = 2;
            this.button2.Text = "bottom anchor";
            this.button2.UseVisualStyleBackColor = true;
            // 
            // button3
            // 
            this.button3.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
            this.button3.Location = new System.Drawing.Point(227, 75);
            this.button3.Name = "button3";
            this.button3.Size = new System.Drawing.Size(75, 23);
            this.button3.TabIndex = 1;
            this.button3.Text = "Hide Rtb";
            this.button3.UseVisualStyleBackColor = true;
            this.button3.Click += new System.EventHandler(this.button3_Click);
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(314, 226);
            this.Controls.Add(this.button1);
            this.Controls.Add(this.button2);
            this.Controls.Add(this.button3);
            this.Controls.Add(this.richTextBox1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.ResumeLayout(false);

        }

        #endregion

        private System.Windows.Forms.RichTextBox richTextBox1;
        private System.Windows.Forms.Button button3;
        private System.Windows.Forms.Button button2;
        private System.Windows.Forms.Button button1;
    }
}

和代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace HideShowTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (richTextBox1.Visible)
            {
                richTextBox1.Hide();

                this.Height -= richTextBox1.Height;
            }
            else
            {
                richTextBox1.Show();

                this.Height += richTextBox1.Height;
            }
            button3.Text = richTextBox1.Visible ? "Hide Rtb" : "Show Rtb";
        }
    }
}

如果我点击“隐藏 Rtb”,底部的锚按钮会向上移动(它应该停留在同一个地方)。

【问题讨论】:

  • repro代码有一个明显的bug,button2和button3的Anchor属性不正确。锚定到底部,减小 from 的大小会将它们从 Y=75 移动到 Y=-5。你当然不希望他们在那里。

标签: winforms toggle richtextbox


【解决方案1】:

编辑:因为您编辑了问题并询问了完全不同的事情:

您将 button2 的 Anchor 属性设置为 Left 和 Bottom,因此当您调整表单大小以保持按钮与底部之间的距离相同时,它显然会移动表格。

为避免这种情况,您可以将Anchor 属性设置为Top 或在更改表单大小时相应地更改按钮的位置。例如:

int t = button2.Top;
ToggleRtbVisibility();
button2.Top = t;

原答案

1) 如果要切换“显示和隐藏”,请隐藏和显示控件,不要删除它。

2) 我怀疑您检查了表单的Size 属性而不是ClientSize 属性,但是您正在更改ClientSize 的值。另外,不要使用固定数字来改变大小,而是计算它们。

试试这样的:

private void ToggleRtbVisibility(bool hide)
{
    rtbLog.Visible = !hide;
    int preservedHeight = rtbLog.Height + rtbLog.Margin.Top + rtbLog.Margin.Bottom;
    if (hide)
    {
        this.Height -= preservedHeight;
    }
    else
    {
        this.Height += preservedHeight;
    }
}

如果你不想使用参数来显式指定是显示还是隐藏控件,你可以把上面的方法改成这样:

private void ToggleRtbVisibility()
{
    rtbLog.Visible = !rtbLog.Visible;
    int preservedHeight = rtbLog.Height + rtbLog.Margin.Top + rtbLog.Margin.Bottom;
    if (rtbLog.Visible == false)
        this.Height -= preservedHeight;
    else
        this.Height += preservedHeight;
}

希望对您有所帮助。

【讨论】:

  • 谢谢艾哈迈德,描述这个问题并不容易:我做了一个样本(上图),应该可以证明这个问题。
  • 请把上面的答案去掉(因为不是答案)并添加相关信息into your question
  • 另外,这似乎是一个不同的问题。你的原来的问题解决了吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-13
  • 2017-04-13
  • 1970-01-01
  • 2018-02-26
  • 1970-01-01
  • 2014-07-17
相关资源
最近更新 更多