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. removing controls [modified]

removing controls [modified]

Scheduled Pinned Locked Moved Visual Basic
question
7 Posts 4 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.
  • J Offline
    J Offline
    JR212
    wrote on last edited by
    #1

    Hi, I need to remove all buttons on my form. I use now

    	For n As Integer = Controls.Count - 1 To 0 Step -1
    		Dim c As Control = Controls(n)
    		If TypeOf c Is Button Then
    			Me.Controls.Remove(c)
    		End If
    	Next
    

    but when I use 0 to controls.count or a for each The routine do not remove all the buttons So the question is what is the best way to remove all buttons on a form? not all the controls on the form are buttons the OK and cancel are on a TableLayoutPanel these 2 has to stay Thanks

    modified on Tuesday, May 12, 2009 6:32 AM

    T Z 2 Replies Last reply
    0
    • J JR212

      Hi, I need to remove all buttons on my form. I use now

      	For n As Integer = Controls.Count - 1 To 0 Step -1
      		Dim c As Control = Controls(n)
      		If TypeOf c Is Button Then
      			Me.Controls.Remove(c)
      		End If
      	Next
      

      but when I use 0 to controls.count or a for each The routine do not remove all the buttons So the question is what is the best way to remove all buttons on a form? not all the controls on the form are buttons the OK and cancel are on a TableLayoutPanel these 2 has to stay Thanks

      modified on Tuesday, May 12, 2009 6:32 AM

      T Offline
      T Offline
      Tom Deketelaere
      wrote on last edited by
      #2

      jan212r wrote:

      TableLayoutPanel

      There is your problem

      jan212r wrote:

      Controls.Count

      controls will only return the controls directly on the form You'll have to use recursion and check if the current control is a parent control (groupbox / panel / tablelayoutpanel / ... ) if so call the same function and go over all the controls in that parent control

      1 Reply Last reply
      0
      • J JR212

        Hi, I need to remove all buttons on my form. I use now

        	For n As Integer = Controls.Count - 1 To 0 Step -1
        		Dim c As Control = Controls(n)
        		If TypeOf c Is Button Then
        			Me.Controls.Remove(c)
        		End If
        	Next
        

        but when I use 0 to controls.count or a for each The routine do not remove all the buttons So the question is what is the best way to remove all buttons on a form? not all the controls on the form are buttons the OK and cancel are on a TableLayoutPanel these 2 has to stay Thanks

        modified on Tuesday, May 12, 2009 6:32 AM

        Z Offline
        Z Offline
        Zaegra
        wrote on last edited by
        #3

        For Each Btn As Button In Me.Controls
        Btn.Dispose()
        Next

        This removes all buttons directly on the form, And:

        For Each pan As Panel In Me.Controls
        For Each pButton as Button in pan.Controls
        pButton.Dispose()
        Next
        Next

        Removes all buttons in the panels contained by the form. Etc.. I think you'll get the trick :-\ Cheers, :cool: Zaegra

        Motivation is the key to software development.

        J 1 Reply Last reply
        0
        • Z Zaegra

          For Each Btn As Button In Me.Controls
          Btn.Dispose()
          Next

          This removes all buttons directly on the form, And:

          For Each pan As Panel In Me.Controls
          For Each pButton as Button in pan.Controls
          pButton.Dispose()
          Next
          Next

          Removes all buttons in the panels contained by the form. Etc.. I think you'll get the trick :-\ Cheers, :cool: Zaegra

          Motivation is the key to software development.

          J Offline
          J Offline
          JR212
          wrote on last edited by
          #4

          Thanks for the input. But this gives no awnser to the question. I have an option that works, but when I use for each it do not work. It remove about 25% of the buttons.

          Z 1 Reply Last reply
          0
          • J JR212

            Thanks for the input. But this gives no awnser to the question. I have an option that works, but when I use for each it do not work. It remove about 25% of the buttons.

            Z Offline
            Z Offline
            Zaegra
            wrote on last edited by
            #5

            I think you haven't understood it properly: That is because those buttons are NOT directly on the form, so you'll have to iterate through each container that might contain a button and then remove it. It's as simple as that ;) Cheers, Zaegra

            Motivation is the key to software development.

            J 1 Reply Last reply
            0
            • Z Zaegra

              I think you haven't understood it properly: That is because those buttons are NOT directly on the form, so you'll have to iterate through each container that might contain a button and then remove it. It's as simple as that ;) Cheers, Zaegra

              Motivation is the key to software development.

              J Offline
              J Offline
              JR212
              wrote on last edited by
              #6

              Thats also the reasen why the are on a other control becourse these 2 must stay on the form, but the others 1-60 depending on the status must all been removed. But when there are 12 (+ ok and cancel) on the form there are only 3 buttons that are removed so there stay 9 + ok + cancel Jan

              T 1 Reply Last reply
              0
              • J JR212

                Thats also the reasen why the are on a other control becourse these 2 must stay on the form, but the others 1-60 depending on the status must all been removed. But when there are 12 (+ ok and cancel) on the form there are only 3 buttons that are removed so there stay 9 + ok + cancel Jan

                T Offline
                T Offline
                tiagu
                wrote on last edited by
                #7

                i am trying to remove a DomainUpDown control at runtime.   My coding is as follows and it gives error.             Dim UpDown As System.Windows.Forms.DomainUpDown             For Each UpDown In Me.Controls                   UpDown.Dispose()             Next What is the error and how do I correct it. Someone please help.

                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