首页 > 解决方案 > 如何在 FlowLayoutPanel 中查找标签

问题描述

我有 ac#FlowLayoutPanel容器,我在其中添加了许多Label.Text设置为不同值的标签,即Label.Text = "ABCDEF".

搜索容器中的所有标签以找到带有 的特定标签的最佳方法是Text = "ABCDEF"什么?

谢谢你

标签: c#labelflowlayoutpanel

解决方案


You can find the label with text as follow:

foreach (var item in flowLayoutPanel1.Controls)
{
    if (item is Label)
    {
        if ("ASDF" == ((Label)item).Text)
        {
            MessageBox.Show("found it");
        }
    }
}

Also if you know your component's name, you can search it as below:

foreach (var item in flowLayoutPanel1.Controls.Find("label1", true))
{
    if ("ASDF" == ((Label) item).Text)
    {
        MessageBox.Show("found it");
    }
}

推荐阅读