方法很好用.目的是遍历所有容器的子控件... 方法1
private void GetControl(Control.ControlCollection ctc, ref int checkNull)
{
foreach (Control ct in ctc)
{
if (ct is TextBox)
{
if (ct.Text.Length <= 0)
{
checkNull = 1;
}
}

//C#只遍历窗体的子控件,不遍历孙控件
//当窗体上的控件有子控件时,需要用递归的方法遍历,才能全部列出窗体上的控件
if (ct.HasChildren)
{
GetControl(ct.Controls,ref checkNull);
}
}
}
调用的时候直接 这样用 ==========c# 遍历子控件,比如Form下的group,或者panel
int check = 0;
GetControl(this.groupBox1.Controls, ref check);
if (check != 1)
{
运行.....
}
else
{
MessageBox.Show("请输入必要参数");
}
==============================方法2 ,不过功能没上面的好用

//遍历控件
public void OperateControls(Control control)
{
foreach (Control c in control.Controls)
{
if (c is Panel)
{
OperateControls(c);
}
if (c is GroupBox)
{
OperateControls(c);
}
if (c is TextBox)
{
// 它是 TextBox, 要干什么随便你
if (c.Text == "")
{
MessageBox.Show("必填参数不能为空!");
return;
}
}
}
}
===========c# 遍历子控件,比如Form下的group,或者panel===== 下面是参考资料
private void button1_Click(object sender, EventArgs e)
{
listBox1.Items.Clear();
GetControl(Controls);
}
private void GetControl(Control.ControlCollection ctc)
{
foreach (Control ct in ctc)
{
//调用AddControlInofToListBox方法获取控件信息
AddControlInofToListBox(ct);
//C#只遍历窗体的子控件,不遍历孙控件
//当窗体上的控件有子控件时,需要用递归的方法遍历,才能全部列出窗体上的控件
if (ct.HasChildren)
{
GetControl(ct.Controls);
}
}
} private void AddControlInofToListBox(Control ct)
{
switch (ct.GetType().Name)
{
//如果是ListBox、CheckBox、Button
case "ListBox":
case "GroupBox":
case "Button":
listBox1.Items.Add("控件名:" + ct.Name);
break;
//如果是CheckBox
case "CheckBox":
if (((CheckBox)ct).Checked)
{
listBox1.Items.Add("控件名:" + ct.Name + ",是否选中:是");
//((CheckBox)ct).Checked = false;
}
else
{
listBox1.Items.Add("控件名:" + ct.Name + ",是否选中:否");
//((CheckBox)ct).Checked = true;
}
break;
//如果是RadioButton
case "RadioButton":
RadioButton rdb = (RadioButton)ct;
if (rdb.Checked)
{
listBox1.Items.Add("控件名:" + ct.Name + ",是否选中:是");
//rdb.Checked = false;
}
else
{
listBox1.Items.Add("控件名:" + ct.Name + ",是否选中:否");
//rdb.Checked = true;
}
break;
//其它值
default:
listBox1.Items.Add("控件名:" + ct.Name + ",值:" + ct.Text);
break;
}
}

//遍历groupBox1控件的子控件 private void button2_Click(object sender, EventArgs e)
{
listBox1.Items.Clear();
//可以共用上面的,遍历窗体控件的方法
//只是参数由Controls改为groupBox1.Controls
GetControl(groupBox1.Controls);
}
-------------------------------------
foreach(TextBox t in this.Controls)
{
MessageBox.Show(t.Text);
}
for(int i=0;this.Controls.Count-1;i++)
if(this.Controls[i] is TextBox)
this.Controls[i].Text="";
/// <summary>
/// 设置此界面中控件的某些属性
/// </summary>
/// <param name="ctl"></param>
public void Set_Controls(Control ctl)
{
//当控件没有子控件时
if ( !ctl.HasChildren)
{
switch(ctl.GetType().ToString())
{
case "System.Windows.Forms.Label":
break;
case "System.Windows.Forms.Button":
break;
case "System.Windows.Forms.TextBox":
break;
case "System.Windows.Forms.ListView":
break;
case "System.Windows.Forms.GroupBox":
break;
case "System.Windows.Forms.ComboBox":
break;
case "System.Windows.Forms.ImageList":
break;
case "System.Windows.Forms.DataGrid":
break;
case "System.Windows.Forms.MainMenu":
break;
case "System.Windows.Forms.TreeView":
break;
}
}
else //当控件有子控件时
{
int i = 0;
while ( i < ctl.Controls.Count )
{
Set_Controls( ctl.Controls[i] );
i ++;
}
}
}

你调用时,可以这样用:
Set_Controls(this);
private void button1_Click(object sender, System.EventArgs e)
{
foreach(TextBox s in this.Controls )
{
MessageBox.Show (s.ToString());

}
}
foreach(Control c in Controls)
{
if(c is TextBox)
MessageBox.Show("good");
}
===============

c# 遍历子控件,比如Form下的group,或者panel的更多相关文章

  1. C# WPF 之 遍历子控件

    /// <summary> /// 检查非空字段 /// </summary> /// <param name="IsOk"></para ...

  2. C#遍历窗体控件(原文出自http://www.liangshunet.com/ca/201403/286434593.htm)

    一.C#遍历窗体控件 主要遍历属于窗体(Form)的控件(Controls),假如窗体中有 Panel.Button 和 TextBox 控件,遍历代码如下: /// <summary> ...

  3. 记录下UIButton的图文妙用和子控件的优先显示

    UIButton的用处特别多,这里只记录下把按钮应用在图文显示的场景,和需要把图片作为按钮的背景图片显示场景: 另外记录下在父控件的子控件优先显示方法(控件置于最前面和置于最后面). 先上效果图: 1 ...

  4. 记录下帮助一位网友解决的关于android子控件的onTouch或onClick和父OnTouch 冲突的问题。

    前三天收到位网友的私信求助,问题大概如标题所示.具体是下面的情况,个人感觉,这个问题挺有趣,也会在实际项目开发中很常见.不想看前奏的请直接跳至解决方法. 问题原型: 父控件是自定义的 LinearLa ...

  5. 五种情况下会刷新控件状态(刷新所有子FWinControls的显示)——从DFM读取数据时、新增加子控件时、重新创建当前控件的句柄时、设置父控件时、显示状态被改变时

    五种情况下会刷新控件状态(刷新控件状态才能刷新所有子FWinControls的显示): 在TWinControls.PaintControls中,对所有FWinControls只是重绘了边框,而没有整 ...

  6. wpf 寻找某个控件下的子控件

    /// <summary> /// 寻找某个控件下的子控件 /// </summary> /// <typeparam name="ChildType" ...

  7. wpf 父控件和子控件 各自触发鼠标按下事件

    父控件 PreviewMouseDown子控件 MouseDown

  8. OnClick事件的Sender参数的前世今生——TWinControl.WinProc优先捕捉到鼠标消息,然后使用IsControlMouseMsg函数进行消息转发给图形子控件(意外发现OnClick是由WM_LBUTTONUP触发的)

    这是一个再普通不过的Button1Click执行体: procedure TForm1.Button1Click(Sender: TObject); begin ShowMessage('I am B ...

  9. 如何有效地让一个“ParentFont = False”子控件使用与父母相同的字体名称?

    如何有效地让一个“ParentFont = False”子控件使用与父母相同的字体名称?(How to efficiently let a `ParentFont = False` child con ...

随机推荐

  1. jQuery的delegate()与proxy()方法

    1. jQuery 事件 - delegate() 方法 定义和用法 delegate() 方法为指定的元素(属于被选元素的子元素)添加一个或多个事件处理程序,并规定当这些事件发生时运行的函数. 使用 ...

  2. 集群因子(Clustering Factor)

    clustering factor是CBO使用的统计信息,用来衡量一个表中的列是否是规则排序存放的. 在通过索引访问表的时候,被用来作为代价评估的指示器.扫描索引的时候,clustering fact ...

  3. java.util.concurrent包

    在JavaSE5中,JUC(java.util.concurrent)包出现了 在java.util.concurrent包及其子包中,有了很多好玩的新东西: 1.执行器的概念和线程池的实现.Exec ...

  4. tableview隐藏多余分割线

    - (void)setExtraCellLineHidden: (UITableView *)tableView{ UIView *view =[ [UIView alloc]init]; view. ...

  5. Leetcode: Longest Substring with At Least K Repeating Characters

    Find the length of the longest substring T of a given string (consists of lowercase letters only) su ...

  6. TPageControl组件

    TPageControl的功能是创建多个Dialog页,而这些重叠的每一个页Dialog就是通过TTabSheet对象实现的

  7. poj: 2159

    简单题,看起来很凶 #include <iostream> #include <stdio.h> #include <string> #include <st ...

  8. 今天遇到的一个问题(windows的ssh客户端连接不到虚拟机Ubuntu)

    今天比较郁闷,想用windows上的ssh客户端连接虚拟机中的Ubuntu. 但是死活连不上,之前是能脸上的,所以比较郁闷. 我首先在windows上ping Ubuntu的ip地址,竟然发不了数据包 ...

  9. linux第13天 生产者与消费者

    pthread_cond_t   my_condition = PTHREAD_COND_INITIALIZER; pthread_mutex_t mutex = PTHREAD_MUTEX_INIT ...

  10. 异常:Struts:org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find BasicDataSource

    org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [org.apache.common ...