CadParamPluging/UI/LayoutSelectForm.cs

119 lines
3.9 KiB
C#

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using CadParamPluging.Cad;
namespace CadParamPluging.UI
{
public class LayoutSelectForm : Form
{
private readonly ListBox _list;
private readonly List<LayoutAnnotation> _items;
public LayoutAnnotation Selected { get; private set; }
public LayoutSelectForm(IEnumerable<LayoutAnnotation> candidates)
{
Text = "选择图纸";
Size = new Size(520, 360);
StartPosition = FormStartPosition.CenterParent;
FormBorderStyle = FormBorderStyle.FixedDialog;
MaximizeBox = false;
MinimizeBox = false;
ShowInTaskbar = false;
_items = (candidates ?? Enumerable.Empty<LayoutAnnotation>()).ToList();
var layout = new TableLayoutPanel
{
Dock = DockStyle.Fill,
Padding = new Padding(10),
RowCount = 3,
ColumnCount = 1
};
layout.RowStyles.Add(new RowStyle(SizeType.AutoSize));
layout.RowStyles.Add(new RowStyle(SizeType.Percent, 100f));
layout.RowStyles.Add(new RowStyle(SizeType.AutoSize));
var lbl = new Label
{
Text = "匹配到多张图纸,请选择:",
Dock = DockStyle.Fill,
AutoSize = true
};
_list = new ListBox { Dock = DockStyle.Fill };
foreach (var it in _items)
{
_list.Items.Add(Format(it));
}
if (_list.Items.Count > 0)
{
_list.SelectedIndex = 0;
}
var buttons = new FlowLayoutPanel
{
Dock = DockStyle.Fill,
FlowDirection = FlowDirection.RightToLeft,
AutoSize = true
};
var btnCancel = new Button { Text = "取消", DialogResult = DialogResult.Cancel };
var btnOk = new Button { Text = "确定" };
btnOk.Click += (_, __) => OnOk();
buttons.Controls.Add(btnCancel);
buttons.Controls.Add(btnOk);
layout.Controls.Add(lbl, 0, 0);
layout.Controls.Add(_list, 0, 1);
layout.Controls.Add(buttons, 0, 2);
Controls.Add(layout);
AcceptButton = btnOk;
CancelButton = btnCancel;
}
private void OnOk()
{
var idx = _list.SelectedIndex;
if (idx < 0 || idx >= _items.Count)
{
MessageBox.Show(this, "请选择一项。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
Selected = _items[idx];
DialogResult = DialogResult.OK;
Close();
}
private static string Format(LayoutAnnotation it)
{
var d = it?.DeliveryStatuses != null && it.DeliveryStatuses.Count > 0 ? string.Join("/", it.DeliveryStatuses) : "(无)";
var p = it?.ProcessMethods != null && it.ProcessMethods.Count > 0 ? string.Join("/", it.ProcessMethods) : "(无)";
var s = it?.StructuralFeatures != null && it.StructuralFeatures.Count > 0 ? string.Join("/", it.StructuralFeatures) : "(无)";
return $"{it?.LayoutName} | 交付状态={d} | 工艺方法={p} | 结构特征={s}";
}
public static bool TrySelect(IWin32Window owner, IEnumerable<LayoutAnnotation> candidates, out LayoutAnnotation selected)
{
using (var f = new LayoutSelectForm(candidates))
{
var r = f.ShowDialog(owner);
if (r == DialogResult.OK)
{
selected = f.Selected;
return selected != null;
}
}
selected = null;
return false;
}
}
}