TableLayoutPanel 只能按点找到启动时可见区域中的控件
TableLayoutPanel is only able to find Controls by Point that were in the visible area on startup
我有一个带滚动条的 TableLayoutPanel
(因为控件的总高度大于 TableLayoutPanel
的高度)。我尝试使用 PageUp 和 PageDown 键以编程方式滚动到 next/previous 行。
滚动有效,但仅限于我想滚动到程序启动时已经在可见区域中的控件。 当我尝试进一步向下移动控件(同时进入可见区域)时,tlp.GetChildAtPoint(new Point(1, tlp.AutoScrollPosition.Y * -1 + 2))
returns null
.
有人可以帮我解决这个问题吗?
注意:我找到了解决方法(请参阅我的回答),但我当然愿意接受其他解决方案。
重现问题的代码:
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;
}
}
}
我找到了避免 GetChildAtPoint
方法的解决方案。它检查第一列的每个控件的位置以获得第一个可见列。
我还必须将 .AutoScrollPosition = ...
替换为滚动到最后一个控件,然后滚动到所需的控件(我希望控件位于可见区域的顶部)。
已更改 KeyDown
-侦听器:
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);
}
}
我有一个带滚动条的 TableLayoutPanel
(因为控件的总高度大于 TableLayoutPanel
的高度)。我尝试使用 PageUp 和 PageDown 键以编程方式滚动到 next/previous 行。
滚动有效,但仅限于我想滚动到程序启动时已经在可见区域中的控件。 当我尝试进一步向下移动控件(同时进入可见区域)时,tlp.GetChildAtPoint(new Point(1, tlp.AutoScrollPosition.Y * -1 + 2))
returns null
.
有人可以帮我解决这个问题吗?
注意:我找到了解决方法(请参阅我的回答),但我当然愿意接受其他解决方案。
重现问题的代码:
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;
}
}
}
我找到了避免 GetChildAtPoint
方法的解决方案。它检查第一列的每个控件的位置以获得第一个可见列。
我还必须将 .AutoScrollPosition = ...
替换为滚动到最后一个控件,然后滚动到所需的控件(我希望控件位于可见区域的顶部)。
已更改 KeyDown
-侦听器:
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);
}
}