You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

204 lines
7.5 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Mesnac.Equips.BaseInfo;
namespace Mesnac.Equips.SetConfig.DataConfig
{
public partial class NewDataForm : Form
{
private bool isNew = true;
private ConfigEquipFactory configEquipFactory = null;
public NewDataForm(ConfigEquipFactory configEquipFactory, bool isNew)
{
this.isNew = isNew;
this.configEquipFactory = configEquipFactory;
InitializeComponent();
}
private void NewDataForm_Shown(object sender, EventArgs e)
{
IniComboBox1();
IniDefault();
}
private readonly string _selectDefault = "---请选择---";
private void IniComboBox1()
{
int i = 0;
while (true)
{
i++;
this.textBox1.Text = "D" + i.ToString();
if (!SameDataName(this.textBox1.Text))
{
break;
}
}
this.comboBox1.Items.Clear();
this.comboBox1.Items.Add(_selectDefault);
foreach (string s in Mesnac.Maths.Factory.Instance.AllMaths.Keys)
{
this.comboBox1.Items.Add(s);
}
}
private void IniDefault()
{
if (configEquipFactory.CurrentData == null || isNew)
{
this.textBox3.Text = "0";
this.textBox4.Text = "1";
this.comboBox1.Text = _selectDefault;
this.textBox5.Text = string.Empty;
this.textBox6.Text = string.Empty;
this.textBox7.Text = string.Empty;
this.textBox8.Text = string.Empty;
this.textBoxRunName.Text = string.Empty;
this.checkBox1.Checked = true;
}
else
{
this.textBox1.Text = configEquipFactory.CurrentData.Name.ToString();
this.textBox3.Text = configEquipFactory.CurrentData.Start.ToString();
this.textBox4.Text = configEquipFactory.CurrentData.Len.ToString();
this.comboBox1.Text = Mesnac.Maths.Factory.Instance.GetMethodName(configEquipFactory.CurrentData.Method.ToString());
this.textBox5.Text = Mesnac.Maths.Factory.Instance.GetMethodParameter(configEquipFactory.CurrentData.Method.ToString());
this.textBox6.Text = configEquipFactory.CurrentData.Remark.ToString();
this.textBox7.Text = configEquipFactory.CurrentData.Max.ToString();
this.textBox8.Text = configEquipFactory.CurrentData.Subtractor.ToString();
this.textBoxRunName.Text = configEquipFactory.CurrentData.RunName.ToString();
this.checkBox1.Checked = configEquipFactory.CurrentData.IsSave;
}
}
private void textBox_Int_KeyPress(object sender, KeyPressEventArgs e)
{
//判断按键是不是要输入的类型。
if (((int)e.KeyChar < 48 || (int)e.KeyChar > 57) && (int)e.KeyChar != 8 && (int)e.KeyChar != 46)
{
e.Handled = true;
}
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
Mesnac.Maths.IMath math = null;
Mesnac.Maths.Factory.Instance.AllMaths.TryGetValue(comboBox1.Text.Replace(_selectDefault, string.Empty), out math);
if (math == null)
{
textBox2.Text = string.Empty;
}
else
{
StringBuilder sb = new StringBuilder();
sb.Append("文件:").AppendLine((new System.IO.FileInfo(math.GetType().Assembly.Location)).Name);
sb.Append("类名:").AppendLine(math.GetType().FullName);
sb.AppendLine("说明:").AppendLine(math.Caption);
textBox2.Text = sb.ToString();
}
}
#region 数据保存
private bool SameDataName(string name)
{
foreach (Data data in this.configEquipFactory.CurrentGroup.Data.Values)
{
if (data.Name.Equals(name))
{
if (isNew)
{
return true;
}
else
{
if (data.Name != this.configEquipFactory.CurrentData.Name)
{
return true;
}
}
}
}
return false;
}
private void button1_Click(object sender, EventArgs e)
{
int int_start = 0;
if (!int.TryParse(this.textBox3.Text, out int_start))
{
MessageBox.Show("请设置正确的起始位!");
return;
}
if (int_start < 0)
{
MessageBox.Show("起始位必须大于等于0");
return;
}
int int_len = 0;
if (!int.TryParse(this.textBox4.Text, out int_len))
{
MessageBox.Show("请设置正确的长度!");
return;
}
if (int_len <= 0)
{
MessageBox.Show("长度必须大于0");
return;
}
if (int_start + int_len > this.configEquipFactory.CurrentGroup.Len)
{
MessageBox.Show("数据索引值比读取数据组大!");
return;
}
if (SameDataName(this.textBox1.Text))
{
MessageBox.Show("设备名称重复!");
return;
}
if (this.isNew)
{
configEquipFactory.CurrentData = new Data();
}
else
{
configEquipFactory.CurrentGroup.Data.Remove(configEquipFactory.CurrentData.Name);
}
configEquipFactory.CurrentData.Name = this.textBox1.Text;
configEquipFactory.CurrentData.Start = int_start;
configEquipFactory.CurrentData.Len = int_len;
configEquipFactory.CurrentData.Max = this.textBox7.Text;
configEquipFactory.CurrentData.Subtractor = this.textBox8.Text;
configEquipFactory.CurrentData.RunName = this.textBoxRunName.Text;
configEquipFactory.CurrentData.Method = string.Empty;
configEquipFactory.CurrentData.IsSave = this.checkBox1.Checked;
if (!string.IsNullOrWhiteSpace(comboBox1.Text.Replace(_selectDefault, string.Empty)))
{
configEquipFactory.CurrentData.Method = string.Format("{0}({1})", comboBox1.Text, this.textBox5.Text);
}
configEquipFactory.CurrentData.Remark = this.textBox6.Text;
configEquipFactory.CurrentGroup.Data.Add(configEquipFactory.CurrentData.Name, configEquipFactory.CurrentData);
this.DialogResult = System.Windows.Forms.DialogResult.OK;
this.Close();
}
#endregion
private void button2_Click(object sender, EventArgs e)
{
if (string.IsNullOrWhiteSpace(textBoxRunName.Text))
{
MessageBox.Show("必须填写别名才可以进行设置");
return;
}
new DataFieldForm(textBoxRunName.Text).ShowDialog(this);
}
}
}