using SelfHelpClient.BL; using SelfHelpClient.BO; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; namespace SelfHelpClient { public partial class WeightForm : Form { WeightBill entity; Thread refersh; public WeightForm(ViewEntity view) { InitializeComponent(); if (view.BillType == 0) entity = WeightBillBL.CreateWeightBill(view.ID); else entity = WeightBillBL.GetWeightBill(view.ID); var tableHeight = InitTableLayoutPanel(); BuildGridPanel(tableHeight); this.FormClosing += delegate { if (refersh != null && refersh.IsAlive) refersh.Abort(); }; } protected override void OnLoad(EventArgs e) { base.OnLoad(e); farmerGrid.DataSource = entity.FarmerDetails; farmerGrid.Refresh(); weightGrid.DataSource = entity.Details; weightGrid.Refresh(); refersh = new Thread(RefershDetail); refersh.Start(); } private void RefershDetail() { while (true) { Thread.Sleep(2000); if (this.IsHandleCreated) { this.Invoke(new Action(() => { var detail = WeightBillBL.GetWeightDetail(entity.ID); if (detail.Any()) { weightGrid.DataSource = detail; weightGrid.Refresh(); } })); } } } private void BuildGridPanel(int tableHeight) { groupBox1.Location = new Point(0, tableHeight + 10); groupBox1.Width = Screen.PrimaryScreen.Bounds.Width; groupBox1.Height = 200; groupBox2.Location = new Point(0, tableHeight + groupBox1.Height + 10); groupBox2.Width = Screen.PrimaryScreen.Bounds.Width; groupBox2.Height = 150; } private int InitTableLayoutPanel() { TableLayoutPanel tablePanel = new TableLayoutPanel(); tablePanel.Width = Screen.PrimaryScreen.Bounds.Width; panel2.Controls.Add(tablePanel); var dic = BuildFields(); var table = ComputeColAndRow(dic.Count); tablePanel.Height = 60 * table.Rows + 5; DynamicLayout(tablePanel, table); var type = typeof(WeightBill); var r = 0; var c = 0; foreach (var item in dic) { Label title = BuildLabel(item.Value + ":"); tablePanel.Controls.Add(title); tablePanel.SetRow(title, r); tablePanel.SetColumn(title, c); c++; var v = type.GetProperty(item.Key).GetValue(entity); var lbl = string.Empty; switch (item.Key) { case "WeighTime": lbl = string.Format("{0:yyyy-MM-dd HH:mm:ss}", v); break; case "JingJianFee": case "DiscontMoney": case "ShackWeight": case "ShackPrice": case "ShackMoney": lbl = string.Format("{0:#0.######}", v); break; case "AnimalTestDate": lbl = string.Format("{0:yyyy-MM-dd}", v); break; default: lbl = string.Format("{0}", v); break; } Label content = BuildLabel(lbl); tablePanel.Controls.Add(content); tablePanel.SetRow(content, r); tablePanel.SetColumn(content, c); c++; if (c % table.Cols == 0) { c = 0; r++; } } return tablePanel.Height; } TableSturct ComputeColAndRow(int fileCount) { var screenWidth = Screen.PrimaryScreen.Bounds.Width; var col = Math.Floor((decimal)screenWidth / (160 + 280)); var row = Math.Ceiling(fileCount / col); return new TableSturct() { Rows = (int)row, Cols = (int)col * 2 }; } private void DynamicLayout(TableLayoutPanel layoutPanel, TableSturct table) { layoutPanel.RowCount = table.Rows; //设置分成几行 for (int i = 0; i < table.Rows; i++) { layoutPanel.RowStyles.Add(new RowStyle(SizeType.Absolute, 60)); } layoutPanel.ColumnCount = table.Cols; //设置分成几列 for (int i = 0; i < table.Cols; i++) { var precent = i % 2 == 0 ? 30F : 70F; layoutPanel.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, precent)); } } Dictionary BuildFields() { var dic = new Dictionary() { {"Supplier_Name","供应商"},{"BankAccount","银行卡号"},{"PurchaseType_Name","收购类型"},{"WeighTime","过磅时间"},{"Car_Name","车辆"},{"Employee_Name","业务员"},{"JingJianFee","经检费"},{"DiscontMoney","猪场扣款"},{"ShackWeight","棚前重量"},{"ShackPrice","棚前单价"},{"ShackMoney","棚前金额"},{"AnimalTestMan","动检人"},{"AnimalTestNumber","动检证号"},{"AnimalTestDate","动检日期"}, }; return dic; } Label BuildLabel(string content) { Label label = new Label(); label.Text = content; label.Font = new System.Drawing.Font("宋体", 18); label.Dock = DockStyle.Fill; label.TextAlign = ContentAlignment.MiddleLeft; return label; } private void backBtn_Click(object sender, EventArgs e) { MainForm.Form.Show(); this.Close(); } private void okBtn_Click(object sender, EventArgs e) { //目前过磅信息界面上的确认按钮没有功能,暂时做点击完后返回主界面; MainForm.Form.Show(); this.Close(); } } struct TableSturct { public int Rows { get; set; } public int Cols { get; set; } } }