Dynamically delete / add controls in a Windows Forms form

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?

+3
source share
2 answers

I would suggest that it would be easier:

  • ..
  • 1 Dock
  • control1 control2 Dock.

.. , .

+4

.. , , , , OP.

, , .

, , , .

, : , , "" ; InitializeComponent , "" . , , .

0

Source: https://habr.com/ru/post/1762878/


All Articles