foreach(Control c in groupBox.Controls()) groupBox.Controls.Remove(c);
Alternatively you can use groupBox.Controls.Clear()
سؤال
I want to clear the content of a GroupBox to fill it with new options, but I cant use the Dispose() function since I don't want to destroy the objects in it (Textboxes from another class). Is there a way to empty a GroupBox without destroying the content?
EDIT: Since its difficult to understand what exactly I want, here is what I tried to do (this is IronPython btw, but that shouldnt be a problem)
def loadMethod(self, sender, e):
self.MethodParaBox.Visible = True
for i in self.MethodParaBox.Controls:
self.MethodParaBox.Controls[0].Dispose()
panel = Panel()
panel.AutoScroll = True
panel.Width = 150
panel.Height = 130
panel.Left = 25
panel.Top = 25
self.MethodParaBox.Controls.Add(panel)
for i, z in enumerate(self.parent.getParameter(self.project.SelectedItem, self.design.SelectedItem)):
if self.textBoxList[i].value.Enabled == False:
label = Label()
label.Left = 0
label.Top = 30 * i
label.Width = 80
label.Text = z
panel.Controls.Add(label)
self.textBoxList[i].option3.Left = 90
self.textBoxList[i].option3.Top = 0 + 30 * i
self.textBoxList[i].option3.Width = 40
panel.Controls.Add(self.textBoxList[i].option3)
if i call this function a second time it will throw an exception because the textbox no longer exists
المحلول
foreach(Control c in groupBox.Controls()) groupBox.Controls.Remove(c);
Alternatively you can use groupBox.Controls.Clear()
نصائح أخرى
you can use Control.ControlCollection.Clear
Method
groupBox.Controls.Clear()
Or Control.ControlCollection.RemoveAt
Method
While groupBox.Controls.Count > 0
groupBox.Controls.RemoveAt(0)
End While
Or by using For loop with Remove method
For i As Integer = (groupBox.Controls.Count - 1) To 0 Step -1
Dim ctrl As Control = groupBox.Controls(i)
groupBox.Controls.Remove(ctrl)
ctrl.Dispose()
Next i
You can simply select the groupbox contents in the design and move them away from the gb and then you can delete it safely and move back the contents as you wish.