Click here to Skip to main content
6,295,667 members and growing! (17,340 online)
Email Password   helpLost your password?
Desktop Development » Miscellaneous » General     Intermediate

Bringing Back FlowLayout to WinForms Controls

By Matt Dockins

This article describes how to create a Panel that allows FlowLayout positioning for WinForms.
C#, Windows, .NET 1.0, .NET 1.1VS.NET2003, Dev
Posted:8 Oct 2004
Views:54,860
Bookmarked:20 times
Announcements
Loading...
 
Search    
Advanced Search
printPrint   Broken Article?Report       add Share
  Discuss Discuss   Recommend Article Email
10 votes for this article.
Popularity: 3.45 Rating: 3.45 out of 5
2 votes, 20.0%
1

2
1 vote, 10.0%
3
4 votes, 40.0%
4
3 votes, 30.0%
5

Sample Image - FlowLayoutPanel.jpg

Introduction

With the next Visual Studio being in Beta 2 and the FlowLayout supposedly making its comeback in said revision, this could very well be a day late and a dollar short, but I had need of it and thought I would share it anyway. The FlowLayout option for laying out your controls has been missing from .NET WinForms for sometime now. While many of us don't mind absolute positioning, it can get to be a bear (or at the very least annoying) when you want to add controls dynamically but don't want to worry about System.Drawing.Points or .Top and .Left properties.

Background

I'm currently implementing a thumbnail view and was anguished over the fact that there was no FlowLayout option for WinForms, and having Googled for FlowLayout controls and seeing a remarkable dearth of them, I decided to write my own and share it for general consumption.

Since I've posted this, I've enhanced it a little to include a LayoutStyle property that takes an enum for LayoutStyles.FlowLayout or LayoutStyles.GridLayout.

Usage

The FlowLayoutPanel is a small application with a custom control (FlowLayoutPanel.cs) that extends System.Windows.Forms.Panel and overrides:

protected override void OnLayout(LayoutEventArgs levent)

to handle FlowLayout behavior for the Panel.

It's straightforward and simple, with all the real work being done in the method above (code below). The download contains a solution from Visual Studio .NET 2003 that bundles an example application and the FlowLayoutPanel. Barring the addition of a LayoutStyle property, there is no significant difference in the usage between this and a standard Panel, except, of course, if the LayoutStyle property is set to LayoutStyles.FlowLayout, then adding a control via this.Controls.Add(someControl) or dragging and dropping in design mode positions the control automatically using FlowLayout characteristics.

protected override void OnLayout(LayoutEventArgs levent)
{
    if (this.LayoutStyle == LayoutStyles.FlowLayout) 
    {
      int nextTop = 0, nextLeft = 0;
      int maxHeight = 0, maxWidth = 0;
      int ParentWidth;
      if (this.Parent != null)
      {
          ParentWidth = this.Parent.Width;
      }
      else
      {
          ParentWidth = this.Width;
      }
      foreach(Control myControl in this.Controls)
      {
          myControl.Top = nextTop;
          myControl.Left = nextLeft;
          if (myControl.Height > maxHeight)
          {
              maxHeight = myControl.Height;
          }
          if (myControl.Width > maxWidth)
          {
              maxWidth = myControl.Width;
          }
          if ((nextLeft + myControl.Width + maxWidth) >= ParentWidth)
          {
              nextTop += maxHeight;
              nextLeft = 0;
          }
          else
          {
              nextLeft += myControl.Width;
          }
      } 
    this.AutoScrollPosition = new System.Drawing.Point(0,0);
    base.OnLayout (levent);
}

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Matt Dockins


Member
Matt Dockins is the President of Premier Programming Services, offering custom programming solutions for the Sage SalesLogix Business Partner and Sage SalesLogix Client as well as general programming services.

Premier Programming Services is a fully licensed Sage SalesLogix Development Partner, a VineyardSoft KnowledgeSync partner, and Certified Microsoft ISV.

http://www.premierprogrammingservices.com




Occupation: President
Company: Premier Programming Services
Location: United States United States

Other popular Miscellaneous articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 18 of 18 (Total in Forum: 18) (Refresh)FirstPrevNext
GeneralAdd this snippet to enable smooth scrolling PinmemberMalcolm Hall6:34 26 Jun '08  
QuestionWill this work with Compact Framework? Pinmember6:03 30 Jan '07  
GeneralCan we Center Align of controls in flowlayout? Pinmembershahulhameed.s@polaris.co.in2:37 27 Jul '05  
GeneralFix for ordering newly added controls? PinmemberCarl Mercier10:40 20 Apr '05  
GeneralRe: Fix for ordering newly added controls? PinsussAnonymous6:50 21 Apr '05  
GeneralA few added properties Pinmembergregoftheweb12:11 19 Jan '05  
GeneralDocking and Anchoring PinsussDuray AKAR7:08 14 Dec '04  
GeneralSome problems and their solutions PinsupporterMarc Clifton4:35 7 Nov '04  
GeneralRe: Some problems and their solutions PinmemberMatt Dockins17:54 7 Nov '04  
GeneralRe: Some problems and their solutions PinmemberCarl Mercier10:35 20 Apr '05  
GeneralRe: Some problems and their solutions PinsupporterMarc Clifton15:49 20 Apr '05  
GeneralRe: Some problems and their solutions PinmemberCarl Mercier15:53 20 Apr '05  
GeneralThanks PinmemberSteven Campbell10:41 29 Oct '04  
GeneralRe: Thanks PinmemberCarl Mercier6:42 20 Apr '05  
GeneralRe: Thanks PinmemberCarl Mercier10:46 20 Apr '05  
GeneralLooks good PinsupporterMarc Clifton10:57 8 Oct '04  
GeneralRe: Looks good Pinmembermdockins11:40 8 Oct '04  
GeneralRe: Looks good Pinmemberchadyoshikawa7:13 7 Aug '05  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 8 Oct 2004
Editor: Smitha Vijayan
Copyright 2004 by Matt Dockins
Everything else Copyright © CodeProject, 1999-2009
Web11 | Advertise on the Code Project