Windows Develop Bookmark and Share   
 index > Windows Forms Designer > FlowLayoutPanel.GetPreferredSize() not working as expected in design mode
 

FlowLayoutPanel.GetPreferredSize() not working as expected in design mode

I'm developing an autosizingtoolbar control that is a composite of a few other controls, including a FlowLayoutPanel. The contents of the internal FlowLayoutPanel determine the height of the toolbar. I've written a designer forthe control that reparents any controls dropped onto it to the internal FlowLayoutPanel.I then need to resize the FlowLayoutPanel and the control which contains it, so I thentrigger a method in the composite control's class that handles the control's internal layout at runtime (which works fine!).
At design time, unfortunately, in that layout method when I request the FlowLayoutPanel's preferred size for a given width, it ignores the most recently added control and returns the size one control too small. I've tried everything I could think of to force a layout in the FlowLayoutPanel before asking for its preferred size, but it never does any good. I've also verified that the most recently added control is indeed in the FlowLayoutPanel's controls collection. I think I just need to tell the designer host something, but I'm not sure how. Using the ComponentChangeService.OnComponentChanged method with the FlowLayoutPanel as the component argument didn't seem to help. I appreciate any suggestions.

Thank you,
Josh Usovsky

Skwerl  Monday, April 27, 2009 7:45 PM
Hi Josh,

Can you clarify me following questions so that I can give you appropriate suggestions?
1. How did youreparent any controls dropped ontoFlowLayoutPanel in designer?
2. Which method did you trigger that handles the control's internal layout?

If you want to autosize control both in runtime and design time, you can use custom layout engine. Please visit the following link for details:

You have make sure that when your get the preferred size ofFlowLayoutPanel, the reparent code has been executed.

Regards,
Hongye Sun (hongyes@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
Send us any feedback you have about the help from MSFT at fbmsdn@microsoft.com.
Hongye Sun  Tuesday, April 28, 2009 7:02 AM
Hi, Hongye.I have tried reparenting a few different ways, but they all had the same effect.Presently, I and just moving the dropped controlusing the control designer's Control property that have cast to my control type so that I can access the internal FlowPayoutPanel.
protected override void OnDragDrop(DragEventArgs de)
		{
			DesignerTransaction transaction = null;
			IDesignerHost host = (IDesignerHost)GetService(typeof(IDesignerHost));
			IComponentChangeService changeService = (IComponentChangeService)GetService(typeof(IComponentChangeService));

			IToolboxService toolboxService = (IToolboxService)GetService(typeof(IToolboxService));
			ToolboxItem toolboxItem = toolboxService.GetSelectedToolboxItem();
			if (toolboxItem != null)
			{
				//Dropping a toolbox item
//TODO: Finish this section after proof of concept transaction = host.CreateTransaction("MovingDroppedToolboxControl"); IComponent[] comps = toolboxItem.CreateComponents(); changeService.OnComponentChanging(this.control.Toolbar, null); this.control.Toolbar.Controls.Add(comps[0] as Control); changeService.OnComponentChanged(this.control.Toolbar, null, null, null); transaction.Commit(); } else if (!(this.Control as FlowToolbar).flowLayoutPanel.Contains(this.controlBeingDragged)) { if (host != null && changeService != null) { transaction = host.CreateTransaction("MovingControl"); changeService.OnComponentChanging(control.Toolbar, null); control.Toolbar.Controls.Add(this.controlBeingDragged); changeService.OnComponentChanged(this.control.Toolbar, null, null, null); transaction.Commit(); (this.Control as FlowToolbar).flowLayoutPanel.PerformLayout(); (this.Control as FlowToolbar).LayoutChildren(); } } }
I've also tried doing it with a PropertyDescriptor.
PropertyDescriptor descriptor = TypeDescriptor.GetProperties(this.controlBeingDragged)["Parent"];
			if (descriptor != null)
			{
				descriptor.SetValue(this.controlBeingDragged, this.Control as FlowToolbar).flowLayoutPanel);

			}
LayoutChildren()from the code block above isa method in the FlowToolbar control and is whatI useat runtimefor laying out the controls (I will override the LayoutEngine before I finish the control). When I attach the debugger, I can see that when the designer calls this method, the reparented control is in the FlowLayoutPanel's Controls collection, butthe panel's GetPreferredSize() method ignores it.In Visual Studio, after the dropped control is reparented,if I try to resize the FlowToolbar with the mouse, the layout routine runs again and everything is fine. Seeing this, I thought maybe if I could spur thelayout of the controls in code and perhaps that would fix the issue. I have called "PerformLayout" (and all other manner of things) on the control the designer is attached to and on it's internal FlowLayoutPanel, but that has not helped. It might also be worth noting that everything works fine in Visual Studio 2008. I only have this issue in 2005.

Thanks so much for your help!
Josh Usovsky
Skwerl  Tuesday, April 28, 2009 4:16 PM
Sorry, Skwerl. I still can't reproduce the issue.

I notice that the LayoutChildren method only be invoked in one condition. Why not call it in the first condition check:if (toolboxItem != null)?

My test environment is VS 2005. The GetPreferredSize always returns correct width.

Is it possible for you to build a sample repro project and send it to my emailhongyes@online.microsoft.com, remove 'online.'?

In addition, what .NET Framework versions are you targeting in VS 2005 and 2008? It seems thatGetPreferredSize method result is not relevant to IDE.

Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
Send us any feedback you have about the help from MSFT at fbmsdn@microsoft.com.
Hongye Sun  Wednesday, April 29, 2009 8:51 AM
Hi Skwerl, I didn't heard from you since my last reply. Do you need any further help? Please let me know.
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
Send us any feedback you have about the help from MSFT at fbmsdn@microsoft.com.
Hongye Sun  Tuesday, May 05, 2009 9:48 AM

You can use google to search for other answers

Custom Search

More Threads

• Design mode repaint problem
• why invoke IComponentChangeService.OnComponentChanging method did not work?
• Minimize, Maximize, Normal
• Generic Windows Form Controls
• Windows XP Buttons Style
• Question: Add Property to UserControl
• Using Custom ProfessionalColor Table with toolstriprenderer.
• Valid a Custom Control Property at Design Timem, Please Help!!!!
• Taking backup of SQL Server 2000 database in VB.Net 2005?
• How to Create smart tag?