narsil/Tanshu.Accounts.Helpers/ControlFactory.cs
2010-03-02 23:26:21 +05:30

103 lines
4.2 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using Tanshu.Accounts.Contracts;
namespace Tanshu.Accounts.Helpers
{
public static class ControlFactory
{
public static void GenerateButtons(ref Panel panel, Rectangle clientArea, int x, int y, int border, List<ProductDisplaySmallBO> list, ButtonClickDelegate bcDelegate)
{
int width = (clientArea.Width - (border * (x - 1))) / x;
int height = (clientArea.Height - (border * (y - 1))) / y;
int left = 0, top = 0, count = 0;
for (int i = 0; i < x; i++)
{
left = clientArea.Left + (i * (width + border));
for (int j = 0; j < y; j++)
{
count++;
if (list.Count < count)
break;
top = clientArea.Top + (j * (height + border));
ProductDisplaySmallBO item = list[(i * x) + j];
Button btn = GetButton(string.Format("{0}-{1}", i, j), item.Name, width, height, left, top, item.ProductID, bcDelegate);
panel.Controls.Add(btn);
}
}
}
public static void GenerateButtons(ref Panel panel, Rectangle clientArea, int x, int y, int border, ButtonClickDelegate bcDelegate)
{
int width = (clientArea.Width - (border * (x - 1))) / x;
int height = (clientArea.Height - (border * (y - 1))) / y;
for (int i = 0; i < x; i++)
{
int left = clientArea.Left + (i * (width + border));
for (int j = 0; j < y; j++)
{
int top = clientArea.Top + (j * (height + border));
Button btn = GetButton(string.Format("{0}-{1}", i, j), string.Format("{0}-{1}", i, j), width, height, left, top, string.Format("{0}-{1} Clicked", i, j), bcDelegate);
panel.Controls.Add(btn);
}
}
}
private static TextBox GetTextBox(string name, string text, int width, int height, int x, int y, object tag, ButtonClickDelegate bcDelegate)
{
TextBox textBox = CreateControl(ControlType.TextBox, name, text, width, height, x, y) as TextBox;
textBox.Tag = tag;
if (bcDelegate != null)
textBox.Click += new EventHandler(bcDelegate);
return textBox;
}
private static Button GetButton(string name, string text, int width, int height, int x, int y, object tag, ButtonClickDelegate bcDelegate)
{
Button button = CreateControl(ControlType.Button, name, text, width, height, x, y) as Button;
button.Tag = tag;
if (bcDelegate != null)
button.Click += new EventHandler(bcDelegate);
return button;
}
private static Control CreateControl(ControlType controlType, string name, string text, int width, int height, int x, int y)
{
switch (controlType)
{
case ControlType.TextBox:
{
TextBox textBox = new TextBox();
textBox.Name = name;
textBox.Text = text;
textBox.Width = width;
textBox.Height = height;
textBox.Left = x;
textBox.Top = y;
return textBox;
}
case ControlType.Button:
{
Button button = new Button();
button.Name = name;
button.Text = text;
button.Width = width;
button.Height = height;
button.Left = x;
button.Top = y;
return button;
}
default:
throw new ArgumentOutOfRangeException();
}
}
}
public enum ControlType
{
TextBox = 1,
Button = 2
}
public delegate void ButtonClickDelegate(object sender, EventArgs e);
}