方法很好用.目的是遍历所有容器的子控件... 方法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. redmine一键安装

    参加项目 bitnami项目  https://bitnami.com/stack/redmine/installer 百度网盘地址为:http://pan.baidu.com/s/1jESnO

  2. The prefix "mx" for element "mx:WindowedApplication" is not bound.

    <mx:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009"  > ......出现了错误The p ...

  3. Oracle 10G如何从recovery catalog中Unregister目标数据库

    从10g开始,RMAN简化了unregister目标数据库的步骤 方法1: $rman target system/oracle@test catalog rman/rman@catadb rman& ...

  4. Fusioncharts使用说明

    背景 最近由于工作需要,再次接触到了Fusioncharts,但也有不足之处,现在官网上似乎是不支持flash的版本了,只能看到html5相关的javascript版本,无奈再次从网上搜索到了一些别人 ...

  5. Java 多线程 并发编程

    一.多线程 1.操作系统有两个容易混淆的概念,进程和线程. 进程:一个计算机程序的运行实例,包含了需要执行的指令:有自己的独立地址空间,包含程序内容和数据:不同进程的地址空间是互相隔离的:进程拥有各种 ...

  6. 体验Java的封装性

    package com.cnblogs.java; //体验Java的封装性 /* * 如下的人类年龄赋值-300岁,显然很不合理,这种直接对类的属性赋值,有时候虽然不合理但却会编译通过. * 所以我 ...

  7. Linux Ubuntu常用终端命令

    查看cpu温度: 安装命令如下:sudo apt-get install acpi 然后acpi -t 即可 输入法配置窗口命令: fcitx-config-gtk3 im-config 任务管理器命 ...

  8. [原创]java WEB学习笔记87:Hibernate学习之路-- -映射 继承关系(subclass , joined-subclass,union-subclass )

    本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...

  9. 关于LED 流水灯的软件调试方法(非开发板调试)

    因为: 硬件 norflash 有寿命,所以尽量少用,而且自己也不会把 程序在 KEIL中从SDRAM 中调试,不会设置.所以采取软件虚拟的方法调试. 主要修改一下几部分: 1.  ledcircle ...

  10. zw版【转发·台湾nvp系列Delphi例程】HALCON color_fuses2

    zw版[转发·台湾nvp系列Delphi例程]HALCON color_fuses2 procedure TForm1.Button1Click(Sender: TObject);var w, h : ...