在面板中滚动后,哪些控件是“可查看的”

时间:2013-06-10 14:24:54

标签: c# winforms

我在Windows窗体面板中有一个标签数组(最多50个标签)。当垂直滚动数组的“可查看”索引时 - 更改。如何只获取现在屏幕标签的索引?

1 个答案:

答案 0 :(得分:0)

这是一种将Panel的ClientRectangle和每个Label转换为屏幕坐标,然后检查交叉点的方法:

Visbile Labels in Scrolled Panel

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
    }

}