Windows Develop Bookmark and Share   
 index > Windows Forms General > Update UserControl's width inside FlowLayoutPanel automatically
 

Update UserControl's width inside FlowLayoutPanel automatically

Hi,

I have UserControl inside FlowLayoutPanel with FlowDirection=TopDown (control per line, variable height).

I want the UserControl to always have the same width as its container panel. Is there any UserControl\FlowLayoutPanel property I can set once instead of catching the resizing event of the panel and set the UserControl's width each time?

Thanks in advance.
TechMan  Tuesday, March 06, 2007 5:25 PM

hmm. I can't think of another way.

But the event shouldn't be to much of a trial. If you implement a special method in your usercontrol(s)

void FlowControlPanel_Layout(Object sender, LayoutEventArgs e)

{

foreach( Control ctrl in ((Control)sender).Controls)

{

((MyUserControl)ctrl).ResizeInLayoutEvent();

}

}

I think thats the best you are going to hope for unless someone can tell you how to get Dock / Anchor to work

JeroGrav  Tuesday, March 06, 2007 9:54 PM

Normally I would suggest using the Dock or Anchor properties but they don't seem to work for controls in a flowlayoutpanel.

But I did manage to do what you want. Simply wireup an event handler to the FlowLayoutPanel's Layout event. Inside that event, size the UserControl based on the FlowLayoutPanel's size.

Hope that helps.

Jero

JeroGrav  Tuesday, March 06, 2007 9:02 PM
Thanks JeroGrav.
I also thought Docking\Anchoring is the way to go but FlowLayoutPanel behaves differently.

Isn't there a way of doing it without an event handler?
I'm going to have multiple user controls inside this panel (10-20) and each one of them should get the correct size...
TechMan  Tuesday, March 06, 2007 9:13 PM

hmm. I can't think of another way.

But the event shouldn't be to much of a trial. If you implement a special method in your usercontrol(s)

void FlowControlPanel_Layout(Object sender, LayoutEventArgs e)

{

foreach( Control ctrl in ((Control)sender).Controls)

{

((MyUserControl)ctrl).ResizeInLayoutEvent();

}

}

I think thats the best you are going to hope for unless someone can tell you how to get Dock / Anchor to work

JeroGrav  Tuesday, March 06, 2007 9:54 PM

Hi,TechMan

If you just want your controls have the same with,why not try Panel Control?

Gavin Jin - MSFT  Wednesday, March 07, 2007 2:32 AM
Hi Gavin Jin,

I'm adding these user controls at runtime through code and I need the flow order (each one of them will have different height) so I'm not sure simple Panel will help me.
TechMan  Wednesday, March 07, 2007 6:40 AM
Yes JeroGrav,

This was my intention, I think I'll go with this approach.

Thanks.

TechMan  Wednesday, March 07, 2007 6:42 AM

You can use google to search for other answers

Custom Search

More Threads

• Print Vertical Text
• PropertyGrid not catching mouse events
• MDI Merge context menu
• How to Set attribute(invisible) to ListBox Item
• about listbox
• Controls collection
• Run a program in the background
• Changing format on PropertyGrid
• Declare New Form
• ADO - aggregate functions