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. Looping through a form...

Looping through a form...

Scheduled Pinned Locked Moved Visual Basic
helpquestion
7 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.
  • D Offline
    D Offline
    DaveFish
    wrote on last edited by
    #1

    I'm attempting to loop through the controls on a windows form, but get stuck trying to access control within a groupbox. here's a copy of the code so far Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click Dim Ctrl As Control Dim Grp As Control For Each Ctrl In Controls MsgBox(Ctrl.Name) Next For Each Grp In GroupBox1 (This is where I get stuck) I cannot find the correct syntax or code to access the Groupbox itself. In total I have 2 buttons, 6 comboboxes, and 4 checkboxes on the form. Any ideas out there?? :( DPF

    J J 2 Replies Last reply
    0
    • D DaveFish

      I'm attempting to loop through the controls on a windows form, but get stuck trying to access control within a groupbox. here's a copy of the code so far Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click Dim Ctrl As Control Dim Grp As Control For Each Ctrl In Controls MsgBox(Ctrl.Name) Next For Each Grp In GroupBox1 (This is where I get stuck) I cannot find the correct syntax or code to access the Groupbox itself. In total I have 2 buttons, 6 comboboxes, and 4 checkboxes on the form. Any ideas out there?? :( DPF

      J Offline
      J Offline
      Just Greeky Creek
      wrote on last edited by
      #2

      try this Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click Dim Ctrl As Control Dim Grp As Control For Each Ctrl In me.Controls Messagebox.show (Ctrl.Name) Next For Each Grp In GroupBox1**.Controls** (This is where YOU get stuck) 'CODE NEXT Note: use Messagebox.show instead of Messagebox

      1 Reply Last reply
      0
      • D DaveFish

        I'm attempting to loop through the controls on a windows form, but get stuck trying to access control within a groupbox. here's a copy of the code so far Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click Dim Ctrl As Control Dim Grp As Control For Each Ctrl In Controls MsgBox(Ctrl.Name) Next For Each Grp In GroupBox1 (This is where I get stuck) I cannot find the correct syntax or code to access the Groupbox itself. In total I have 2 buttons, 6 comboboxes, and 4 checkboxes on the form. Any ideas out there?? :( DPF

        J Offline
        J Offline
        Jim Matthews
        wrote on last edited by
        #3

        i've never actually had to do this myself, but something like this should work:

        Private Sub Button1_Click(Byval sender as Object, byval e as System.EventArgs) handles Button1.Click
        
        for each currentControl as control in me.controls
        
           messagebox.show(currentControl.Name)
        
           if ((typeof currentcontrol is groupbox) orelse (typeof currentcontrol is panel)) then
        
              for each subControl as control in directcast(currentcontrol, groupbox).controls
        
                messagebox.show(subcontrol.Name)  
        
              next
        
           end if
        
        next
        
        end sub
        

        by the way, just as a general rule, try to avoid the calls to legacy vb functions such as MsgBox as much as you can. most everything you can do (i can't think of anything offhand that can't be) with the legacy calls has been replaced by managed class functionality. i.e. Use the system.windows.forms.messagebox class instead of MsgBox function, and String.Substring instead of Instr etc. hope this helps.


        -jim

        D 1 Reply Last reply
        0
        • J Jim Matthews

          i've never actually had to do this myself, but something like this should work:

          Private Sub Button1_Click(Byval sender as Object, byval e as System.EventArgs) handles Button1.Click
          
          for each currentControl as control in me.controls
          
             messagebox.show(currentControl.Name)
          
             if ((typeof currentcontrol is groupbox) orelse (typeof currentcontrol is panel)) then
          
                for each subControl as control in directcast(currentcontrol, groupbox).controls
          
                  messagebox.show(subcontrol.Name)  
          
                next
          
             end if
          
          next
          
          end sub
          

          by the way, just as a general rule, try to avoid the calls to legacy vb functions such as MsgBox as much as you can. most everything you can do (i can't think of anything offhand that can't be) with the legacy calls has been replaced by managed class functionality. i.e. Use the system.windows.forms.messagebox class instead of MsgBox function, and String.Substring instead of Instr etc. hope this helps.


          -jim

          D Offline
          D Offline
          DaveFish
          wrote on last edited by
          #4

          Thanks guy's, both sets of code work in my app and it does give me a clue of where to go next. My next task from here is to use the data from the text boxes and populate an array with the label text in the first column and the data in the second. Is this the best way to approach this, or is it a long route to a short journey?? DPF

          J 1 Reply Last reply
          0
          • D DaveFish

            Thanks guy's, both sets of code work in my app and it does give me a clue of where to go next. My next task from here is to use the data from the text boxes and populate an array with the label text in the first column and the data in the second. Is this the best way to approach this, or is it a long route to a short journey?? DPF

            J Offline
            J Offline
            Jim Matthews
            wrote on last edited by
            #5

            i guess it depends on what you eventually want to do with that data. are you going to be saving it to a database or serialize it to an xml file? if either of the above, i'd recommend looking at an introduction to windows forms databinding and using that. this would allow you to use the controls up front to display the data and have a single structure on the backend (dataset/datatable) that hold the data values. then you don't have to worry about looping through the controls at all. if you need to have the data in an array structure of some sort you can do a conversion from the datatable, to your particular type of array, but the dataset/datatable structures are very flexible and will allow you to do anything that you would be able to do with an array. generally, looping through all controls on a form is not something that you would want to do in order to obtain the values held in those controls, but more of something you would do in order to set the state of all of those controls. i.e. enabled, disabled, visible etc.


            -jim

            D 1 Reply Last reply
            0
            • J Jim Matthews

              i guess it depends on what you eventually want to do with that data. are you going to be saving it to a database or serialize it to an xml file? if either of the above, i'd recommend looking at an introduction to windows forms databinding and using that. this would allow you to use the controls up front to display the data and have a single structure on the backend (dataset/datatable) that hold the data values. then you don't have to worry about looping through the controls at all. if you need to have the data in an array structure of some sort you can do a conversion from the datatable, to your particular type of array, but the dataset/datatable structures are very flexible and will allow you to do anything that you would be able to do with an array. generally, looping through all controls on a form is not something that you would want to do in order to obtain the values held in those controls, but more of something you would do in order to set the state of all of those controls. i.e. enabled, disabled, visible etc.


              -jim

              D Offline
              D Offline
              DaveFish
              wrote on last edited by
              #6

              Hmm... more food for thought. As a brief overview, I have six forms, each of which has a number of checks again data loaded in the previous form to refine the selction in the current forms comboboxes. Once the selections are satisfied the data is passed back to an array (One per form) for further processing in modules. If it's assumed all works well, it intend to dump the result and data to an xml file and have the ability to load this back into my program at a later date. I hope that makes sense. I'll read the link above and also on datasets/datatable Thanks so far DPF

              D 1 Reply Last reply
              0
              • D DaveFish

                Hmm... more food for thought. As a brief overview, I have six forms, each of which has a number of checks again data loaded in the previous form to refine the selction in the current forms comboboxes. Once the selections are satisfied the data is passed back to an array (One per form) for further processing in modules. If it's assumed all works well, it intend to dump the result and data to an xml file and have the ability to load this back into my program at a later date. I hope that makes sense. I'll read the link above and also on datasets/datatable Thanks so far DPF

                D Offline
                D Offline
                DaveFish
                wrote on last edited by
                #7

                I've read the link as provided and although this guides you through binding data from a table to a control I need to pass data from controls on multiple forms back to table,dataset or array for processing, so I have many 'forward' and 'backward'passes. Can anyone suggests where I would start with this. ADO loks interetsing, but is this the best approach bearing in mind my app as above.:-D DPF

                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