Leticia Esperon Leticia Esperon - 29 days ago 21
C# Question

How can I prevent a button from getting resized when adding user control to form?

I have a tab control and a user control with a button. When I add the user control to the tab with this code:

ProductImporters uc = new ProductImporters();
uc.Dock = DockStyle.Fill;
tabProducts.Controls.Add(uc);
uc.Show();
uc.BringToFront();


The button grows a lot. I've already tried setting the button with a max-size, auto-size in false, I tried fixing and locking it, nothing works.

The user control is the same size of the tab where it is going to be put in.

Any ideas are appreciated, thank you.

Answer

Instead of DockStyle.Fill use DockStyle.None. Use Anchoring to fix the position of the button. For example, anchor to right and bottom to fix it on the bottom right position. Set Autosize property of the button to false. This will avoid resize.