lay out a form's components

Shows how to layout components on a Window

Download formlayout.zip

Synopsis:

formlayout.cs


formlayout.cs

Synopsis
using System;
using System.Windows.Forms;

public class formlayout : Form
{
  private ListBox mLB1 = new ListBox();
  private ListBox mLB2 = new ListBox();
  private ListBox mLB3 = new ListBox();

  private void InitializeComponent()
  {
    //...
    this.Layout += new System.Windows.Forms.LayoutEventHandler(this.HandleLayout);
    //...
  }

  //----------------------------------------
  //There are 3 list boxes side by side
  //make the height of all 3 the same as the main window's client
  //make the first a fixed width
  //make the second and third take up the remaining width
  private void HandleLayout(object sender, LayoutEventArgs e)
  {
    int gap = 5;
    
    //set the height to the main window's height
    mLB1.SetBounds(0,0,0,this.ClientSize.Height,BoundsSpecified.Y | BoundsSpecified.Height);

    //get the right edge of the first list box
    int rightedge = mLB1.Bounds.X + mLB1.Bounds.Width;
    
    //calculate the remainder of the space in the client window
    int lbwidth = (this.ClientSize.Width - rightedge - gap) / 2;
    
    //the left edge of the next list box is the last list box's right edge
    //plus a gap
    int leftedge = rightedge + gap;

    mLB2.SetBounds(leftedge,0,lbwidth,this.ClientSize.Height);
    
    leftedge += lbwidth;
    mLB3.SetBounds(leftedge,0,lbwidth,this.ClientSize.Height);

    Invalidate();
  }
}






Contact me about content on this page using john_web-at-arrizza-dot-com
For Web Master or site problems contact: webadmin-at-arrizza-dot-com
Copyright John Arrizza (c) 2001,2002,2003,2004,2005,2006,2007