
本文实例讲述了WinForm遍历窗体所有子控件的方法。分享给大家供大家参考,具体如下:
/// <summary>
/// C# 只遍历控件子控件,不遍历孙控件
///当控件有子控件时,需要用递归的方法遍历,才能全部列出控件上的控件
/// </summary>
/// <typeparam name="T">控件类型</typeparam>
/// <param name="control">要遍历的控件</param>
/// <param name="controlsName">控件名</param>
/// <returns></returns>
public static T GetControl<T>(Control control, string controlsName) where T : Control
{
if (control == null) return null;
Control _control;
for (int i = 0; i < controlControlsCount; i++)
{
_control = controlControls[i];
如果你的控件没有被放在某个容器里,比如groupbox一类的东西里面的话,使用:
foreach(Control c in thisControls)
{
____if( c is TextBox )
_________richtextboxText += cText;
}
应该可以获得想要的效果,如果使用了容器,那记得对每个容器都调用上面的代码,把this换成容器的对象就可以了。
如果你的控件没有被放在某个容器里,比如groupbox一类的东西里面的话,使用:
foreach(Control c in thisControls)
{
____if( c is TextBox )
_________richtextboxText += cText;
}
应该可以获得想要的效果,如果使用了容器,那记得对每个容器都调用上面的代码,把this换成容器的对象就可以了。
把窗体本身或者控件传入这个类(或者做为参数传给这个类的方法),看你的需要了,比如说你在form1里创建的这个类叫myclass,把这个mycalss的构造方法改成
public class myclass{
form1 frm;
public myclass(form1 frm)
{
thisfrm=frm;
}
}
这样你在form1中创建这个myclass的时候就可以是
myclass mc=new myclass(this);
当然也可以直接传窗体或者控件给类中的方法,比如mycalss中有
public void msg(EditText txt)
或者
public void msg(form1 frm)
其它方法也很多,看你需要了。
OptionExplicitPrivateDeclareFunctionFindWindowLibuser32AliasFindWindowA(ByVallpClassNameAsString,ByVallpWindowNameAsString)AsLongPrivateDeclareFunctionGetWindowTextLibuser32AliasGetWindowTextA(ByValhwndAsLong,ByVallpStringAsString,ByValcchAsLong)AsLongPrivateDeclareFunctionGetWindowLibuser32(ByValhwndAsLong,ByValwCmdAsLong)AsLongPrivateDeclareFunctionGetDesktopWindowLibuser32()AsLongPrivateDeclareFunctionGetClassNameLibuser32AliasGetClassNameA(ByValhwndAsLong,ByVallpClassNameAsString,ByValnMaxCountAsLong)AsLongPrivateConstGW_HWNDFIRST=0PrivateConstGW_HWNDNEXT=2PrivateConstGW_CHILD=5PrivateSubCommand1_Click()CallList1_ClickEndSubPrivateSubCommand2_Click()DimhwndAsLongDimsAsString,tAsStringList1Clearhwnd=GetDesktopWindow()s=String(256,Chr(0))GetClassNamehwnd,s,255s=Replace(s,Chr(0),)t=String(256,Chr(0))GetWindowTexthwnd,t,255t=Replace(t,Chr(0),)List1AddItem桌面:&hwnd&类名:&s&标题:&t&vbCrLfhwnd=GetWindow(hwnd,GW_CHILDOrGW_HWNDFIRST)s=String(256,Chr(0))GetClassNamehwnd,s,255s=Replace(s,Chr(0),)t=String(256,Chr(0))GetWindowTexthwnd,t,255t=Replace(t,Chr(0),)List1AddItem窗口:&hwnd&类名:&s&标题:&t&vbCrLfWhilehwnd<0hwnd=GetWindow(hwnd,GW_HWNDNEXT)s=String(256,Chr(0))GetClassNamehwnd,s,255s=Replace(s,Chr(0),)t=String(256,Chr(0))GetWindowTexthwnd,t,255t=Replace(t,Chr(0),)List1AddItem窗口:&hwnd&类名:&s&标题:&t&vbCrLfWendEndSubPrivateSubForm_Load()Command1Caption=获取所有控件Command2Caption=遍历所有窗体EndSubPrivateSubEnumAllHandles(ByValhwndAsLong)DimhnAsLongDimfirsthdAsLongDimsAsString,tAsStringfirsthd=GetWindow(hwnd,GW_CHILD)firsthd=GetWindow(firsthd,GW_HWNDFIRST)hn=firsthdDoWhilehn<0s=String(256,Chr(0))GetClassNamehn,s,255s=Replace(s,Chr(0),)t=String(256,Chr(0))GetWindowTexthn,t,255t=Replace(t,Chr(0),)Text1Text=Text1Text&句柄:&hn&父句柄:&hwnd&类名:&s&标题:&t&vbCrLfTreeView1NodesAddk&hwnd,tvwChild,k&hn,句柄:&hn&类名:&s&标题:&tEnumAllHandleshnhn=GetWindow(hn,GW_HWNDNEXT)Ifhn=firsthdThenExitDoLoopEndSubPrivateSubList1_Click()IfList1ListIndex=-1ThenExitSubTreeView1NodesClearTreeView1NodesAdd,,k&Trim(Str(Val(Mid(List1Text,4)))),List1TextText1Text=EnumAllHandlesVal(Mid(List1Text,4))TreeView1Nodes(k&Trim(Str(Val(Mid(List1Text,4)))))Expanded=TrueEndSub'添加两个按钮一个文本框一个列表框和一个树形图
以上就是关于如何遍历winform窗体中的所有控件全部的内容,包括:如何遍历winform窗体中的所有控件、急!如何遍历一个窗体内的全部控件或对象、在C#winform中如何遍历子窗体中所有的控件等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)