using LB_VisionProcesses; using VisionControl.Forms; namespace LB_VisionProcesses.Forms { public partial class GlobalVarControl : UserControl { NumericUpDown numericUpDown = new NumericUpDown(); CheckBox checkBox = new CheckBox(); TextBox textBox = new TextBox(); ComboBox comboBox = new ComboBox(); public bool isSelected { get { return ckbLabel.Checked; } } public string Key { get { return ckbLabel.Text; } } public object Value { get { string type = cmbType.Text; switch (type) { case "int": return Convert.ToInt32(numericUpDown.Value); case "double": return Convert.ToDouble(numericUpDown.Value); case "bool": return checkBox.Checked; case "List": return textBox.Text.Split(',').ToList(); case "string": default: return textBox.Text; } } } public GlobalVarControl(string key, object value = null) { InitializeComponent(); ckbLabel.Text = key; if (value == null) { cmbType.Text = "string"; return; } Type type = value?.GetType(); switch (type) { case Type t when t == typeof(int): cmbType.Text = "int"; numericUpDown.Value = Convert.ToDecimal((int)value); break; case Type t when t == typeof(double): cmbType.Text = "double"; numericUpDown.Value = Convert.ToDecimal((double)value); break; case Type t when t == typeof(bool): cmbType.Text = "bool"; checkBox.Checked = Convert.ToBoolean(value); break; case Type t when t == typeof(List): cmbType.Text = "string"; textBox.Text = string.Join(',', value as List); break; default: cmbType.Text = "string"; textBox.Text = value.ToString(); break; } } private void cmbType_SelectedIndexChanged(object sender, EventArgs e) { numericUpDown.Visible = false; checkBox.Visible = false; textBox.Visible = false; comboBox.Visible = false; string type = cmbType.Text; switch (type) { case "int": numericUpDown.Increment = 1; numericUpDown.Value = 0; tableLayoutPanel1.Controls.Add(numericUpDown, 2, 0); numericUpDown.Dock = DockStyle.Fill; numericUpDown.Visible = true; numericUpDown.Maximum = 999; numericUpDown.Minimum = -999; break; case "double": numericUpDown.Increment = Convert.ToDecimal(0.1); numericUpDown.Value = Convert.ToDecimal(0.0); tableLayoutPanel1.Controls.Add(numericUpDown, 2, 0); numericUpDown.Dock = DockStyle.Fill; numericUpDown.Visible = true; numericUpDown.Maximum = 999; numericUpDown.Minimum = -999; break; case "bool": checkBox.Checked = false; checkBox.Text = ""; tableLayoutPanel1.Controls.Add(checkBox, 2, 0); checkBox.Dock = DockStyle.Fill; checkBox.Visible = true; break; case "List": case "string": default: textBox.Text = ""; tableLayoutPanel1.Controls.Add(textBox, 2, 0); textBox.Dock = DockStyle.Fill; textBox.Visible = true; break; } } private System.Windows.Forms.ToolTip toolTip = new System.Windows.Forms.ToolTip(); private void ShowToolTip(Control control, string message) => toolTip.SetToolTip(control, message); private void ckbLabel_MouseDown(object sender, MouseEventArgs e) { if (e.Button.ToString() == "Right") { if (ckbLabel.Text.Contains(".")) { MessageBox.Show($"变量【{ckbLabel.Text}】不允许重命名!", "异常"); return; } RenameForm renameForm = new RenameForm(ckbLabel.Text); renameForm.ShowDialog(); if (renameForm.bRename) { string oriName = renameForm.strOriName; string newName = renameForm.strNewName; if (IProcess.dicGlobalVars.ContainsKey(newName)) MessageBox.Show($"变量【{newName}】已存在,请更换名称", "异常"); else { if (!IProcess.dicGlobalVars.TryRemove(oriName, out object obj) || !IProcess.dicGlobalVars.TryAdd(newName, obj)) { MessageBox.Show($"重命名为【{newName}】失败!", "异常"); return; } ckbLabel.Text = newName; } } } } } }