I want to remove and add a runtime control on a Windows Forms form. The problem is that the control must have the same size, location and bindings as the other.
If the user opens a window and certain criteria are met, I want to remove the old control and replace it with another.
So, I tried this:
RichTextBox InsideText = new RichTextBox();
InsideText.Location = InsideBox.Location;
InsideText.Size = InsideBox.Size;
Controls.Remove(InsideBox);
Controls.Add(InsideText);
But, as expected, this did not work. InsideBoxnot deleted, InsideTextnot added.
What am I doing wrong? Is there a better approach to this?
source
share