C# 什么是控制;“可视”;在面板中滚动后
我在Windows窗体面板中有一个标签数组(最多50个标签)。垂直滚动数组的“可视”索引时-更改。如何仅获取当前屏幕标签的索引?这里有一种方法可以将面板和每个标签的ClientRectangle转换为屏幕坐标,然后检查交叉点:C# 什么是控制;“可视”;在面板中滚动后,c#,winforms,C#,Winforms,我在Windows窗体面板中有一个标签数组(最多50个标签)。垂直滚动数组的“可视”索引时-更改。如何仅获取当前屏幕标签的索引?这里有一种方法可以将面板和每个标签的ClientRectangle转换为屏幕坐标,然后检查交叉点: public partial class Form1 : Form { private Label[] Labels; public Form1() { InitializeComponent(); Labels
public partial class Form1 : Form
{
private Label[] Labels;
public Form1()
{
InitializeComponent();
Labels = new Label[] { label1, label2, label3, label4, label5 };
}
private void button1_Click(object sender, EventArgs e)
{
List<string> names = new List<string>(); // for demonstration purposes
// determine which Labels are currently visible in the scrolled panel:
Rectangle rectPanel = panel1.RectangleToScreen(panel1.ClientRectangle);
for(int i = 0; i < Labels.Length; i++)
{
Rectangle rectLabel = Labels[i].RectangleToScreen(Labels[i].ClientRectangle);
if (rectLabel.IntersectsWith(rectPanel))
{
// ... do something with "i" ...
names.Add(Labels[i].Name); // for demonstration purposes
}
}
listBox1.DataSource = null; // for demonstration purposes
listBox1.DataSource = names; // for demonstration purposes
}
}
公共部分类表单1:表单
{
自有标签[]标签;
公共表格1()
{
初始化组件();
标签=新标签[]{label1,label2,label3,label4,label5};
}
私有无效按钮1\u单击(对象发送者,事件参数e)
{
列表名称=新列表();//用于演示
//确定哪些标签当前在滚动面板中可见:
矩形矩形面板=面板1.矩形到屏幕(面板1.ClientRectangle);
for(int i=0;i
我认为您需要计算表单的高度和标签的高度(包括填充n个边距)。使用标签的边界并将其与面板的DisplayRectangle()相交。避免在一个面板中放置50个标签,这是非常浪费的。这当然应该是一个列表框或面板的绘制事件处理程序。