tags:

views:

52

answers:

2

I want to remove and add a control at runtime on a WinForm. The problem is, that the Control must have the exact same size, location and anchors as another one.

If the user opens the window and a certain criteria is fulfilled, I want to delete the old control and replace it by 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, it didn't work. The InsideBox is not removed and the InsideText not added.

What am I doing wrong? Is there a better approach to this?

A: 

Note: The reason why I suggest an alternative approach instead of directly addressing the original question directly is because I don't see anything wrong with the code that the OP posted.

An alternative approach would be to create both controls in the Forms designer in exactly the same location and only switch their visibility when your criterion is fulfilled.

As was noted in a comment, there is indeed a drawback with this approach, namely that handling the form in the designer gets a little more difficult.

If this turns out to be a problem, you can still use the Designer towards another end: Use the above approach only for seeing what code is necessary to create the "new" control; then remove that code from InitializeComponent and move it to where the "old" control should be replaced by the new one. That way, you can be sure that the code for creating the new control will be correct.

stakx
That makes it a bit of a nightmare when you want to try and edit things in the forms designer.
FrustratedWithFormsDesigner
(I'm not the downvoter) This *might* work for the OP in this case, but it doesn't address the general case of wanting to arbitrarily replace one control with an instance of a new control.
Jon B
_@FrustratedWithFormsDesigner:_ I've addressed your critique of my answer with an edit. In case that you were the downvoter, may I ask you to reconsider your vote? I'm fine with a downvote where it's justified, I only hope it wasn't simply because my answer included the word "Forms Designer", which you seem to dislike... :)
stakx
@stakx: That trick will probably work, but will also probably confuse the form designer, as it involves messing withe generated code. In my experience, this only leads to pain and suffering. If OP doesn't want to use the form designer, then creating two controls in the same location and toggling their visibility is easier, but still seems hackish. Dynamically creating, adding, and removing controls works for me, I'm not really sure what the OP's problem is without seeing the project. And yes, forms designer has frustrated me too, but I would much rather use it than doing all layout via code.
FrustratedWithFormsDesigner
_@Frustrated:_ It's not a "trick"; it's simply inspecting Designer-generated code for a specific control and duplicating that code somewhere else. If you don't want to meddle with the Designer's code generation, simply inspect its code for the control you want to create manually, copy that code to somewhere else, and remove the control, again inside the Designer. There's not much that can go wrong this way.
stakx
@stakx: Ok that's a bit more clear then, but... "There's not much that can go wrong this way." I have found that's not always true ;)
FrustratedWithFormsDesigner
_@Frustrated:_ Unfortunately (for all of us), you might have a point there... :)
stakx
+2  A: 

I'd suggest that the easier way would be to add a panel in the correct location with the correct anchor etc set, and then you add control1 inside that panel set to full Dock and then you can just remove control1 and add control2 inside that panel instead and set it to Dock and all the size stuff etc is done by the one Panel instead of having to copy that around.

ho1