Example of nested layout panels and dynamic form creation with .NET

  • From: Jamal Mazrui <empower@xxxxxxxxx>
  • To: programmingblind@xxxxxxxxxxxxx
  • Date: Fri, 10 Oct 2008 16:26:38 -0400 (EDT)

/*
Example of nested layout panels and dynamic form creation
Public domain by Jamal Mazrui
October 10, 2008

Here is an example of using nested layout panels of the .NET Framework.
Note that no pixel coordinates are used for positioning and sizing
controls.  The form, TableLayoutPanel, FlowLayoutPanels, and Buttons are
all laid out  automatically according to defaults and need.

I wrote and compiled this C# code with EdSharp and the command-line
compiler.  Although Visual Studio certainly has its benefits, I think
significantly more time, effort, and code would probably be involved to do
the same thing with the form designer, code generator, and multiple files
of a Visual Studio project.

Below is the source code of LayoutPanels.cs, which is also included with
the compiled version, LayoutPanels.exe in the archive available at
http://EmpowermentZone.com/LayoutPanels.zip

When the executable is run, it calls a function called MultiInput that I
have found useful in various contexts.  It presents a dialog with a number
of edit boxes determined at runtime.  Parameters to the function provide
the dialog title as a string, and the labels and default values of each
field in arrays.  The function returns an array with values as edited by
the user, or an empty array if the dialog is canceled.  The program shows
those return values in a message box.
*/

using System;
using System.Collections.Generic;
using System.Windows.Forms;

class Program {
static void Main() {
string sTitle = "Example of Layout Panels";
string[] aLabels = {"Label1", "Label2", "Label3"};
string[] aValues = {"Value1", "Value2", "Value3"};

string[] aResults = MultiInput(sTitle, aLabels, aValues);
if (aResults.Length == 0) return;

MessageBox.Show(String.Join("\n", aResults), "Results");
Console.Write(String.Join("\n", aResults));
} // Main method

public static string[] MultiInput(string sTitle, string[] aLabels,
string[] aValues) {

/*
Define a dialog form containing a vertical FlowLayoutPanel, which in turn,
contains a TableLayoutPanel and a horizontal FlowLayoutPanel below it
After initializing each control, suspend automatic layout until all its
properties, and those of its child controls, have been set
The OK button captures the values entered or modified, which the function
returns in an array
*/

// Define the dialog form
Form frm = new Form();
frm.SuspendLayout();
frm.AutoSize = true;
frm.AutoSizeMode = AutoSizeMode.GrowAndShrink;
frm.AutoScroll = true;

// Define the main , vertical FlowLayoutPanel
FlowLayoutPanel flpMain = new FlowLayoutPanel();
flpMain.SuspendLayout();
flpMain.AutoSize = true;
flpMain.AutoSizeMode = AutoSizeMode.GrowAndShrink;
flpMain.FlowDirection = FlowDirection.TopDown;

/*
Define a TableLayoutPanel with two colums, the left containing field
labels and the right containing field values
The number of rows is the number of items in the array of field labels
passed to the function.
There will be one row for each field.
determined by the size*/

TableLayoutPanel tlpFields = new TableLayoutPanel();
tlpFields.SuspendLayout();
tlpFields.Anchor = AnchorStyles.None;
tlpFields.AutoSize = true;
tlpFields.AutoSizeMode = AutoSizeMode.GrowAndShrink;
tlpFields.ColumnCount = 2;

// Add a column style for each column
for (int i = 0; i < tlpFields.ColumnCount; i++) {
tlpFields.ColumnStyles.Add(new ColumnStyle(SizeType.AutoSize));
}

// Add a row and row style for each field
tlpFields.RowCount = aLabels.Length;
for (int i = 0; i < tlpFields.RowCount; i++) {
Label lbl = new Label();
lbl.AutoSize = true;
lbl.Text = aLabels[i] + ":";
TextBox txt = new TextBox();
txt.Text = aValues[i];
tlpFields.Controls.AddRange(new Control[] {lbl, txt});
tlpFields.RowStyles.Add(new RowStyle(SizeType.AutoSize));
}
tlpFields.ResumeLayout();

// Below the TableLayoutPanel of fields, add a horizontal FlowLayoutPanel
containing buttons
FlowLayoutPanel flpButtons = new FlowLayoutPanel();
flpButtons.SuspendLayout();
flpButtons.Anchor = AnchorStyles.None;
flpButtons.AutoSize = true;
flpButtons.AutoSizeMode = AutoSizeMode.GrowAndShrink;
flpButtons.FlowDirection = FlowDirection.LeftToRight;

// Define the OK button
Button btnOK = new Button();
btnOK.Text = "OK";

// Define its event handler
List<string> listResults = new List<string>();
btnOK.Click += delegate(object o, EventArgs e) {
foreach (Control ctl in tlpFields.Controls) {
if (ctl.GetType() == typeof(TextBox)) listResults.Add(ctl.Text);
}
frm.Close();
};

// Define the Cancel button
Button btnCancel = new Button();
btnCancel.Text = "Cancel";
btnCancel.Click += delegate(object o, EventArgs e) {frm.Close();};

flpButtons.Controls.AddRange(new Control[] {btnOK, btnCancel});
flpButtons.ResumeLayout();

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

// Set remaining properties of the form
frm.AcceptButton = btnOK;
frm.CancelButton = btnCancel;
frm.StartPosition = FormStartPosition.CenterParent;
frm.Text = sTitle;
frm.Controls.Add(flpMain);
frm.ResumeLayout();
frm.ShowDialog();
frm.Dispose();

return listResults.ToArray();
} // MultiInput method

} // Program class

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

Other related posts: