简体   繁体   English

奇怪的自动滚动行为

[英]Strange AutoScroll behavior

Introduction 介绍

I have written a simple user control. 我已经编写了一个简单的用户控件。

The control contains a FlowLayoutPanel and a button on it. 该控件包含一个FlowLayoutPanel和一个按钮。

The button click event handler is as follows: 按钮单击事件处理程序如下:

private void uxAdd_Click(object sender, EventArgs e)
{
    count++;
    Label label = new Label();
    label.Text = "Label " + count.ToString();
    uxFlowLayout.Controls.Add(label);
}

The Add button adds a label onto the FlowLayoutPanel and because the FlowLayoutPanel.AutoSize and control's AutoSize properties are equal to true and FlowLayoutPanel.FlowDirection is equal to TopDown the control will be increased in height. Add按钮将标签AddFlowLayoutPanel上,并且由于FlowLayoutPanel.AutoSize和控件的AutoSize属性等于true且FlowLayoutPanel.FlowDirection等于TopDown因此控件的高度将增加。

I've put my user control on a form and set the form's AutoScroll property to true. 我已经将用户控件放在窗体上,并将窗体的AutoScroll属性设置为true。

Also I wrote the following Layout event handler: 我还编写了以下Layout事件处理程序:

private void MainForm_Layout(object sender, LayoutEventArgs e)
{
    uxSection.Width = this.ClientSize.Width;
}

( uxSection is a user control that I'm talking about in the beginning of the Introduction section) uxSection是我在“ 简介”部分的开头要谈论的用户控件)

Problem 问题

Here is what I'm doing: 这是我在做什么:

Step 0 . 步骤0 Run the program. 运行程序。

Step 1 . 步骤1 Add a few labels so that the vertical scrollbar will appear. 添加一些标签,以便出现垂直滚动条。

Step 2 . 第二步 Scroll to the bottom of the form. 滚动到窗体的底部。

Step 3 . 第三步 Resize the form horizontally. 水平调整窗体的大小。

在此处输入图片说明

As you can see, after resizing the form, the scrollbar position is not at the bottom (I expect it to be at the bottom). 如您所见,在调整窗体大小之后,滚动条位置不在底部(我希望它在底部)。

So the question is, what am I missing? 所以问题是,我想念什么? Why does the scrollbar resets to the top? 滚动条为什么重置到顶部?

This happens because the whole control is redrawn upon a resize. 发生这种情况是因为在调整大小后重新绘制了整个控件。

You'll want to either keep the location of the scrollbar saved in a variable so that when the resize event occurs you can reset the position of the scrollbar OR you'll want to push the scroll bar to the bottom of the list when a resize event is fired by using the number of items in your list to calculate where the bottom element is and then pushing the scrollbar to that position. 您可能希望将滚动条的位置保存在变量中,以便在发生调整大小事件时可以重置滚动条的位置,或者在调整大小时将滚动条推到列表的底部通过使用列表中的项目数来计算底部元素的位置,然后将滚动条推到该位置,从而触发事件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM