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. C#
  4. Removing

Removing

Scheduled Pinned Locked Moved C#
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.
  • M Offline
    M Offline
    myNameIsRon
    wrote on last edited by
    #1

    Hi, I need to delete all the radiobuttons in a panel (other controls are in panel as well). This code I came up with only seems to get ride of some, but not all :o( foreach (Control control in panel_Label.Controls) { if (control is RadioButton) { control.Dispose(); } } thanks, Ron

    S T 2 Replies Last reply
    0
    • M myNameIsRon

      Hi, I need to delete all the radiobuttons in a panel (other controls are in panel as well). This code I came up with only seems to get ride of some, but not all :o( foreach (Control control in panel_Label.Controls) { if (control is RadioButton) { control.Dispose(); } } thanks, Ron

      S Offline
      S Offline
      Stefan Troschuetz
      wrote on last edited by
      #2

      Probably some of the radio buttons aren't direct child controls of the panel, but childs of childs (of childs ..) of the panel.?


      "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook

      www.troschuetz.de

      M 1 Reply Last reply
      0
      • M myNameIsRon

        Hi, I need to delete all the radiobuttons in a panel (other controls are in panel as well). This code I came up with only seems to get ride of some, but not all :o( foreach (Control control in panel_Label.Controls) { if (control is RadioButton) { control.Dispose(); } } thanks, Ron

        T Offline
        T Offline
        Tuwing Sabado
        wrote on last edited by
        #3

        try this call the method on pageload RemoveRadioButtons(MyPanel); here's the method... public void RemoveRadioButtons(Control parentControl) { foreach(Control childControl in parentControl.Controls) { if(childControl.HasControl()) { //Call again the RemoveRadioButtons method to iterate to childcontrols RemoveRadioButtons(childControl); } elseif(childControl is RadioButton) { parentControl.Controls.Remove(childControl); } } } Regards, Mark

        1 Reply Last reply
        0
        • S Stefan Troschuetz

          Probably some of the radio buttons aren't direct child controls of the panel, but childs of childs (of childs ..) of the panel.?


          "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook

          www.troschuetz.de

          M Offline
          M Offline
          myNameIsRon
          wrote on last edited by
          #4

          Hi Rick, All radio buttons are added at run time with this code (to the same panel): RadioButton newPointer = new RadioButton(); newPointer.Size = new System.Drawing.Size(78, 16); newPointer.CheckAlign = ContentAlignment.TopLeft; newPointer.TextAlign = ContentAlignment.TopLeft; newPointer.FlatStyle = FlatStyle.Flat; newPointer.Text = pointerName; panel_Label.Controls.Add(newPointer); thanks, Ron

          S 1 Reply Last reply
          0
          • M myNameIsRon

            Hi Rick, All radio buttons are added at run time with this code (to the same panel): RadioButton newPointer = new RadioButton(); newPointer.Size = new System.Drawing.Size(78, 16); newPointer.CheckAlign = ContentAlignment.TopLeft; newPointer.TextAlign = ContentAlignment.TopLeft; newPointer.FlatStyle = FlatStyle.Flat; newPointer.Text = pointerName; panel_Label.Controls.Add(newPointer); thanks, Ron

            S Offline
            S Offline
            Stefan Troschuetz
            wrote on last edited by
            #5

            myNameIsRon wrote:

            Hi Rick

            I'm not Rick. It's the person I cite in my signature.

            myNameIsRon wrote:

            All radio buttons are added at run time with this code (to the same panel):

            OK, so no recursion or iteration of additional controls is needed. Try to explicitely remove the radio button from the panel before disposing it.

            foreach (Control control in panel_Label.Controls)
            {
            if (control is RadioButton)
            {
            panel_Label.Controls.Remove(control);
            control.Dispose();
            }
            }


            "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook

            www.troschuetz.de

            M 1 Reply Last reply
            0
            • S Stefan Troschuetz

              myNameIsRon wrote:

              Hi Rick

              I'm not Rick. It's the person I cite in my signature.

              myNameIsRon wrote:

              All radio buttons are added at run time with this code (to the same panel):

              OK, so no recursion or iteration of additional controls is needed. Try to explicitely remove the radio button from the panel before disposing it.

              foreach (Control control in panel_Label.Controls)
              {
              if (control is RadioButton)
              {
              panel_Label.Controls.Remove(control);
              control.Dispose();
              }
              }


              "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook

              www.troschuetz.de

              M Offline
              M Offline
              myNameIsRon
              wrote on last edited by
              #6

              Hi, Thanks for your help. I tried your code, but same thing is happening. If I change anthing else, it works fine: foreach (Control control in panel_Label.Controls) { if (control is RadioButton) { control.Text = "New Text"; } } Ron

              S 1 Reply Last reply
              0
              • M myNameIsRon

                Hi, Thanks for your help. I tried your code, but same thing is happening. If I change anthing else, it works fine: foreach (Control control in panel_Label.Controls) { if (control is RadioButton) { control.Text = "New Text"; } } Ron

                S Offline
                S Offline
                Stefan Troschuetz
                wrote on last edited by
                #7

                Errh, should have seen it immediately. The problem is the foreach loop that prevents you from changing the iterated collection. Change it to a for loop and it should work.


                "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook

                www.troschuetz.de

                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