Control getting resized when adding to FlowLayoutPanel
Hi. I have a weird problem. I have a FLP and I'm adding controls to it. I have four UserControl classes, each is 865 wide. Three of them are fine when I add, but when I add CtrlImage usercontrol it goes to 1298 wide. In trying to nail it down, I've removed all code from the class so it isn't doing anything crazy. It just has a single label and picturebox. I determined when it happens:
Control ctrl = col.GetControl();
MessageBox.Show("1: " + ctrl.Width.ToString());
record.Controls.Add(ctrl);
MessageBox.Show("2: " + ctrl.Width.ToString());
When I run this, I get "1: 865" and "2: 1298". What the heck happens when you add a control to a FLP that would cause it to expand? I tried asking GPT, it suggested I set AutoSize false and Dock to none on ctrl and WrapContents to true on the FLP. None of that made any difference. The only fix is to do ctrl.Width = 865 after I've added it.
I suppose that resolves it, but I really don't like not knowing why that happened and I'm sure this "band aid" is not a real fix. Anyone know what may cause this?
Thanks!
3 Replies
what behavior do you want? float down?
(also why are you using a FLP?)
The behavior I was expecting was just for it to add my UserControl with the data populated, and not adjust the UC's width. I wasn't sure why the width would get fiddled with.
As to why a FLP, I need to add a variable amount of controls to the form and have it scrollable. I wasn't sure there was a better way than a FLP?
if all controls are of the same size tbh even a TableLayoutPanel would do
you just put 1 column, set autoscroll, then you just tlp.Controls.Add(new YourUserControl())