从Form中删除控件:空间仍然被占用

时间:2018-05-02 09:23:57

标签: winforms toggle richtextbox

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

它被移除,但rtbcontrol保留的原始空间仍然可见为空灰色空间。 看起来,它只影响固定在底部的控件。

我做了一个小样本来证明我的问题: Desinger:

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”,则底部锚点按钮会向上移动(它应该保持在同一位置)。

1 个答案:

答案 0 :(得分:0)

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

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

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

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

原始回答

1)如果你想切换&#34;显示和隐藏&#34;,请隐藏并显示控件,不要删除它。

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;
}

希望有所帮助。