Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2183
  • Last Modified:

For each ctl In Me.Controls

Hi,

Thanks for clicking

I am trying to scan thru all controls on a form, and then figure out if it's a ( let's say a third party control called System.Windows.Forms.Textbox

Worked out

Dim ctl As System.Windows.Forms.Control

For Each ctl In Me.Controls
      If the control is a textbox then     <<<-------How do I do this here???
          'Run code for Textbox control only
      End if
Next

You help would be appreciated.

0
StanLawrence
Asked:
StanLawrence
  • 6
  • 4
  • 3
  • +2
4 Solutions
 
bruintjeCommented:
Hello StanLawrence,

you need to check it on type
source: http://www.experts-exchange.com/Q_21809966.html
---------
Dim ctl As System.Windows.Forms.Control

For Each ctl In Me.Controls
      If TypeOf ctl Is TextBox  then
          'Run code for Textbox control only
      End if
Next
---------


hope this helps a bit
bruintje
0
 
wtconwayCommented:
bruintje's suggestion should work for you. This is the way I always did it:

Dim ctl as Control
For each ctl in Me.Controls
     Select Case ctl.GetType.ToString()
         Case "System.Windows.Forms.TextBox"
                 'textbox code
         Case "System.Windows.Forms.ComboBox"
                 'combobox code
          Case "System.Windows.Forms.Label"
                 'labels

     End Select
Next ctl

You get the idea.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Be aware that the "Me.Controls" colletion only iterates thru controls directly contained by the form itself.  If you have nested controls, such as in a Panel for example, then you need a recursive routine that descends into each container control as well...
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
wtconwayCommented:
Good point.
0
 
ctm5Commented:
Recursion is necessary, as Idle Mind says. You can do that by expanding on bruintje's code:

Private Sub DoSomething (aControl as System.Windows.Forms.Control)
Dim ctl As System.Windows.Forms.Control

For Each ctl In aControl.Controls
      If TypeOf ctl Is TextBox  then
          'Run code for Textbox control only
      End If
      If ctl.HasChildren Then
           DoSomething(ctl)
      End If
Next

End Sub

You start out by calling DoSomething with the highest level control (the form, usually)

ctm5


     
0
 
StanLawrenceAuthor Commented:
Guys,

Absolutely brilliant, especially the HasChildren issue.

Yes issues if Panels and TabControls (of which most apps will have!)
0
 
wtconwayCommented:
So have you solved the problem?
0
 
StanLawrenceAuthor Commented:
Yes and I forgot to allocate points. Got distracted!

My apologies. Sorry!!

0
 
StanLawrenceAuthor Commented:
So many people input, and there comments had a knock on effect, and if they hadn't mentioned it, i'd still be stuck.

Thanks to all.
0
 
wtconwayCommented:
Glad to see you solved the problem. Take care.
0
 
StanLawrenceAuthor Commented:
guys,

Can I reopen this post briefly?

I've found one flaw!!

If I put the control in question inside a TabControl and one external to it.

The one inside runs fine, the one outside doesn't. It's due to

For Each ctl In aControl.Controls

aControl.Controls has a count of Zero.

Can anyone help, becuase this is really great code!!
0
 
ctm5Commented:
What type is aControl? Can you post your function? Sounds like the recursion needs tweaking.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
It works for me with TabControls...this is what I tested with:

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        FindTextBoxes(Me)
    End Sub

    Private Sub FindTextBoxes(ByVal container As Control)
        For Each c As Control In container.Controls
            If TypeOf c Is TextBox Then
                Debug.WriteLine(c.Name)
            ElseIf c.HasChildren Then
                FindTextBoxes(c)
            End If
        Next
    End Sub
0
 
StanLawrenceAuthor Commented:
Thanks guys

Here the function I've called

PLEASE NOTE : IT's when I put a UIButton (a 3rd party control button) EXTERNAL to the TabControl. The one's that are children work fine! I simply testing with one in Tabcontrol and one outside!

Calling code
Dim ctl As System.Windows.Forms.Control
For Each ctl In Me.Controls
    Call IterateThruControls(ctl)
Next


Private Sub IterateThruControls(ByVal actl As System.Windows.Forms.Control)
        Dim ctl As System.Windows.Forms.Control
       
        For Each ctl In actl.Controls      <<<<---- It skips over here. No controls in actl collection
              If TypeOf ctl Is Janus.Windows.EditControls.UIButton Then
               'Do whatever here!!!
            End If
            If ctl.HasChildren Then
                Call IterateThruControls(ctl, e)
            End If
        Next ctl
    End Sub
0
 
ctm5Commented:
If you start with the tab control, the recursive function will only iterate through the child controls of the tab control.

Theoretically, you would never call IterateThroughControls unless you knew actl had children. If you start with the panel or the form that the 3rd party button is on as actl, then one of the child controls will be the 3rd party button.

ctm5
0
 
StanLawrenceAuthor Commented:
Thanks ctm5.

Got it sorted!!
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 6
  • 4
  • 3
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now