views:

61

answers:

3

What is the proper way to replace one winform element with another element when something is triggered? For example, I would like to replace a button with a text box in the same position and the same dimensions.

+1  A: 

You can put them both there, and play with visability

Dani
A: 

Put them next to each other and use the Visible property to hide one (Visible=false) and display the other (Visible=true).

Nate C-K
+1  A: 

If you do not (for some reason) want to simply change their visibility, you can add and remove them from the form's Controls collection.

// contrived example...
private void Swap( Control toAdd, Control toRemove )
{
    this.Controls.Remove( toRemove );
    this.Controls.Add( toAdd );
}
Ed Swangren