遍历控件方法

 1 private void writeContronls(string Rx, int rv)
 2 {
 3     foreach (Control cn in this.groupBox1.Controls)
 4     {
 5        if (cn is TextBox && cn.Name == Rx)
 6         {
 7           cn.Text = "0x" + Convert.ToString(rv, 16);
 8        if (comboBox1.SelectedItem != null)
 9           {
10              richTextBox2.Text += currenass.Text + "     " + comboBox1.SelectedItem.ToString().PadRight(10, ' ') + "
" + cn.Name.PadRight(8, ' ') + cn.Text.ToString().PadLeft(8, ' ') + "
";
11 
12            }
13     }
14 }

由于界面上控件比较多,之前修改某个控件的时候都是在触发事件中一起修改,利用这个代码可以单独修改某个控件的内容。

this代表当前窗口,如果要遍历窗口中的groupbox中的控件,要这样写  

this.groupBox1.Controls

  

原文地址:https://www.cnblogs.com/1024E/p/13207485.html