loop over all textboxes in a form, including those inside a groupbox

后端 未结 4 1274
执笔经年
执笔经年 2020-11-27 20:16

I have several textboxes in a winform, some of them are inside a groupbox. I tried to loop over all textboxes in my form:

For Each c As Control In Me.Controls         


        
相关标签:
4条回答
  • 2020-11-27 20:54

    You'd want to do recursion, for example (pseudocode, since I do not know VB):

    Sub LoopControls (Control Container)
        if (Container has Controls)
            LoopControls(Container)
        For Each c As Control In Container
            if (Control is TextBox)
                // do stuff
    End Sub
    

    You would pass your form (me) to the sub initially, and it will traverse the controls in it looking for ones that contain more controls.

    Also check out this question: VB.NET - Iterating through controls in a container object

    0 讨论(0)
  • 2020-11-27 20:58

    You will need to use recursion. The following is a C# solution using an extension method and goes a little beyond the scope of your question but I just pulled it from our framework.

    static partial class ControlExtensions
    {
        public static void ApplyToMatchingChild(this Control parent, Action<Control> actionToApplyWhenFound, bool keepApplyingForever, params Func<Control, bool>[] matchingChildCriteria)
        {
            ControlEventHandler reapplyEventHandler = null;
            if (keepApplyingForever)
            {
                reapplyEventHandler = (s, e) =>
                {
                    ApplyToMatchingChild(e.Control, actionToApplyWhenFound, keepApplyingForever, matchingChildCriteria);
                };
            }
            SearchForMatchingChildTypes(parent, actionToApplyWhenFound, reapplyEventHandler, matchingChildCriteria);
        }
    
        private static void SearchForMatchingChildTypes(Control control, Action<Control> actionToApplyWhenFound, ControlEventHandler reapplyEventHandler, params Func<Control, bool>[] matchingChildCriteria)
        {
            if (matchingChildCriteria.Any(criteria => criteria(control)))
            {
                actionToApplyWhenFound(control);
            }
    
            if (reapplyEventHandler != null)
            {
                control.ControlAdded += reapplyEventHandler;
            }
    
            if (control.HasChildren)
            {
                foreach (var ctl in control.Controls.Cast<Control>())
                {
                    SearchForMatchingChildTypes(ctl, actionToApplyWhenFound, reapplyEventHandler, matchingChildCriteria);
                }
            }
        }
    }
    

    And to call:

    myControl.ApplyToMatchingChild(c => { /* Do Stuff to c */ return; }, false, c => c is TextBox);
    

    That will apply a function to all child textboxes. You can use the keepApplyingForever parameter to ensure that your function will be applied when child controls are later added. The function will also allow you to specify any number of matching criteria, for example, if the control is also a label or some other criteria.

    We actually use this as a neat way to call our dependency injection container for every UserControl added to our Form.

    I'm sure you shouldn't have much issue converting it to VB.NET too... Also, If you don't want the "keepApplyingForever" functionality, it should be easy enough to strip that out too.

    0 讨论(0)
  • 2020-11-27 20:59

    If you don't care about the order (and I can't imagine a reason why you should) of the controls, you can do this iteratively in the following fashion (its quite straightforward, so I don't think any explanation is necessary). Should be much more efficient than any recursion, especially if you have many nested controls, though I doubt if the performance gain would be apparent.

    Public Function FindAllControlsIterative(ByRef parent As Control) As List(Of TextBox)
        Dim list As New List(Of TextBox)
        Dim ContainerStack As New Stack(Of Control)
        ContainerStack.Push(parent)
        While ContainerStack.Count > 0
            For Each child As Control In ContainerStack.Pop().Controls
                If child.HasChildren Then ContainerStack.Push(child)
                If child.GetType Is GetType(TextBox) Then list.Add(child)
            Next
        End While
        Return list
    End Function
    
    0 讨论(0)
  • 2020-11-27 21:05

    You can use this function, linq might be a more elegant way.

    Dim allTxt As New List(Of Control)
    For Each txt As TextBox In FindControlRecursive(allTxt, Me, GetType(TextBox))
       '....'
    Next
    
    Public Shared Function FindControlRecursive(ByVal list As List(Of Control), ByVal parent As Control, ByVal ctrlType As System.Type) As List(Of Control)
        If parent Is Nothing Then Return list
        If parent.GetType Is ctrlType Then
            list.Add(parent)
        End If
        For Each child As Control In parent.Controls
            FindControlRecursive(list, child, ctrlType)
        Next
        Return list
    End Function
    
    0 讨论(0)
提交回复
热议问题