能够找到通过Tag属性WinForm控件 [英] Ability to find WinForm control via the Tag property
问题描述
我在C#中的工作在现有的WinForm的项目。原来的代码使用标签来传达硬件寻址信息为一串代表在连接单片机系统的某些硬件寄存器文本框。我知道如何找到使用Control.ControlCollection.Find方法寻找它的名称未知的控制,但它是我不清楚我是否可以找到标签(只是在这种情况下字符串)控制。
I am working in C# on an existing WinForm project. The original code uses Tag to convey hardware addressing info for a bunch of textboxes that represent certain hardware registers in a connected microcontroller system. I know how to find an unknown control by searching for its Name using the Control.ControlCollection.Find method, but it's unclear to me on whether I can find the control by the Tag (just a string in this instance).
推荐答案
在我的评论跟进:
private void FindTag(Control.ControlCollection controls)
{
foreach (Control c in controls)
{
if (c.Tag != null)
//logic
if (c.HasChildren)
FindTag(c.Controls); //Recursively check all children controls as well; ie groupboxes or tabpages
}
}
然后你就可以得到控制的名称在if语句,做任何你想从那里做。
Then you can get the control name in the if statement and do whatever you want to do from there.
这篇关于能够找到通过Tag属性WinForm控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!