c# 遍历子控件,比如Form下的group,或者panel
方法很好用.目的是遍历所有容器的子控件... 方法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的更多相关文章
- C# WPF 之 遍历子控件
/// <summary> /// 检查非空字段 /// </summary> /// <param name="IsOk"></para ...
- C#遍历窗体控件(原文出自http://www.liangshunet.com/ca/201403/286434593.htm)
一.C#遍历窗体控件 主要遍历属于窗体(Form)的控件(Controls),假如窗体中有 Panel.Button 和 TextBox 控件,遍历代码如下: /// <summary> ...
- 记录下UIButton的图文妙用和子控件的优先显示
UIButton的用处特别多,这里只记录下把按钮应用在图文显示的场景,和需要把图片作为按钮的背景图片显示场景: 另外记录下在父控件的子控件优先显示方法(控件置于最前面和置于最后面). 先上效果图: 1 ...
- 记录下帮助一位网友解决的关于android子控件的onTouch或onClick和父OnTouch 冲突的问题。
前三天收到位网友的私信求助,问题大概如标题所示.具体是下面的情况,个人感觉,这个问题挺有趣,也会在实际项目开发中很常见.不想看前奏的请直接跳至解决方法. 问题原型: 父控件是自定义的 LinearLa ...
- 五种情况下会刷新控件状态(刷新所有子FWinControls的显示)——从DFM读取数据时、新增加子控件时、重新创建当前控件的句柄时、设置父控件时、显示状态被改变时
五种情况下会刷新控件状态(刷新控件状态才能刷新所有子FWinControls的显示): 在TWinControls.PaintControls中,对所有FWinControls只是重绘了边框,而没有整 ...
- wpf 寻找某个控件下的子控件
/// <summary> /// 寻找某个控件下的子控件 /// </summary> /// <typeparam name="ChildType" ...
- wpf 父控件和子控件 各自触发鼠标按下事件
父控件 PreviewMouseDown子控件 MouseDown
- OnClick事件的Sender参数的前世今生——TWinControl.WinProc优先捕捉到鼠标消息,然后使用IsControlMouseMsg函数进行消息转发给图形子控件(意外发现OnClick是由WM_LBUTTONUP触发的)
这是一个再普通不过的Button1Click执行体: procedure TForm1.Button1Click(Sender: TObject); begin ShowMessage('I am B ...
- 如何有效地让一个“ParentFont = False”子控件使用与父母相同的字体名称?
如何有效地让一个“ParentFont = False”子控件使用与父母相同的字体名称?(How to efficiently let a `ParentFont = False` child con ...
随机推荐
- ios webView 默认有缓存
ios webview清除缓存. ios默认webview是有缓存的,所以不改变URL的话,刷新不了网页数据,或者像我这样写 NSMutableURLRequest *request = [NSMut ...
- AJAX POST&跨域 解决方案 - CORS(转载)
跨域是我在日常面试中经常会问到的问题,这词在前端界出现的频率不低,主要原因还是由于安全限制(同源策略, 即JavaScript或Cookie只能访问同域下的内容),因为我们在日常的项目开发时会不可避免 ...
- CSS3的文字阴影—text-shadow
text-shadow还没有出现时,大家在网页设计中阴影一般都是用photoshop做成图片,现在有了css3可以直接使用text-shadow属性来指定阴影. 这个属性可以有两个作用,产生阴影和模糊 ...
- Leetcode: Strobogrammatic Number III
A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside ...
- TImageList 和 TlistView 组件(C++Builder)
__fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { //加载图标到Imagelist Graphics::TBitmap *bm ...
- zjuoj 3600 Taxi Fare
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3600 Taxi Fare Time Limit: 2 Seconds ...
- Linux 硬盘分区
Linux系统中的重要概念,一切资源都看做是文件,包括硬件设备. 1. 基本概念 1)MBR:Master Boot Recorder,存放主引导记录,446字节的引导代码. 2)主分区表:存放主分区 ...
- docker gitlab
Alternatively, you can manually launch the gitlab container and the supporting postgresql and redis ...
- App架构设计学习(一)---- 接口的设计
一.哎,最近换了家工作,结果工作很出的我意外,没有干熟悉的根据需求写代码,反而让我一个小菜鸟去重构一下App的架构(他们公司的app,已经上线了1.0版本了),没办法,只有硬着头皮去先学习学习,再总结 ...
- WM (Constants)
Create page WM (Constants) Summary WM_* Constants and their definitions or descriptions and what c ...