using Newtonsoft.Json; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using static DefectItemsTool.Form1; namespace DefectItemsTool { public partial class Form1 : Form { public Form1() { InitializeComponent(); } #region 尺寸 List sizeItemParams = new List(); public class SizeItemParam { /// /// 名称 /// public string Name { get; set; } /// /// 代码 /// public string Code { get; set; } /// /// 测试点数 /// public int PointCnt { get; set; } /// /// 处理代码 /// public int[] DefectIndex { get; set; } /// /// RGB颜色代码 /// public int[] RGBcolor { get; set; } } private void button1_Click(object sender, EventArgs e) { SizeItemParam sizeItem = new SizeItemParam(); sizeItem.Name = textBox1.Text; sizeItem.Code = textBox2.Text; sizeItem.PointCnt = (int)numericUpDown1.Value; try { sizeItem.DefectIndex = Array.ConvertAll(textBox3.Text.Split(','), s => int.Parse(s)); if (sizeItem.DefectIndex.Length > sizeItem.PointCnt) { MessageBox.Show("处理代码数据错误,长度大于测试点数!", "警告"); return; } } catch { MessageBox.Show("处理代码格式错误!", "警告"); return; } try { sizeItem.RGBcolor = Array.ConvertAll(textBox4.Text.Split(','), s => int.Parse(s)); if (sizeItem.RGBcolor.Length > 3) { MessageBox.Show("RGB颜色代码数据错误,长度大于3!", "警告"); return; } for (int i = 0; i< sizeItem.RGBcolor.Length; i++) { if(sizeItem.RGBcolor[i] >256) { MessageBox.Show("RGB颜色代码数据错误,不能大于255!", "警告"); return; } } } catch { MessageBox.Show("RGB颜色代码格式错误!", "警告"); return; } if(sizeItemParams.Count >0) { for (int i = 0; i < sizeItemParams.Count; i++) { if (sizeItemParams[i].Name == sizeItem.Name) { MessageBox.Show("名称已存在!", "警告"); return; } if (sizeItemParams[i].Code == sizeItem.Code) { MessageBox.Show("代码已存在!", "警告"); return; } if (Enumerable.SequenceEqual(sizeItemParams[i].DefectIndex , sizeItem.DefectIndex)) { MessageBox.Show("处理代码已存在!", "警告"); return; } if (Enumerable.SequenceEqual(sizeItemParams[i].RGBcolor, sizeItem.RGBcolor)) { MessageBox.Show("RGB颜色代码已存在!", "警告"); return; } } } sizeItemParams.Add(sizeItem); //listBox1.Items.Clear(); listBox1.Items.Add(sizeItem.Name); this.Refresh(); } private void listBox1_MouseDoubleClick(object sender, MouseEventArgs e) { int index = this.listBox1.SelectedIndex; textBox5.Text = JsonConvert.SerializeObject(sizeItemParams[index]); } private void button2_Click(object sender, EventArgs e) { SaveFileDialog dialog = new SaveFileDialog(); dialog.Filter = "Json文件(*.json)|*.json"; //设置文件类型 dialog.FileName = "尺寸测试项"; //设置默认文件名 dialog.DefaultExt = "json"; //设置默认格式(可以不设) //dialog.AddExtension = true; //设置自动在文件名中添加扩展名 if (dialog.ShowDialog() == DialogResult.OK) { File.WriteAllText(dialog.FileName.ToString(), JsonConvert.SerializeObject(sizeItemParams)); } } private void button3_Click(object sender, EventArgs e) { OpenFileDialog openFileDialog1 = new OpenFileDialog(); openFileDialog1.Filter = "Json文件(*.json)|*.json"; //设置文件类型 DialogResult result = openFileDialog1.ShowDialog(); if (result == DialogResult.OK) { string str = File.ReadAllText(openFileDialog1.FileName); sizeItemParams = JsonConvert.DeserializeObject>(str); listBox1.Items.Clear(); for (int i = 0; i < sizeItemParams.Count; i++) listBox1.Items.Add(sizeItemParams[i].Name); this.Refresh(); } } #endregion } }