Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 滚动条可见性_C#_Winforms - Fatal编程技术网

C# 滚动条可见性

C# 滚动条可见性,c#,winforms,C#,Winforms,我只编写以下代码: namespace Test01 { partial class Form1 { /// <summary> /// Required designer variable. /// </summary> private System.ComponentModel.IContainer components = null; /// <summary&g

我只编写以下代码:

namespace Test01
{
    partial class Form1
    {
        /// <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 Windows Form 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.panel1 = new System.Windows.Forms.Panel();
            this.button1 = new System.Windows.Forms.Button();
            this.button2 = new System.Windows.Forms.Button();
            this.button3 = new System.Windows.Forms.Button();
            this.button4 = new System.Windows.Forms.Button();
            this.panel1.SuspendLayout();
            this.SuspendLayout();
            // 
            // panel1
            // 
            this.panel1.AutoScroll = true;
            this.panel1.Controls.Add(this.button4);
            this.panel1.Controls.Add(this.button3);
            this.panel1.Controls.Add(this.button2);
            this.panel1.Location = new System.Drawing.Point(24, 13);
            this.panel1.Name = "panel1";
            this.panel1.Size = new System.Drawing.Size(206, 43);
            this.panel1.TabIndex = 0;
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(89, 62);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(75, 23);
            this.button1.TabIndex = 1;
            this.button1.Text = "button1";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.button1_Click);
            // 
            // button2
            // 
            this.button2.Location = new System.Drawing.Point(3, 3);
            this.button2.Name = "button2";
            this.button2.Size = new System.Drawing.Size(75, 23);
            this.button2.TabIndex = 2;
            this.button2.Text = "button2";
            this.button2.UseVisualStyleBackColor = true;
            // 
            // button3
            // 
            this.button3.Location = new System.Drawing.Point(84, 3);
            this.button3.Name = "button3";
            this.button3.Size = new System.Drawing.Size(75, 23);
            this.button3.TabIndex = 3;
            this.button3.Text = "button3";
            this.button3.UseVisualStyleBackColor = true;
            // 
            // button4
            // 
            this.button4.Location = new System.Drawing.Point(165, 3);
            this.button4.Name = "button4";
            this.button4.Size = new System.Drawing.Size(75, 23);
            this.button4.TabIndex = 4;
            this.button4.Text = "button4";
            this.button4.UseVisualStyleBackColor = true;
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(251, 98);
            this.Controls.Add(this.button1);
            this.Controls.Add(this.panel1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.panel1.ResumeLayout(false);
            this.ResumeLayout(false);

        }

        #endregion

        private System.Windows.Forms.Panel panel1;
        private System.Windows.Forms.Button button4;
        private System.Windows.Forms.Button button3;
        private System.Windows.Forms.Button button2;
        private System.Windows.Forms.Button button1;
    }
}
我运行它并得到以下结果:

在第一列中,我只需按一下按钮1,就可以正常工作了

在第二列,我移动滚动条并按下按钮1-按钮4消失,但滚动条仍然存在。当我折叠和打开窗口时,滚动条消失了

在第三栏,我打开经典主题,只需按下按钮1——一切正常


我已经阅读并尝试使用验证、失效、更新、刷新-但没有帮助。那么,如何解决此问题呢?

尝试通过BeginInvoke调用刷新方法,使其重新进入窗口循环:

this.BeginInvoke((Action)scrollBar.PerformLayout);

从当前方法调用它(不调用)有时可以避免重新渲染。

在移除按钮之前,将滚动条的位置重置为零:

    private void button1_Click(object sender, EventArgs e)
    {
        panel1.HorizontalScroll.Value = 0;
        panel1.Controls.Remove(button4);
    }
        panel1.HorizontalScroll.Value = 1;
        panel1.HorizontalScroll.Value = 0;
        panel1.Controls.Remove(button4);
编辑

我发现当滚动条的值为零时,这种方法并不总是有效的。我尝试了各种方法来强制它始终清除滚动条,但唯一可靠的方法似乎是在移除按钮之前将其值设置为1然后设置为0:

    private void button1_Click(object sender, EventArgs e)
    {
        panel1.HorizontalScroll.Value = 0;
        panel1.Controls.Remove(button4);
    }
        panel1.HorizontalScroll.Value = 1;
        panel1.HorizontalScroll.Value = 0;
        panel1.Controls.Remove(button4);

这很难看,但似乎管用。我个人认为这是框架中的一个bug

尝试用FlowLayoutPanel替换您的面板。使用与要更换的面板相同的尺寸。在FlowLayoutPanel控件上,设置
AutoScroll=True
FlowDirection=TopDown


面板应该可以为您移动控件,您也不必担心滚动条。

出于好奇,我刚刚尝试了这个,发现它与面板的问题完全相同。这种行为似乎是
AutoScroll
属性中的一个bug,它会影响所有可滚动控件。