我在Windows窗体面板中有一个标签数组(最多50个标签)。当垂直滚动数组的“可查看”索引时 - 更改。如何只获取现在屏幕标签的索引?
答案 0 :(得分:0)
这是一种将Panel的ClientRectangle和每个Label转换为屏幕坐标,然后检查交叉点的方法:
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
}
}