Whats the best way to go through all the controls on a form in vb2005? Im writing a program that can edit a string of bytes based on the information on the form. each control is tagged with the hex address it modifies and values it can be, what is the best way to go through all the controls on a form even those controls embedded in other controls?
什么是在vb2005中完成表单上所有控件的最佳方法?我正在编写一个程序,可以根据表单上的信息编辑一串字节。每个控件都使用它修改的十六进制地址和它可能的值来标记,即使嵌入在其他控件中的控件,通过表单上所有控件的最佳方法是什么?
4 个解决方案
#1
Something like this, passing in the form to start with:
像这样的东西,传递形式开始:
Private Sub DoSomethingToAllControls(ByVal Container As Control)
Dim ctl As Control
For Each ctl In Container.Controls
' Do Something..
' Recursively call this function for any container controls.
If ctl.HasChildren Then
DoSomethingToAllControls(ctl)
End If
Next
End Sub
#2
Get the instance of System.Windows.Forms.Control.ControlCollection (Me.Controls) from the current form. The from there step through the controls in the collection.
从当前表单获取System.Windows.Forms.Control.ControlCollection(Me.Controls)的实例。从那里逐步通过集合中的控件。
#3
This is C#, but should give the idea. The function just recursivly enumerates all controls and you can do with them what ever you want.
这是C#,但应该提出这个想法。该函数只是递归地枚举所有控件,你可以随心所欲地使用它们。
public static IEnumerable<Control> GetControlsRecursive(Control control)
{
yield return control;
foreach (Control directSubcontrol in control.Controls)
{
foreach (Control subcontrol in GetControlsRecursive(directSubcontrol))
{
yield return subcontrol;
}
}
}
The usage would be something like this.
用法就是这样的。
foreach (Control control in GetControlsRecursive(myForm))
{
DoStuff(control);
}
Solution Without the yield return
statement not availiable in VB.NET.
解决方案没有yield return语句在VB.NET中无法使用。
public static IEnumerable<Control> GetControlsRecursive(Control control)
{
List<Control> controls = new List<Control>() { control };
foreach (Control subcontrol in control.Controls)
{
controls.AddRange(GetControlsRecursive(subcontrol));
}
return controls;
}
#4
Private Sub enumerateControls(ByVal controlcontainer As Object)
Dim basec As Control = controlcontainer
If basec.HasChildren Then
For Each itm As Control In basec.Controls
enumerateControls(itm)
Next
End If
If controlcontainer.tag IsNot Nothing Then
run function to determine control type and function
End If
End Sub
#1
Something like this, passing in the form to start with:
像这样的东西,传递形式开始:
Private Sub DoSomethingToAllControls(ByVal Container As Control)
Dim ctl As Control
For Each ctl In Container.Controls
' Do Something..
' Recursively call this function for any container controls.
If ctl.HasChildren Then
DoSomethingToAllControls(ctl)
End If
Next
End Sub
#2
Get the instance of System.Windows.Forms.Control.ControlCollection (Me.Controls) from the current form. The from there step through the controls in the collection.
从当前表单获取System.Windows.Forms.Control.ControlCollection(Me.Controls)的实例。从那里逐步通过集合中的控件。
#3
This is C#, but should give the idea. The function just recursivly enumerates all controls and you can do with them what ever you want.
这是C#,但应该提出这个想法。该函数只是递归地枚举所有控件,你可以随心所欲地使用它们。
public static IEnumerable<Control> GetControlsRecursive(Control control)
{
yield return control;
foreach (Control directSubcontrol in control.Controls)
{
foreach (Control subcontrol in GetControlsRecursive(directSubcontrol))
{
yield return subcontrol;
}
}
}
The usage would be something like this.
用法就是这样的。
foreach (Control control in GetControlsRecursive(myForm))
{
DoStuff(control);
}
Solution Without the yield return
statement not availiable in VB.NET.
解决方案没有yield return语句在VB.NET中无法使用。
public static IEnumerable<Control> GetControlsRecursive(Control control)
{
List<Control> controls = new List<Control>() { control };
foreach (Control subcontrol in control.Controls)
{
controls.AddRange(GetControlsRecursive(subcontrol));
}
return controls;
}
#4
Private Sub enumerateControls(ByVal controlcontainer As Object)
Dim basec As Control = controlcontainer
If basec.HasChildren Then
For Each itm As Control In basec.Controls
enumerateControls(itm)
Next
End If
If controlcontainer.tag IsNot Nothing Then
run function to determine control type and function
End If
End Sub