简体   繁体   English

TableLayoutPanel仅能在启动时按可见点找到控件

[英]TableLayoutPanel is only able to find Controls by Point that were in the visible area on startup

I have a TableLayoutPanel with a scrollbar (because the total height of the Controls is bigger than the height of the TableLayoutPanel ). 我有一个带有滚动条的TableLayoutPanel (因为Controls的总高度大于TableLayoutPanel的高度)。 I try to scroll programmatically to the next/previous row with the PageUp- and PageDown-Keys. 我尝试使用PageUp-和PageDown-Keys以编程方式滚动到下一行/上一行。

Scrolling works, but only as far as I want to scroll to a Control that was already in the visible area when the program started. 滚动有效,但仅在我想滚动到程序启动时已经在可见区域中的Control范围内。 When I try to get a Control further down (that has meanwhile entered the visible area), tlp.GetChildAtPoint(new Point(1, tlp.AutoScrollPosition.Y * -1 + 2)) returns null . 当我尝试使控件进一步下降(同时进入可见区域)时, tlp.GetChildAtPoint(new Point(1, tlp.AutoScrollPosition.Y * -1 + 2))返回null

Can someone please help me to resolve this issue? 有人可以帮我解决这个问题吗?

Note: I found a workaround (see my answer), but I am of course open to other solutions. 注意:我找到了一种解决方法(请参阅我的答案),但是我当然可以接受其他解决方案。


Code to reproduce the problem: 重现该问题的代码:

public class FormTLPTest : Form
{
    TableLayoutPanel tlp;

    public FormTLPTest()
    {
        Height = 200;
        Width = 200;
        KeyPreview = true;

        tlp = new TableLayoutPanel();
        tlp.Dock = DockStyle.Fill;
        tlp.CellBorderStyle = TableLayoutPanelCellBorderStyle.Single;
        tlp.AutoScroll = false;
        tlp.VerticalScroll.Visible = true;
        tlp.HorizontalScroll.Visible = false;
        tlp.ColumnStyles.Add(new ColumnStyle(SizeType.Absolute, 40));

        Controls.Add(tlp);

        tlp.SuspendLayout();

        for (int i = 0; i < 20; i++)
        {
            Label lb2 = new Label();
            lb2.Margin = new Padding(0);
            lb2.Dock = DockStyle.Fill;
            lb2.BackColor = Color.White;
            lb2.Text = "Line " + i;

            tlp.Controls.Add(lb2, 0, i);
            tlp.RowStyles.Add(new RowStyle(SizeType.Absolute, 18));
            tlp.RowCount++;
        }

        tlp.ResumeLayout();
        tlp.AutoScroll = true;

        KeyDown += FormTLPTest_KeyDown;
    }

    private void FormTLPTest_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.PageUp || e.KeyCode == Keys.PageDown || e.KeyCode == Keys.Next)
        {
            Control currentTopControl = tlp.GetChildAtPoint(new Point(1, tlp.AutoScrollPosition.Y * -1 + 2));
            int currentRow = 0;
            if (currentTopControl != null)
                currentRow = tlp.GetPositionFromControl(currentTopControl).Row;

            int nextRow = -1;
            if (e.KeyCode == Keys.PageUp)
                nextRow = currentRow - 1;
            else
                nextRow = currentRow + 1;

            if (nextRow < 0 || nextRow > tlp.RowCount - 1)
                return;

            Control nextTopControl = tlp.GetControlFromPosition(0, nextRow);
            tlp.AutoScrollPosition = nextTopControl.Location;
        }
    }
}

I found a solution avoiding the GetChildAtPoint -method. 我找到了避免GetChildAtPoint方法的解决方案。 It checks the location of every control of the first column to get the first visible column. 它检查第一列的每个控件的位置,以获取第一个可见列。

I also had to replace .AutoScrollPosition = ... with a scroll to the last control and then to the desired control (I want the control to be at the top of the visible area). 我还必须将.AutoScrollPosition = ...替换为滚动到最后一个控件,然后再滚动到所需的控件(我希望该控件位于可见区域的顶部)。


changed KeyDown -listener: 更改了KeyDown -listener:

private void FormTLPTest_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.PageUp || e.KeyCode == Keys.PageDown || e.KeyCode == Keys.Next)
    {
        int currentRow = 0;
        for (int y=0;y<tlp.RowCount;y++)
        {
            Control c = tlp.GetControlFromPosition(0, y);
            if (c.Location.Y >= 0)
            {
                currentRow = y;
                break;
            }
        }

        int nextRow = -1;
        if (e.KeyCode == Keys.PageUp)
            nextRow = currentRow - 1;
        else
            nextRow = currentRow + 1;

        if (nextRow < 0 || nextRow > tlp.RowCount - 1)
            return;

        Control nextTopControl = tlp.GetControlFromPosition(0, nextRow);
        tlp.ScrollControlIntoView(tlp.GetControlFromPosition(0, tlp.RowCount - 1));
        tlp.ScrollControlIntoView(nextTopControl);
    }
}

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

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