在 WinForms 中查找包含特定文本的标签? [英] Find a label in WinForms which contains certain text?

查看:23
本文介绍了在 WinForms 中查找包含特定文本的标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 C# Windows 窗体应用程序中,如何在包含我要查找的文本的窗体上找到标签?

In C# Windows form application, how to find the label on the form which contains the text I'm looking for?

例如:我正在尝试搜索 Text 属性包含25"的标签.

For example: I am trying to search a label whose Text property contains "25".

推荐答案

您可以通过以下方式使用 linq 找到它:

You can find it using linq this way:

var control =  this.Controls.OfType<Control>().Where(x => x is Label && x.Text.Contains("25"));

或者@Sayse建议只过滤Label类型:

or as @Sayse suggested just filter on Label type:

var Labelcontrol =  this.Controls.OfType<Label>().Where(x => x.Text.Contains("25"));

说明:

如果我们想获取表单的所有控件,我们必须这样做:

Explanation:

If we want to fetch all controls of the form we have to do :

var AllControls = this.Controls.OfType<Control>();

如果我们只想获取 Label 类型的控件,则:

and if we want to fetch only Controls of Type Label then:

var LabelControls = this.Controls.OfType<Label>();

这里的this指的是当前的应用形式.

Here this refers to current form of application.

如果你在嵌套控件中有标签,意味着在一些用户控件或其他控件中,那么你需要像 this SO post(如何获取特定类型的 Windows 窗体窗体的所有子控件)

If you have label in nested controls, means inside some user control or some other control, then you need to check recrursively as in this SO post (How to get ALL child controls of a Windows Forms form of a specific type)

这篇关于在 WinForms 中查找包含特定文本的标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆