使用foreach循环来获取文本框的一个分组框之内 [英] Using a foreach loop to retrieve TextBox's within a GroupBox

查看:312
本文介绍了使用foreach循环来获取文本框的一个分组框之内的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有十个组框在的WinForm 。每个组中包含10个文本框,我已经定义的每个文本框名称。使用foreach循环我怎样才能得到每个文本框?

解决方案

 的foreach(控制GB的this.Controls)
 {
       如果(GB为分组框)
       {
          的foreach(在gb.Controls控制TB)
          {
             如果(tb是文本框)
             {
                 //这里是您访问所有textboxs。
             }
          }
       }
 }
 

但是,如果您定义的每个文本框的名字 什么是点一个循环来获取每个文本框

您可以定义一个名单,其中,文本框> 来保存每个参考文本框在创建它们,然后只要进入虽然列表来获取访问的每个文本框

I have ten group boxes in a WinForm. Each group box contains 10 text boxes, and I have defined each TextBox name. How can I get each text box using a foreach loop?

解决方案

 foreach(Control gb in this.Controls)
 {
       if(gb is GroupBox)
       {
          foreach(Control tb in gb.Controls)
          {
             if(tb is TextBox)
             {
                 //here is where you access all the textboxs.
             }
          }
       }
 }

But if you have defined each TextBox name What's the point to get each TextBox by a loop?

You could define a List<TextBox> to hold reference of each TextBox while creating them, then just go though the List to get access of each TextBox.

这篇关于使用foreach循环来获取文本框的一个分组框之内的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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