Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. How to check controls created at runtime in a windows form application?

How to check controls created at runtime in a windows form application?

Scheduled Pinned Locked Moved Visual Basic
helptutorialquestion
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Amer Rehman 0
    wrote on last edited by
    #1

    Hi Here is the code that I've written. Private Function CheckControl(ByVal ctrlName As String) As Boolean             Dim ctrl As Control             Dim blnResult As Boolean             For Each ctrl In Me.Controls                   If ctrl.Name = ctrlName Then                         blnResult = True                   End If             Next             Return blnResult End Function The problem is that it does not give the correct answer i.e. even if the control exists, it returns false. Thanks

    reman

    M F 2 Replies Last reply
    0
    • A Amer Rehman 0

      Hi Here is the code that I've written. Private Function CheckControl(ByVal ctrlName As String) As Boolean             Dim ctrl As Control             Dim blnResult As Boolean             For Each ctrl In Me.Controls                   If ctrl.Name = ctrlName Then                         blnResult = True                   End If             Next             Return blnResult End Function The problem is that it does not give the correct answer i.e. even if the control exists, it returns false. Thanks

      reman

      M Offline
      M Offline
      Mycroft Holmes
      wrote on last edited by
      #2

      Remember a control (panel, groupbox etc) can have a collection of controls, you are only checking the top level of controls on the form. You need to check if a control has controls and call the same procedure from within your foreach loop. Example

              For Each ctrl In Me.Controls
                    If ctrl.Name = ctrlName Then
                          blnResult = True
                    End If
                    CheckControl(ctrl)
              Next
      

      Never underestimate the power of human stupidity RAH

      1 Reply Last reply
      0
      • A Amer Rehman 0

        Hi Here is the code that I've written. Private Function CheckControl(ByVal ctrlName As String) As Boolean             Dim ctrl As Control             Dim blnResult As Boolean             For Each ctrl In Me.Controls                   If ctrl.Name = ctrlName Then                         blnResult = True                   End If             Next             Return blnResult End Function The problem is that it does not give the correct answer i.e. even if the control exists, it returns false. Thanks

        reman

        F Offline
        F Offline
        Fernando Soto
        wrote on last edited by
        #3

        Hi Amer Rehman; If you are using .Net Framework 2.0 or higher you can use the Control.ControlCollection.Find Method to find a control. The second parameter states to search all child controls as well as shown in the code snippet below.

        Private Function CheckControl(ByVal ctrlName As String) As Boolean

        Dim blnResult As Boolean = False
        
        If (Me.Controls.Find(ctrlName, True).Length > 0) Then
            blnResult = True
        End If
        
        Return blnResult
        

        End Function

        Fernando

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups