以前一直在Webform下用Control.FindControl(string)方法来获取页面上的某个控件,可是Winform下面的ControlCollection却没有提供FindControl的方法:
http://msdn.microsoft.com/library/en-us/cpref/html/frlrfsystemwindowsformscontrolcontrolcollectionmemberstopic.asp?frame=true

没办法,只好自己建一个Hash表来实现。


Winform下通过控件名称来获取控件        //Populate data
Winform下通过控件名称来获取控件

;
Winform下通过控件名称来获取控件
Winform下通过控件名称来获取控件                        
Winform下通过控件名称来获取控件
Winform下通过控件名称来获取控件                        
//Create a Hashtable reference to all the TextBoxes
Winform下通过控件名称来获取控件

Winform下通过控件名称来获取控件                        Hashtable htTextBox 
= new Hashtable();
Winform下通过控件名称来获取控件
Winform下通过控件名称来获取控件                        
foreach (Control c in this.Controls)
Winform下通过控件名称来获取控件
{
Winform下通过控件名称来获取控件
Winform下通过控件名称来获取控件                                
if (c.GetType().ToString() == "System.Windows.Forms.TextBox")
Winform下通过控件名称来获取控件
Winform下通过控件名称来获取控件                                        htTextBox.Add(c.Name,c);
Winform下通过控件名称来获取控件
Winform下通过控件名称来获取控件                        }

Winform下通过控件名称来获取控件
Winform下通过控件名称来获取控件                        
//Search TextBox from Hashtable and evalute it.
Winform下通过控件名称来获取控件

Winform下通过控件名称来获取控件                        
for (int i=0;i<10;i++)
Winform下通过控件名称来获取控件
{
Winform下通过控件名称来获取控件
Winform下通过控件名称来获取控件                                TextBox t;
Winform下通过控件名称来获取控件
Winform下通过控件名称来获取控件                                t 
= (TextBox)htTextBox["textBox"+(i+1).ToString()];
Winform下通过控件名称来获取控件
Winform下通过控件名称来获取控件                                t.Text 
= mydata[i].ToString();
Winform下通过控件名称来获取控件
Winform下通过控件名称来获取控件                        }
 
Winform下通过控件名称来获取控件

查了下.NET Framework 2.0的文档,果然发现.NET 2.0中给Control.ControlCollection对象增加了名为Find的方法...
http://msdn2.microsoft.com/library/1hb809fy.aspx


本贴子以“现状”提供且没有任何担保,同时也没有授予任何权利

相关文章:

  • 2022-12-23
  • 2022-02-21
  • 2022-12-23
  • 2022-12-23
  • 2021-08-06
  • 2021-06-09
猜你喜欢
  • 2021-09-16
  • 2022-12-23
  • 2022-12-23
  • 2022-02-04
  • 2022-12-23
  • 2021-08-03
  • 2021-11-25
相关资源
相似解决方案