Home All Groups Group Topic Archive Search About

Can anyone help in shortening this loop???

Author
11 Aug 2006 2:15 PM
kberry
I am clearing Textboxes on a form... this is loop I have came up with
but was wondering if it can be shorter or not as long...  Can anyone
help?

        Dim controlOnForm As Control    'Places a control on the form
        Dim controlOnTab As Control     'Places a control on the tab
        Dim controlTabPage As Control   'Places a control on the tab
page
        Dim controlGroupBox As Control  'Places a control on the group
box
        For Each controlOnForm In Me.Controls   'Focus on the form
            For Each controlOnTab In controlOnForm.Controls 'Focus on
the Tab
                For Each controlTabPage In controlOnTab.Controls 'Focus

on the Tab Page
                    If TypeOf controlTabPage Is TextBox Then 'Focus on
the Textboxes
                        controlTabPage.Text = "" 'Clear Textbox
                    End If
                    If TypeOf controlTabPage Is ComboBox Then 'Focus on

the ComboBox
                        controlTabPage.Text = "" 'Clear ComboBox
                    End If
                    For Each controlGroupBox In controlTabPage.Controls

'Focus on the GroupBox
                        If TypeOf controlGroupBox Is TextBox Then
'Focus on the Textbox
                            controlGroupBox.Text = "" 'Clear Textbox
                        End If
                    Next
                Next
            Next
        Next


Thought it would be easier in .NET than in VB6...

Author
11 Aug 2006 2:38 PM
Cor Ligthert [MVP]
Kberry,

You mean all textboxes as you showed on a form?

\\\
Private Sub Form5_Load(ByVal sender As Object, _
    ByVal e As System.EventArgs) Handles MyBase.Load
        doset(Me)
    End Sub
    Private Sub doSet(ByVal parentCtr As Control)
      For Each ctr as Control In parentCtr.Controls
            If TypeOf ctr = Textbox orelse TypeOf ctr = Combobox _
                   orelse Typeof ctr = ControlGroupBox then
                   ctr.text = ""
            End if
            doSet(ctr)
        Next
    End Sub
///

I hope this helps,

Cor
<kbe***@processbarron.com> schreef in bericht
Show quoteHide quote
news:1155305741.792804.222660@74g2000cwt.googlegroups.com...
>I am clearing Textboxes on a form... this is loop I have came up with
> but was wondering if it can be shorter or not as long...  Can anyone
> help?
>
>        Dim controlOnForm As Control    'Places a control on the form
>        Dim controlOnTab As Control     'Places a control on the tab
>        Dim controlTabPage As Control   'Places a control on the tab
> page
>        Dim controlGroupBox As Control  'Places a control on the group
> box
>        For Each controlOnForm In Me.Controls   'Focus on the form
>            For Each controlOnTab In controlOnForm.Controls 'Focus on
> the Tab
>                For Each controlTabPage In controlOnTab.Controls 'Focus
>
> on the Tab Page
>                    If TypeOf controlTabPage Is TextBox Then 'Focus on
> the Textboxes
>                        controlTabPage.Text = "" 'Clear Textbox
>                    End If
>                    If TypeOf controlTabPage Is ComboBox Then 'Focus on
>
> the ComboBox
>                        controlTabPage.Text = "" 'Clear ComboBox
>                    End If
>                    For Each controlGroupBox In controlTabPage.Controls
>
> 'Focus on the GroupBox
>                        If TypeOf controlGroupBox Is TextBox Then
> 'Focus on the Textbox
>                            controlGroupBox.Text = "" 'Clear Textbox
>                        End If
>                    Next
>                Next
>            Next
>        Next
>
>
> Thought it would be easier in .NET than in VB6...
>
Author
11 Aug 2006 6:20 PM
tomb
An eloquent example of programming, Cor!

T


Cor Ligthert [MVP] wrote:

Show quoteHide quote
>Kberry,
>
>You mean all textboxes as you showed on a form?
>
>\\\
>Private Sub Form5_Load(ByVal sender As Object, _
>    ByVal e As System.EventArgs) Handles MyBase.Load
>        doset(Me)
>    End Sub
>    Private Sub doSet(ByVal parentCtr As Control)
>      For Each ctr as Control In parentCtr.Controls
>            If TypeOf ctr = Textbox orelse TypeOf ctr = Combobox _
>                   orelse Typeof ctr = ControlGroupBox then
>                   ctr.text = ""
>            End if
>            doSet(ctr)
>        Next
>    End Sub
>///
>
>I hope this helps,
>
>Cor
><kbe***@processbarron.com> schreef in bericht
>news:1155305741.792804.222660@74g2000cwt.googlegroups.com...

>
>>I am clearing Textboxes on a form... this is loop I have came up with
>>but was wondering if it can be shorter or not as long...  Can anyone
>>help?
>>
>>       Dim controlOnForm As Control    'Places a control on the form
>>       Dim controlOnTab As Control     'Places a control on the tab
>>       Dim controlTabPage As Control   'Places a control on the tab
>>page
>>       Dim controlGroupBox As Control  'Places a control on the group
>>box
>>       For Each controlOnForm In Me.Controls   'Focus on the form
>>           For Each controlOnTab In controlOnForm.Controls 'Focus on
>>the Tab
>>               For Each controlTabPage In controlOnTab.Controls 'Focus
>>
>>on the Tab Page
>>                   If TypeOf controlTabPage Is TextBox Then 'Focus on
>>the Textboxes
>>                       controlTabPage.Text = "" 'Clear Textbox
>>                   End If
>>                   If TypeOf controlTabPage Is ComboBox Then 'Focus on
>>
>>the ComboBox
>>                       controlTabPage.Text = "" 'Clear ComboBox
>>                   End If
>>                   For Each controlGroupBox In controlTabPage.Controls
>>
>>'Focus on the GroupBox
>>                       If TypeOf controlGroupBox Is TextBox Then
>>'Focus on the Textbox
>>                           controlGroupBox.Text = "" 'Clear Textbox
>>                       End If
>>                   Next
>>               Next
>>           Next
>>       Next
>>
>>
>>Thought it would be easier in .NET than in VB6...
>>
>>   
>>
>
>

>
Author
13 Aug 2006 2:04 AM
Jay B. Harlow [MVP - Outlook]
Cor,
About the only change I normally include is to check to see if the control
has children before I do the recursive call:

|    Private Sub doSet(ByVal parentCtr As Control)
|      For Each ctr as Control In parentCtr.Controls
|            If TypeOf ctr = Textbox orelse TypeOf ctr = Combobox _
|                   orelse Typeof ctr = ControlGroupBox then
|                   ctr.text = ""
|            End if
            If ctr.HasChildren Then
|            doSet(ctr)
            End If
|        Next
|    End Sub



--
Hope this helps
Jay B. Harlow [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


Show quoteHide quote
"Cor Ligthert [MVP]" <notmyfirstn***@planet.nl> wrote in message
news:ed5nVOVvGHA.560@TK2MSFTNGP05.phx.gbl...
| Kberry,
|
| You mean all textboxes as you showed on a form?
|
| \\\
| Private Sub Form5_Load(ByVal sender As Object, _
|    ByVal e As System.EventArgs) Handles MyBase.Load
|        doset(Me)
|    End Sub
|    Private Sub doSet(ByVal parentCtr As Control)
|      For Each ctr as Control In parentCtr.Controls
|            If TypeOf ctr = Textbox orelse TypeOf ctr = Combobox _
|                   orelse Typeof ctr = ControlGroupBox then
|                   ctr.text = ""
|            End if
|            doSet(ctr)
|        Next
|    End Sub
| ///
|
| I hope this helps,
|
| Cor
| <kbe***@processbarron.com> schreef in bericht
| news:1155305741.792804.222660@74g2000cwt.googlegroups.com...
| >I am clearing Textboxes on a form... this is loop I have came up with
| > but was wondering if it can be shorter or not as long...  Can anyone
| > help?
| >
| >        Dim controlOnForm As Control    'Places a control on the form
| >        Dim controlOnTab As Control     'Places a control on the tab
| >        Dim controlTabPage As Control   'Places a control on the tab
| > page
| >        Dim controlGroupBox As Control  'Places a control on the group
| > box
| >        For Each controlOnForm In Me.Controls   'Focus on the form
| >            For Each controlOnTab In controlOnForm.Controls 'Focus on
| > the Tab
| >                For Each controlTabPage In controlOnTab.Controls 'Focus
| >
| > on the Tab Page
| >                    If TypeOf controlTabPage Is TextBox Then 'Focus on
| > the Textboxes
| >                        controlTabPage.Text = "" 'Clear Textbox
| >                    End If
| >                    If TypeOf controlTabPage Is ComboBox Then 'Focus on
| >
| > the ComboBox
| >                        controlTabPage.Text = "" 'Clear ComboBox
| >                    End If
| >                    For Each controlGroupBox In controlTabPage.Controls
| >
| > 'Focus on the GroupBox
| >                        If TypeOf controlGroupBox Is TextBox Then
| > 'Focus on the Textbox
| >                            controlGroupBox.Text = "" 'Clear Textbox
| >                        End If
| >                    Next
| >                Next
| >            Next
| >        Next
| >
| >
| > Thought it would be easier in .NET than in VB6...
| >
|
|
Author
13 Aug 2006 5:48 AM
Cor Ligthert [MVP]
Jay,

I am in doubt in this what I should use.

I like to avoid code which is done in the recursive call by the for each ctr
as the control has no children, on the other hand do I not know what time is
taken by building the stack and destroy it again.

Maybe I will make a test for this in short future.

Cor

Show quoteHide quote
"Jay B. Harlow [MVP - Outlook]" <Jay_Harlow_***@tsbradley.net> schreef in
bericht news:uh5pPznvGHA.1288@TK2MSFTNGP02.phx.gbl...
> Cor,
> About the only change I normally include is to check to see if the control
> has children before I do the recursive call:
>
> |    Private Sub doSet(ByVal parentCtr As Control)
> |      For Each ctr as Control In parentCtr.Controls
> |            If TypeOf ctr = Textbox orelse TypeOf ctr = Combobox _
> |                   orelse Typeof ctr = ControlGroupBox then
> |                   ctr.text = ""
> |            End if
>            If ctr.HasChildren Then
> |            doSet(ctr)
>            End If
> |        Next
> |    End Sub
>
>
>
> --
> Hope this helps
> Jay B. Harlow [MVP - Outlook]
> .NET Application Architect, Enthusiast, & Evangelist
> T.S. Bradley - http://www.tsbradley.net
>
>
> "Cor Ligthert [MVP]" <notmyfirstn***@planet.nl> wrote in message
> news:ed5nVOVvGHA.560@TK2MSFTNGP05.phx.gbl...
> | Kberry,
> |
> | You mean all textboxes as you showed on a form?
> |
> | \\\
> | Private Sub Form5_Load(ByVal sender As Object, _
> |    ByVal e As System.EventArgs) Handles MyBase.Load
> |        doset(Me)
> |    End Sub
> |    Private Sub doSet(ByVal parentCtr As Control)
> |      For Each ctr as Control In parentCtr.Controls
> |            If TypeOf ctr = Textbox orelse TypeOf ctr = Combobox _
> |                   orelse Typeof ctr = ControlGroupBox then
> |                   ctr.text = ""
> |            End if
> |            doSet(ctr)
> |        Next
> |    End Sub
> | ///
> |
> | I hope this helps,
> |
> | Cor
> | <kbe***@processbarron.com> schreef in bericht
> | news:1155305741.792804.222660@74g2000cwt.googlegroups.com...
> | >I am clearing Textboxes on a form... this is loop I have came up with
> | > but was wondering if it can be shorter or not as long...  Can anyone
> | > help?
> | >
> | >        Dim controlOnForm As Control    'Places a control on the form
> | >        Dim controlOnTab As Control     'Places a control on the tab
> | >        Dim controlTabPage As Control   'Places a control on the tab
> | > page
> | >        Dim controlGroupBox As Control  'Places a control on the group
> | > box
> | >        For Each controlOnForm In Me.Controls   'Focus on the form
> | >            For Each controlOnTab In controlOnForm.Controls 'Focus on
> | > the Tab
> | >                For Each controlTabPage In controlOnTab.Controls 'Focus
> | >
> | > on the Tab Page
> | >                    If TypeOf controlTabPage Is TextBox Then 'Focus on
> | > the Textboxes
> | >                        controlTabPage.Text = "" 'Clear Textbox
> | >                    End If
> | >                    If TypeOf controlTabPage Is ComboBox Then 'Focus on
> | >
> | > the ComboBox
> | >                        controlTabPage.Text = "" 'Clear ComboBox
> | >                    End If
> | >                    For Each controlGroupBox In controlTabPage.Controls
> | >
> | > 'Focus on the GroupBox
> | >                        If TypeOf controlGroupBox Is TextBox Then
> | > 'Focus on the Textbox
> | >                            controlGroupBox.Text = "" 'Clear Textbox
> | >                        End If
> | >                    Next
> | >                Next
> | >            Next
> | >        Next
> | >
> | >
> | > Thought it would be easier in .NET than in VB6...
> | >
> |
> |
>
>
Author
13 Aug 2006 2:19 PM
Jay B. Harlow [MVP - Outlook]
Cor,
| I not know what time is
| taken by building the stack and destroy it again.
|
| Maybe I will make a test for this in short future.
The sample given was not intended as a performance preference, rather as an
alternative way of doing it.

I have also seen/used:

| > |    Private Sub doSet(ByVal parentCtr As Control)
            If parentCtr.HasChildren Then
| > |        For Each ctr as Control In parentCtr.Controls
| > |            If TypeOf ctr = Textbox orelse TypeOf ctr = Combobox _
| > |                   orelse Typeof ctr = ControlGroupBox then
| > |                   ctr.text = ""
| > |            End if
| > |        Next
           End If
| > |    End Sub

The first example, as you mention, avoids a recursive call.
The second example avoids creating an Enumerator, creating an Enumerator may
add to memory pressure...

Based on the 80/20 rule both may be pre-mature optimizations...

--
Hope this helps
Jay B. Harlow [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


Show quoteHide quote
"Cor Ligthert [MVP]" <notmyfirstn***@planet.nl> wrote in message
news:%23uuklvpvGHA.4624@TK2MSFTNGP02.phx.gbl...
| Jay,
|
| I am in doubt in this what I should use.
|
| I like to avoid code which is done in the recursive call by the for each
ctr
| as the control has no children, on the other hand do I not know what time
is
| taken by building the stack and destroy it again.
|
| Maybe I will make a test for this in short future.
|
| Cor
|
| "Jay B. Harlow [MVP - Outlook]" <Jay_Harlow_***@tsbradley.net> schreef in
| bericht news:uh5pPznvGHA.1288@TK2MSFTNGP02.phx.gbl...
| > Cor,
| > About the only change I normally include is to check to see if the
control
| > has children before I do the recursive call:
| >
| > |    Private Sub doSet(ByVal parentCtr As Control)
| > |      For Each ctr as Control In parentCtr.Controls
| > |            If TypeOf ctr = Textbox orelse TypeOf ctr = Combobox _
| > |                   orelse Typeof ctr = ControlGroupBox then
| > |                   ctr.text = ""
| > |            End if
| >            If ctr.HasChildren Then
| > |            doSet(ctr)
| >            End If
| > |        Next
| > |    End Sub
| >
| >
| >
| > --
| > Hope this helps
| > Jay B. Harlow [MVP - Outlook]
| > .NET Application Architect, Enthusiast, & Evangelist
| > T.S. Bradley - http://www.tsbradley.net
| >
| >
| > "Cor Ligthert [MVP]" <notmyfirstn***@planet.nl> wrote in message
| > news:ed5nVOVvGHA.560@TK2MSFTNGP05.phx.gbl...
| > | Kberry,
| > |
| > | You mean all textboxes as you showed on a form?
| > |
| > | \\\
| > | Private Sub Form5_Load(ByVal sender As Object, _
| > |    ByVal e As System.EventArgs) Handles MyBase.Load
| > |        doset(Me)
| > |    End Sub
| > |    Private Sub doSet(ByVal parentCtr As Control)
| > |      For Each ctr as Control In parentCtr.Controls
| > |            If TypeOf ctr = Textbox orelse TypeOf ctr = Combobox _
| > |                   orelse Typeof ctr = ControlGroupBox then
| > |                   ctr.text = ""
| > |            End if
| > |            doSet(ctr)
| > |        Next
| > |    End Sub
| > | ///
| > |
| > | I hope this helps,
| > |
| > | Cor
| > | <kbe***@processbarron.com> schreef in bericht
| > | news:1155305741.792804.222660@74g2000cwt.googlegroups.com...
| > | >I am clearing Textboxes on a form... this is loop I have came up with
| > | > but was wondering if it can be shorter or not as long...  Can anyone
| > | > help?
| > | >
| > | >        Dim controlOnForm As Control    'Places a control on the form
| > | >        Dim controlOnTab As Control     'Places a control on the tab
| > | >        Dim controlTabPage As Control   'Places a control on the tab
| > | > page
| > | >        Dim controlGroupBox As Control  'Places a control on the
group
| > | > box
| > | >        For Each controlOnForm In Me.Controls   'Focus on the form
| > | >            For Each controlOnTab In controlOnForm.Controls 'Focus on
| > | > the Tab
| > | >                For Each controlTabPage In controlOnTab.Controls
'Focus
| > | >
| > | > on the Tab Page
| > | >                    If TypeOf controlTabPage Is TextBox Then 'Focus
on
| > | > the Textboxes
| > | >                        controlTabPage.Text = "" 'Clear Textbox
| > | >                    End If
| > | >                    If TypeOf controlTabPage Is ComboBox Then 'Focus
on
| > | >
| > | > the ComboBox
| > | >                        controlTabPage.Text = "" 'Clear ComboBox
| > | >                    End If
| > | >                    For Each controlGroupBox In
controlTabPage.Controls
| > | >
| > | > 'Focus on the GroupBox
| > | >                        If TypeOf controlGroupBox Is TextBox Then
| > | > 'Focus on the Textbox
| > | >                            controlGroupBox.Text = "" 'Clear Textbox
| > | >                        End If
| > | >                    Next
| > | >                Next
| > | >            Next
| > | >        Next
| > | >
| > | >
| > | > Thought it would be easier in .NET than in VB6...
| > | >
| > |
| > |
| >
| >
|
|
Author
14 Aug 2006 1:50 PM
Brian Tkatch
kbe***@processbarron.com wrote:
Show quoteHide quote
> I am clearing Textboxes on a form... this is loop I have came up with
> but was wondering if it can be shorter or not as long...  Can anyone
> help?
>
>         Dim controlOnForm As Control    'Places a control on the form
>         Dim controlOnTab As Control     'Places a control on the tab
>         Dim controlTabPage As Control   'Places a control on the tab
> page
>         Dim controlGroupBox As Control  'Places a control on the group
> box
>         For Each controlOnForm In Me.Controls   'Focus on the form
>             For Each controlOnTab In controlOnForm.Controls 'Focus on
> the Tab
>                 For Each controlTabPage In controlOnTab.Controls 'Focus
>
> on the Tab Page
>                     If TypeOf controlTabPage Is TextBox Then 'Focus on
> the Textboxes
>                         controlTabPage.Text = "" 'Clear Textbox
>                     End If
>                     If TypeOf controlTabPage Is ComboBox Then 'Focus on
>
> the ComboBox
>                         controlTabPage.Text = "" 'Clear ComboBox
>                     End If
>                     For Each controlGroupBox In controlTabPage.Controls
>
> 'Focus on the GroupBox
>                         If TypeOf controlGroupBox Is TextBox Then
> 'Focus on the Textbox
>                             controlGroupBox.Text = "" 'Clear Textbox
>                         End If
>                     Next
>                 Next
>             Next
>         Next
>
>
> Thought it would be easier in .NET than in VB6...

I don't know if this is possible in your case, but here's something i
just implemented.

Bind all the textboxes to different DataColumns in a DataTable.  To
clear the text, do a DataTable.Clear.

Works like a charm.

B.