using HalconDotNet; using LB_SmartVision.ProcessRun; using OpenCvSharp.Flann; using ReaLTaiizor.Controls; using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace LB_SmartVision.Forms.Pages.SettingPage { public partial class CsvSettingControl : UserControl { ConcurrentDictionary dicComboxs = new ConcurrentDictionary(); public bool IsSelected { get { return ckb_Selected.Checked; } } public string Title { get { return RunBll.Name; } } private CsvSetting csv { get; set; } = new CsvSetting(); private ProcessRunBll RunBll { get { if (GlobalVar.dicProcesses.ContainsKey(cmb_Process.Text)) return GlobalVar.dicProcesses[cmb_Process.Text]; else return null; } } public CsvSettingControl(CsvSetting csv = null) { InitializeComponent(); if (csv == null) csv = new CsvSetting(); this.csv = csv; cmb_Process.MouseClick += MouseClick; cmb_Process.SelectedIndexChanged += SelectedIndexChanged; cmb_Process.Items.Clear(); cmb_Process.Items.Add(""); cmb_Process.Items.AddRange(GlobalVar.dicProcesses.Keys.ToArray()); cmb_Process.Text = csv.ProcessName; if (csv.Others.Count > 0) { Single perSpace = 100 / csv.Others.Count; while (tableLayoutPanel1.ColumnCount - 4 < csv.Others.Count) { tableLayoutPanel1.ColumnCount++; tableLayoutPanel1.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, perSpace)); } for (int i = 4; i < tableLayoutPanel1.ColumnCount; i++) { tableLayoutPanel1.ColumnStyles[i].SizeType = SizeType.Percent; tableLayoutPanel1.ColumnStyles[i].Width = perSpace; } int index = 0; foreach (var value in csv.Others) { string key = $"cmb{index}"; if (!dicComboxs.ContainsKey(key)) { ComboBox comboBox = new ComboBox(); comboBox.Name = key; comboBox.MouseClick += MouseClick; comboBox.SelectedIndexChanged += SelectedIndexChanged; comboBox.Text = value; comboBox.Dock = DockStyle.Fill; tableLayoutPanel1.Controls.Add(comboBox, index + 4, 0); dicComboxs.TryAdd(key, comboBox); } else dicComboxs[key].Text = value; index++; } } // 恢复布局(只触发一次重绘) tableLayoutPanel1.ResumeLayout(false); this.ResumeLayout(false); } private void MouseClick(object sender, EventArgs e) { switch ((sender as ComboBox).Name) { case "cmbProcess": cmb_Process.Items.Clear(); cmb_Process.Items.Add(""); cmb_Process.Items.AddRange(GlobalVar.dicProcesses.Keys.ToArray()); break; default: ComboBox comboBox = sender as ComboBox; if (comboBox != null && comboBox.Name.StartsWith("cmb")) { comboBox.Items.Clear(); comboBox.Items.Add(""); if (RunBll == null) return; foreach (var lstOutput in RunBll.dicOutputsMapping) { foreach (var output in lstOutput.Value) comboBox.Items.Add(output); } foreach (var lstInput in RunBll.dicInputsMapping) { foreach (var tuple in lstInput.Value) { string inputs = tuple.Item1; comboBox.Items.Add(inputs); } } } break; } } private void SelectedIndexChanged(object sender, EventArgs e) { switch ((sender as ComboBox).Name) { case "cmbProcess": csv.ProcessName = cmb_Process.Text; break; default: // 更新字典 ComboBox comboBox = sender as ComboBox; if (comboBox != null && comboBox.Name.StartsWith("cmb")) { if (int.TryParse(comboBox.Name.Replace("cmb", ""), out int index)) { if (index < csv.Others.Count) csv.Others[index] = comboBox.Text; else csv.Others.Add(comboBox.Text); } } break; } } private void btnAddMsg_Click(object sender, EventArgs e) { csv.Others.Add(""); this.BeginInvoke(new Action(() => { if (csv.Others.Count > 0) { Single perSpace = 100 / csv.Others.Count; while (tableLayoutPanel1.ColumnCount - 4 < csv.Others.Count) { tableLayoutPanel1.ColumnCount++; tableLayoutPanel1.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, perSpace)); } for (int i = 4; i < tableLayoutPanel1.ColumnCount; i++) { tableLayoutPanel1.ColumnStyles[i].SizeType = SizeType.Percent; tableLayoutPanel1.ColumnStyles[i].Width = perSpace; } int index = 0; foreach (var value in csv.Others) { string key = $"cmb{index}"; if (!dicComboxs.ContainsKey(key)) { ComboBox comboBox = new ComboBox(); comboBox.Name = key; comboBox.MouseClick += MouseClick; comboBox.SelectedIndexChanged += SelectedIndexChanged; comboBox.Text = value; comboBox.Dock = DockStyle.Fill; tableLayoutPanel1.Controls.Add(comboBox, index + 4, 0); dicComboxs.TryAdd(key, comboBox); } else dicComboxs[key].Text = value; index++; } } })); } private void btnDelMsg_Click(object sender, EventArgs e) { if (csv.Others.Count <= 0) return; if (dicComboxs.TryRemove($"cmb{csv.Others.Count - 1}", out ComboBox cmb)) { cmb.MouseClick -= MouseClick; cmb.SelectedIndexChanged -= SelectedIndexChanged; tableLayoutPanel1.Controls.Remove(cmb); csv.Others.RemoveAt(csv.Others.Count - 1); } this.BeginInvoke(new Action(() => { if (csv.Others.Count > 0) { Single perSpace = 100 / csv.Others.Count; tableLayoutPanel1.ColumnCount--; tableLayoutPanel1.ColumnStyles.RemoveAt(tableLayoutPanel1.ColumnStyles.Count - 1); for (int i = 4; i < tableLayoutPanel1.ColumnCount; i++) { tableLayoutPanel1.ColumnStyles[i].SizeType = SizeType.Percent; tableLayoutPanel1.ColumnStyles[i].Width = perSpace; } int index = 0; foreach (var value in csv.Others) { string key = $"cmb{index}"; if (!dicComboxs.ContainsKey(key)) { ComboBox comboBox = new ComboBox(); comboBox.Name = key; comboBox.MouseClick += MouseClick; comboBox.SelectedIndexChanged += SelectedIndexChanged; comboBox.Text = value; comboBox.Dock = DockStyle.Fill; tableLayoutPanel1.Controls.Add(comboBox, index + 4, 0); dicComboxs.TryAdd(key, comboBox); } else dicComboxs[key].Text = value; index++; } } })); } } }