Re: How to easily arranging controls?

  • From: Jamal Mazrui <empower@xxxxxxxxx>
  • To: programmingblind@xxxxxxxxxxxxx
  • Date: Sat, 1 Dec 2007 15:15:36 -0500 (EST)

Hi Meared,
Web searches on FlowLayout Panel and TableLayout Panel with find examples.
Here are some I found useful.

Many Windows Forms samples to download are available at
http://windowsclient.net/downloads/folders/applications/default.aspx

Search that page for "layout" for a Word document with fairly
comprehensive information about autolayout in the .NET Framework 2.0.

A good blog entry is at
http://blogs.msdn.com/jpricket/archive/2006/04/05/569280.aspx

and an article is at
http://www.codeproject.com/dotnet/Whidbey_ControlContainers.asp?print=true

Below is a fruit basket program I previously posted that illustrates
Combining horizontal and vertical FlowLayoutPanels.

Jamal

//Fruit Basket program in C# 2.0 with layout panels
//Public domain by Jamal Mazrui
//February 25, 2007

//Compile either in Visual Studio or at command prompt as follows:
//csc.exe /nologo /t:winexe fb.cs

using System;
using System.ComponentModel;
using System.Windows.Forms;

class Program {
static void Main() {
Form frm = new Form();
frm.SuspendLayout();
frm.Text = "Fruit Basket";
frm.AutoSize = true;
frm.AutoSizeMode = AutoSizeMode.GrowAndShrink;

FlowLayoutPanel flpMain = new FlowLayoutPanel();
flpMain.SuspendLayout();
flpMain.AutoSize = true;
flpMain.AutoSizeMode  = AutoSizeMode.GrowAndShrink;
flpMain.FlowDirection = FlowDirection.TopDown;

FlowLayoutPanel flpData = new FlowLayoutPanel();
flpData.SuspendLayout();
flpData.AutoSize = true;
flpData.AutoSizeMode  = AutoSizeMode.GrowAndShrink;
flpData.FlowDirection = FlowDirection.LeftToRight;

Label lblFruit = new Label();
lblFruit.Text = "&Fruit:";

TextBox txtFruit = new TextBox();
txtFruit.AccessibleName = lblFruit.Text.Replace("&", "");

Label lblBasket = new Label();
lblBasket.Text = "&Basket:";

ListBox lstBasket = new ListBox();
lstBasket.AccessibleName = lblBasket.Text.Replace("&", "");

flpData.Controls.AddRange(new Control[] {lblFruit, txtFruit, lblBasket,
lstBasket});
flpData.ResumeLayout();

FlowLayoutPanel flpButtons = new FlowLayoutPanel();
flpButtons.SuspendLayout();
flpButtons.Anchor = AnchorStyles.None;
flpButtons.AutoSize = true;
flpButtons.AutoSizeMode  = AutoSizeMode.GrowAndShrink;
flpButtons.FlowDirection = FlowDirection.LeftToRight;

Button btnAdd = new Button();
btnAdd.Text = "&Add";
btnAdd.AccessibleName = btnAdd.Text.Replace("&", "");
btnAdd.Click += delegate(object o, EventArgs e) {
string sFruit = txtFruit.Text.Trim();
if (sFruit == "") MessageBox.Show("No fruit to add!", "Alert");
else {
int iIndex = lstBasket.SelectedIndex;
lstBasket.Items.Insert(0, sFruit);
lstBasket.SelectedIndex = 0;
txtFruit.Clear();
}
};

Button btnDelete = new Button();
btnDelete.Text = "&Delete";
btnDelete.AccessibleName = btnDelete.Text.Replace("&", "");
btnDelete.Click += delegate(object o, EventArgs e) {
int iIndex = lstBasket.SelectedIndex;
if (iIndex == -1) MessageBox.Show("No fruit to delete!", "Alert");
else {
lstBasket.Items.RemoveAt(iIndex);
int iCount = lstBasket.Items.Count;
if (iIndex  < iCount) lstBasket.SelectedIndex = iIndex;
else if (iCount > 0) lstBasket.SelectedIndex = iCount - 1;
}
};

flpButtons.Controls.AddRange(new Control[] {btnAdd, btnDelete});
flpButtons.ResumeLayout();

flpMain.Controls.AddRange(new Control[] {flpData, flpButtons});
flpMain.ResumeLayout();

frm.Controls.Add(flpMain);
frm.AcceptButton = btnAdd;
frm.StartPosition = FormStartPosition.CenterScreen;
frm.Closing += delegate(object o, CancelEventArgs e) {
if (MessageBox.Show("Exit program?", "Confirm",
MessageBoxButtons.OKCancel) == DialogResult.Cancel) e.Cancel = true;};

frm.ResumeLayout();
frm.ShowDialog();
} // Main method
} // Program class

__________
View the list's information and change your settings at 
//www.freelists.org/list/programmingblind

Other related posts: