narsil/Tanshu.Accounts.Helpers/ControlFactory.cs

185 lines
7.6 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, ref List<Button> buttonList, Rectangle clientArea, int x, int y, int border, List<ProductDisplaySmallBO> list, ButtonClickDelegate bcDelegate)
{
if (buttonList.Count != 0)
{
for (int i = buttonList.Count - 1; i >= 0; i--)
{
buttonList[i].Dispose();
}
buttonList = new List<Button>();
}
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 j = 0; j < y; j++)
{
top = clientArea.Top + (j * (height + border));
for (int i = 0; i < x; i++)
{
count++;
if (list.Count < count)
break;
left = clientArea.Left + (i * (width + border));
ProductDisplaySmallBO item = list[(j * x) + i];
Button btn = GetButton(string.Format("{0}-{1}", i, j), item.Name, width, height, left, top, item.ProductID, bcDelegate);
panel.Controls.Add(btn);
buttonList.Add(btn);
}
}
}
public static void GenerateButtons(ref Panel panel, ref List<Button> buttonList, Rectangle clientArea, int x, int y, int border, int page, List<ProductTypeBO> list, ButtonClickDelegate bcDelegate)
{
if (buttonList.Count != 0)
{
for (int i = buttonList.Count - 1; i >= 0; i--)
{
buttonList[i].Dispose();
}
buttonList = new List<Button>();
}
int width = (clientArea.Width - (border * (x - 1))) / x;
int height = (clientArea.Height - (border * (y - 1))) / y;
int left = 0, top = 0, count = 0;
if (list.Count > x * y)
{
if (page != 0)
list.RemoveRange(0, y * page);
if (list.Count > y)
list.RemoveRange(y, list.Count - y);
//int pages = Convert.ToInt32(Math.Ceiling(Convert.ToDouble(list.Count / (x * y)))) - 1;
//if (page > 0)
// startButton = true;
//if (page != pages)
// finishButton = true;
//if (startButton)
//{
// list.RemoveRange(0, start);
// list.Insert(0, new ProductTypeBO() { Name = "Previous", ProductTypeID = new Guid() });
//}
//if (finishButton)
//{
// list.RemoveRange((x * y) - 1, list.Count - (x * y) + 1);
// list.Insert(x * y - 1, new ProductTypeBO() { Name = "Next", ProductTypeID = new Guid() });
//}
// the list is long
}
int sJ = 0;
int sI = 0;
//Math.DivRem(start, x, out sI);
//sJ = (start - sI) / y;
//count = start - 1;
for (int j = sJ; j < y; j++)
{
top = clientArea.Top + (j * (height + border));
for (int i = sI; i < x; i++)
{
count++;
if (list.Count < count)
break;
left = clientArea.Left + (i * (width + border));
//ProductTypeBO item = list[(j * x) + i];
ProductTypeBO item = list[count - 1];
Button btn = GetButton(string.Format("{0}-{1}", i, j), item.Name, width, height, left, top, item.ProductTypeID, bcDelegate);
panel.Controls.Add(btn);
buttonList.Add(btn);
}
}
}
public static void GenerateTables(ref Panel panel, ref List<Button> buttonList, Rectangle clientArea, int x, int y, int border, List<FoodTableBO> list, ButtonClickDelegate bcDelegate)
{
if (buttonList.Count != 0)
{
for (int i = buttonList.Count - 1; i >= 0; i--)
{
buttonList[i].Dispose();
}
buttonList = new List<Button>();
}
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 j = 0; j < y; j++)
{
top = clientArea.Top + (j * (height + border));
for (int i = 0; i < x; i++)
{
count++;
if (list.Count < count)
break;
left = clientArea.Left + (i * (width + border));
FoodTableBO item = list[count - 1];
Button btn = GetButton(string.Format("{0}-{1}", i, j), item.Name, width, height, left, top, item.TableID, bcDelegate);
panel.Controls.Add(btn);
buttonList.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);
}