改进的适用于Windows桌面和移动设备的FindControl(译文)
By S.F.
本文链接 https://www.kyfws.com/news/improved-findcontrol-for-windows-desktop-and-mobil/
版权声明 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
- 3 分钟阅读 - 1075 个词 阅读量 0改进的适用于Windows桌面和移动设备的FindControl(译文)
原文地址:https://www.codeproject.com/Articles/45618/Improved-FindControl-for-Windows-desktop-and-mobil
原文作者:Joel Ivory Johnson
译文由本站翻译
前言
Windows桌面和移动设备的改进的FindControl.
几天[] ()a g o I p o s t e d s o m e [c [] ()o d e ] ([[()h t t [](] p :[] ()//w w w .j []()2 i .[] ()n e t /B l o g E n [] ()g i n e /p o s t /[] ()2 0 0 9 /1 1 /2 5 [] ()/[F i n d C o n t [] ()r o l ] (h t t p :[] ()//m s d n .m i c [] ()r o s o f t .c o [] m)] [] ()/e n -u s /l i b [] ()r a r y /4 8 6 w c [] ()6 4 h .a s p x )-[] ()f o r -W i n d o w [] ()s -F o r m s .a s [] ()p x )c o n t a i n i n g a n i [] m] p l e m e n t a t i o n f o r a W i n d o w s F o r m s v e r s i o n o f [F i n d [] C)] [ ()o n t r o l ] (h t [] ()t p ://m s d n . ()微软.[] ()c o m /e n -u s /[] ()l i b r a r y /4 8 [] ()6 w c 6 4 h .a s p [] ()x ).A v i a d P .p o i n t e d o u t a c o r r e c t i o n a n d a w a y t h a t [] ()t h e r o u t i n e c a n b e s [] ()i m l i f i e d .W h i l e I h a d i n t e n d e d t h e o [] ()r i g i n a l [c o d e ] (h t t [](] p ://w w w .j []()2 i .n e t /B l o g E n g i n e /p o s t /2 0 0 9 /1 1 /2 5 /[F i n d C o n t r o l ] (h t t p ://m s d n .m i c r o s o f t .c o [] m]] [[ )/e n -u s /l i b r a r y /4 8 6 w c 6 4 h .a s p x )-f o r -W i n d o w s -F o r m s .a s p x )t [](] o d o a b r e a d t h -f [] ()i r s t s e a r c h ,i t w a s d o i n g a c o m b i n a t i o n o f d e p t h a n d b r e a d t h [] ().T h e f u n c t i o n a l i t y [] [] c o u l d a l s o [] ()b e i m p l e m e n t e d w i t h a s i n g l e l o [] ()o p .T h e r e s u l t [](] o f t h e m e s s a g e [](] s ,[] ()t h e r e s u l t i n g [c o d e ] (h t t [](] p ://w w w .j []()2 i .n e t /B l o g E n g i n e /p o s t /2 0 0 9 /1 1 /2 5 /[F i n d C o n t r o l ] ([h t t p ://m s d n .m i c r o s o f t .c o m /e n -u s /l i b r a r y /4 8 6 w c 6 4 h .a s p x ) -f o r -W i n d o w s -F o r m s .a s p x )f r o m [] ()o u r c o m m u n i c a t i o n ,i s s h o w n b e l o w :
Control FindControl(string target) {
return FindControl(this,target);
}
static Control FindControl(Control root, string target){
if(root.Name == target)
return root;
List currentLevel = new List() { root };
while (currentLevel.Count > 0)
{
Control match = currentLevel.FirstOrDefault(x => x.Name == target);
if (match != null) return match;
currentLevel = currentLevel.SelectMany(x => x.Controls.Cast()).ToList();
} return null;
}
许可
本文以及所有相关的源代码和文件均已获得The Code Project Open License (CPOL)的许可。
All-Topics 新闻 翻译