narsil/Tanshu.Accounts.PointOfSale/Sales/ModifierForm.cs

112 lines
3.7 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using Tanshu.Accounts.Entities;
using Tanshu.Accounts.Helpers;
namespace Tanshu.Accounts.PointOfSale
{
public partial class ModifierForm : Form
{
private IList<InventoryModifier> selection;
private IList<Modifier> modifiersList;
private IList<CheckBox> list;
public ModifierForm(IList<Modifier> source, IList<InventoryModifier> selection)
{
InitializeComponent();
this.selection = selection;
this.modifiersList = source;
list = new List<CheckBox>();
}
private void button_Click(object sender, EventArgs e)
{
CheckBox button = sender as CheckBox;
if (button == null)
return;
if (button.CheckState == CheckState.Checked)
selection.Add(new InventoryModifier() { Modifier = (Modifier)button.Tag });
else
selection.Remove(selection.First(x => x.Modifier.ModifierID == ((Modifier)button.Tag).ModifierID));
}
private static Point GetSize(FlowLayoutPanel panel, int count)
{
Point min = new Point(20, 50);
Point max = new Point(75, 75);
var marginWidth = panel.Margin.Right + panel.Margin.Left;
var marginHeight = panel.Margin.Top + panel.Margin.Bottom;
var rows = panel.ClientSize.Height / (max.Y + marginHeight);
var cols = panel.ClientSize.Width / (max.X + marginWidth);
var num = rows * cols;
if (num >= count)
return max;
var w = max.X;
var h = max.Y;
while (w > min.X && h > min.Y)
{
h = (panel.ClientSize.Height / (rows + 1)) - marginHeight;
if (h >= min.Y)
{
++rows;
}
if (rows * cols >= count)
break;
w = (panel.ClientSize.Width / (cols + 1)) - marginWidth;
if (w >= min.X)
{
++cols;
}
if (rows * cols >= count)
break;
}
max.X = (panel.ClientSize.Width / cols) - marginWidth;
max.Y = (panel.ClientSize.Height / rows) - marginHeight;
return max;
}
private void ModifierForm_Load(object sender, EventArgs e)
{
var size = GetSize(flpModifier, modifiersList.Count);
ButtonClickDelegate bcDelegate = new ButtonClickDelegate(button_Click);
for (int i = 0; i < modifiersList.Count; i++)
{
var control = new CheckBox()
{
Name = i.ToString(),
Text = modifiersList[i].Name,
Width = size.X,
Height = size.Y,
Tag = modifiersList[i],
Appearance = Appearance.Button,
Checked = selection.Count(x => x.Modifier.ModifierID == modifiersList[i].ModifierID) > 0
};
control.Click += new EventHandler(bcDelegate);
flpModifier.Controls.Add(control);
list.Add(control);
}
}
private void btnClose_Click(object sender, EventArgs e)
{
this.Close();
}
private void flpModifier_Resize(object sender, EventArgs e)
{
var size = GetSize(flpModifier, modifiersList.Count);
foreach (var item in list)
{
item.Width = size.X;
item.Height = size.Y;
}
}
}
}