| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617 | using Resources;using SmartCoalApplication.Base.CommTool;using SmartCoalApplication.Base.FunctionModel;using SmartCoalApplication.Base.MeasureModel;using SmartCoalApplication.Base.SettingModel;using SmartCoalApplication.Core;using SmartCoalApplication.Core.DbOpreate.DbModel;using SmartCoalApplication.Resources;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.Text.RegularExpressions;using System.Threading.Tasks;using System.Windows.Forms;namespace SmartCoalApplication.AutomaticMeasurement{    internal partial class ResultViewNameRule2 : PdnBaseForm    {        private ResultsView resultsView;        private MeasureMaintenanceType measureMaintenanceType;        private string pattern = @"^\d+(\.\d)?$";        private FileNameRule fileNameRule = Program.instance.fileNameRule;        private string filePath = Application.StartupPath + "\\Config\\" + Program.instance.SettingPrefix + "\\FileNameRule.xml";        private bool addFlag = false;        public ResultViewNameRule2(ResultsView resultsView, MeasureMaintenanceType measureMaintenanceType)        {            this.resultsView = resultsView;            this.measureMaintenanceType = measureMaintenanceType;            InitializeComponent();            setLanguege();            this.Icon = PdnInfo.AppIcon;            NameRules();        }        private void setLanguege()        {            this.Text = PdnResources.GetString("FileNamingRules");            this.groupBox19.Text = PdnResources.GetString("FileNamingRules.groupBox19");            this.label29.Text = PdnResources.GetString("FileNamingRules.label29"); // 名称            this.groupBox1.Text = PdnResources.GetString("FileNamingRules.groupBox1");  // 预览            this.label1.Text = PdnResources.GetString("FileNamingRules.label1"); // 名称            this.label14.Text = PdnResources.GetString("FileNamingRules.label14"); // 名称格式            this.label15.Text = PdnResources.GetString("FileNamingRules.label15"); // 数值位数            this.radioButton1.Text = PdnResources.GetString("FileNamingRules.radioButton1"); // 延续        }        #region [命名规则]        /// <summary>        /// 初始化        /// </summary>        public void NameRules()        {            //            // 设置comboBox1的集合和属性            //            comboBox1.Items.AddRange(InvariantData.numberNum);            comboBox1.SelectedIndex = 0;            #region [设置listView1]            listView1.View = View.Details;            listView1.Columns.Add("1", 0, HorizontalAlignment.Center);            listView1.Columns.Add(PdnResources.GetString("Menu.Set.Generalsettings.format.text"), 60, HorizontalAlignment.Center);            listView1.Columns.Add(PdnResources.GetString("Menu.instruction.text"), 316, HorizontalAlignment.Center);            listView1.BeginUpdate();            var dict = InvariantData.parameterList;            foreach (var s in dict)            {                ListViewItem lvi1 = new ListViewItem();                lvi1.Text = s.Key;                lvi1.SubItems.Add(s.Key);                lvi1.SubItems.Add(s.Value);                listView1.Items.Add(lvi1);            }            listView1.EndUpdate();            #endregion [设置listView1]            #region [读取XMl]            listNames.DataSource = fileNameRule.NameRuleList;            listNames.DisplayMember = "Name";            listNames.SelectedIndex = -1;            if (!string.IsNullOrEmpty(this.measureMaintenanceType.NameRuleID))            {                var details = fileNameRule.NameRuleList.Where(m => m.ID.Equals(this.measureMaintenanceType.NameRuleID)).FirstOrDefault();                if (details != null)                {                    this.listNames.SelectedIndex = fileNameRule.NameRuleList.IndexOf(details);                }            }            #endregion        }        /// <summary>        /// 校验名称        /// </summary>        /// <returns></returns>        private bool Inspect()        {            bool check = false;            if (string.IsNullOrEmpty(textBox2.Text))            {                MessageBox.Show(PdnResources.GetString("Menu.pleaseNameformat.text"));                check = true;                return check;            }            else            {                string[] arrs = Regex.Split(textBox2.Text.Trim(), "%");                List<string> strList = InvariantData.parameterList.Keys.ToList();                if (arrs.Length == 1)                {                    if (!textBox2.Text.Contains("%"))                    {                        MessageBox.Show("名稱格式不合法, 請檢查!");                        check = true;                        return check;                    }                }                foreach (string text in arrs)                {                    if (text == "") continue;                    if (!strList.Contains("%" + text))                    {                        MessageBox.Show("名稱格式不合法, 請檢查!");                        check = true;                        return check;                    }                    int index = this.textBox2.Text.IndexOf(text);                    if (index == 0)                    {                        string str = this.textBox2.Text.Substring(0, 1);                        if (str != "%")                        {                            MessageBox.Show("名稱格式不合法, 請檢查!");                            check = true;                            return check;                        }                    }                    else if (index == this.textBox2.Text.Length - 1)                    {                        string str = this.textBox2.Text.Substring(this.textBox2.Text.Length - 2, 1);                        if (str != "%")                        {                            MessageBox.Show("名稱格式不合法, 請檢查!");                            check = true;                            return check;                        }                    }                    else                    {                        string str = this.textBox2.Text.Substring(index - 1, 1);                        string str1 = this.textBox2.Text.Substring(index + 1, 1);                        if (str != "%" && str1 != "%")                        {                            MessageBox.Show("名稱格式不合法, 請檢查!");                            check = true;                            return check;                        }                    }                }                if (Islegal())                {                    MessageBox.Show("文件名稱中不能包含   \\  /  :  *  ?  \" <  >  | ");                    check = true;                    return check;                }            }            if (radioButton1.Checked && !(Regex.IsMatch(textBox3.Text, pattern)))            {                MessageBox.Show(PdnResources.GetString("Menu.Numberscanbeentet.text") + "!");                check = true;            }            return check;        }        /// <summary>        /// 是否符合window命名规则不        /// </summary>        /// <returns></returns>        public bool Islegal()        {            char[] charstr = { '\\', '/', ':', '*', '?', '"', '<', '>', '|' };            char[] textstr = (textBox7.Text.Trim() + textBox8.Text.Trim()).ToString().Trim().ToCharArray();            int count = 0;            for (int i = 0; i < charstr.Length; i++)            {                for (int j = 0; j < textstr.Length; j++)                {                    if (charstr[i] == textstr[j])                    {                        count++;                    }                }            }            if (count > 0)                return true;            else                return false;        }        #endregion        #region [操作]        /// <summary>        /// 取消        /// </summary>        /// <param name="sender"></param>        /// <param name="e"></param>        private void button2_Click(object sender, EventArgs e)        {            this.Close();        }        /// <summary>        /// 新增        /// </summary>        /// <param name="sender"></param>        /// <param name="e"></param>        private void button1_Click(object sender, EventArgs e)        {            addFlag = true;            this.textBox2.TextChanged -= new EventHandler(textBox2_TextChanged);            this.textBox1.Text = string.Empty;            this.textBox4.Text = string.Empty;            this.textBox2.Text = string.Empty;            this.textBox7.Text = string.Empty;            this.textBox8.Text = string.Empty;            this.comboBox1.SelectedIndex = 0;            this.radioButton1.Checked = true;            this.radioButton2.Checked = false;            this.textBox3.Text = "1";            this.textBox5.Text = string.Empty;            this.listNames.SelectedIndex = -1;            this.textBox2.TextChanged += new EventHandler(textBox2_TextChanged);        }        /// <summary>        /// 保存        /// </summary>        /// <param name="sender"></param>        /// <param name="e"></param>        private void btn_Save_Click(object sender, EventArgs e)        {            if (string.IsNullOrEmpty(this.textBox1.Text))            {                MessageBox.Show(PdnResources.GetString("ResultNameView2.InputName"));                return;            }            //先校验            bool pass = Inspect();            if (pass)            {                return;            }            NameRuleListDetails nameRuleListDetails;            string id = Guid.NewGuid().ToString();            string name = this.textBox1.Text;            int index;            if (this.listNames.SelectedItem == null || this.listNames.SelectedIndex == -1)            {                nameRuleListDetails = new NameRuleListDetails();                nameRuleListDetails.ID = id;                var repeatData = fileNameRule.NameRuleList.Where(m => m.Name == name && m.ID != id).FirstOrDefault();                if (repeatData != null)                {                    MessageBox.Show(PdnResources.GetString("ResultNameView2.nameRepeat"));                    return;                }                fileNameRule.NameRuleList.Add(nameRuleListDetails);                index = fileNameRule.NameRuleList.Count - 1;                            }            else            {                index = this.listNames.SelectedIndex;                var nameRuleListDetailsB = (NameRuleListDetails)this.listNames.SelectedItem;                var repeatData = fileNameRule.NameRuleList.Where(m => m.Name == name && m.ID != nameRuleListDetailsB.ID).FirstOrDefault();                if (repeatData != null)                {                    MessageBox.Show(PdnResources.GetString("ResultNameView2.nameRepeat"));                    return;                }                               nameRuleListDetails = fileNameRule.NameRuleList.Where(m => m.ID == nameRuleListDetailsB.ID).FirstOrDefault();                                            }            nameRuleListDetails.Name = this.textBox1.Text;            nameRuleListDetails.Text = this.textBox4.Text;            nameRuleListDetails.NameFormat = this.textBox2.Text;            nameRuleListDetails.ZTextOne = this.textBox7.Text;            nameRuleListDetails.ZTextTwo = this.textBox8.Text;                        try            {                nameRuleListDetails.DigitNum = Convert.ToInt32(this.comboBox1.SelectedItem);            }            catch (Exception ex)            {                nameRuleListDetails.DigitNum = 1;            }            try            {                nameRuleListDetails.ContinueNumber = Convert.ToInt32(this.textBox3.Text);            }            catch (Exception ex)            {                nameRuleListDetails.ContinueNumber = 1;            }            nameRuleListDetails.NowNumber = nameRuleListDetails.ContinueNumber;            nameRuleListDetails.isContinue = this.radioButton1.Checked ? true : false;            if(this.resultsView.getMeasureMaintenanceTypeID() == nameRuleListDetails.ID)            {                resultsView.NameRuleCallBack(nameRuleListDetails);            }            WriteXML();            this.listNames.SelectedIndexChanged -= new System.EventHandler(this.listNames_SelectedChange);            listNames.DataSource = null;            listNames.DataSource = this.fileNameRule.NameRuleList;            listNames.DisplayMember = "Name";            this.listNames.SelectedIndexChanged += new System.EventHandler(this.listNames_SelectedChange);            listNames.SelectedIndex = index;                        MessageBox.Show("保存成功!");        }        /// <summary>        /// 删除        /// </summary>        /// <param name="sender"></param>        /// <param name="e"></param>        private void btn_Del_Click(object sender, EventArgs e)        {            if (this.listNames.SelectedItem == null || this.listNames.SelectedIndex == -1)            {                return;            }            DialogResult result = MessageBox.Show(PdnResources.GetString("ResultViewNameRule2.delete"), PdnResources.GetString("ResultViewNameRule2.deleteTitle"), MessageBoxButtons.OKCancel);            if (result == DialogResult.OK)            {                var nameRuleListDetails = (NameRuleListDetails)this.listNames.SelectedItem;                                this.fileNameRule.NameRuleList.Remove(nameRuleListDetails);                                this.listNames.SelectedIndexChanged -= new System.EventHandler(this.listNames_SelectedChange);                listNames.DataSource = null;                listNames.DataSource = this.fileNameRule.NameRuleList;                listNames.DisplayMember = "Name";                if(this.listNames.Items != null && this.listNames.Items.Count != 0)                {                    NameRuleListDetails newName = (NameRuleListDetails)this.listNames.Items[0];                    this.textBox1.Text = newName.Name;                    this.textBox4.Text = newName.Text;                    this.textBox2.Text = newName.NameFormat;                    this.textBox7.Text = newName.ZTextOne;                    this.textBox8.Text = newName.ZTextTwo;                    this.comboBox1.SelectedIndex = newName.DigitNum - 1;                    this.textBox3.Text = newName.ContinueNumber.ToString();                    this.radioButton1.Checked = nameRuleListDetails.isContinue ? true : false;                    this.radioButton2.Checked = !this.radioButton1.Checked;                    if (nameRuleListDetails.ID == resultsView.getMeasureMaintenanceTypeID())                    {                        resultsView.NameRuleCallBack(null);                    }                }                else                {                    resultsView.NameRuleCallBack(null);                }                                this.listNames.SelectedIndexChanged += new System.EventHandler(this.listNames_SelectedChange);                WriteXML();                if (this.listNames.Items.Count > 0)                {                    listNames.SelectedIndex = 0;                }                else                {                    button1_Click(null, null);                }            }        }        /// <summary>        /// 右侧列表选择        /// </summary>        /// <param name="sender"></param>        /// <param name="e"></param>        private void listNames_SelectedChange(object sender, EventArgs e)        {            if (this.listNames.SelectedItem == null || this.listNames.SelectedIndex == -1)            {                return;            }            NameRuleListDetails nameRuleListDetails = (NameRuleListDetails)this.listNames.SelectedItem;            this.textBox1.Text = nameRuleListDetails.Name;            this.textBox4.Text = nameRuleListDetails.Text;            this.textBox2.Text = nameRuleListDetails.NameFormat;            this.textBox7.Text = nameRuleListDetails.ZTextOne;            this.textBox8.Text = nameRuleListDetails.ZTextTwo;            this.comboBox1.SelectedIndex = nameRuleListDetails.DigitNum - 1;            this.textBox3.Text = nameRuleListDetails.ContinueNumber.ToString();            this.radioButton1.Checked = nameRuleListDetails.isContinue ? true : false;            this.radioButton2.Checked = !this.radioButton1.Checked;        }        /// <summary>        /// 保存XML文件        /// </summary>        private void WriteXML()        {            Program.instance.fileNameRule = this.fileNameRule;            string stageModelXml = XmlSerializeHelper.XmlSerialize<FileNameRule>(fileNameRule);            FileOperationHelper.WriteStringToFile(stageModelXml, filePath, FileMode.Create);        }        /// <summary>        ///         /// </summary>        /// <param name="sender"></param>        /// <param name="e"></param>        private void radioButton2_CheckedChanged(object sender, EventArgs e)        {            if (radioButton2.Checked)            {                textBox3.ReadOnly = true;            }            else            {                textBox3.ReadOnly = false;            }            if (!string.IsNullOrEmpty(textBox2.Text))            {                textBox2_TextChanged(null, null);            }        }        private void listView1_DoubleClick(object sender, EventArgs e)        {            if (this.listView1.FocusedItem != null && this.listView1.FocusedItem.Index != -1)            {                var dict = InvariantData.parameterList;                int start = this.textBox2.SelectionStart;                this.textBox2.Text = this.textBox2.Text.Insert(start, dict.Keys.ToList()[this.listView1.FocusedItem.Index]);                this.textBox2.SelectionStart = this.textBox2.Text.Length;            }        }        #endregion        /// <summary>        /// 预览展示        /// </summary>        /// <param name="sender"></param>        /// <param name="e"></param>        private void textBox2_TextChanged(object sender, EventArgs e)        {            if (string.IsNullOrEmpty(this.textBox2.Text))            {                this.textBox5.Text = string.Empty;                return;            }            //先校验            bool pass = Inspect();            if (pass)            {                return;            }            string ruleName = string.Empty;            string ruleGain = string.Empty;            List<mic_rulers> list = Program.instance.mic_rulersAll;            var ruler = list.Where(m => m.id.Equals(Program.instance.configModel.RulerId)).FirstOrDefault();            if (ruler != null)            {                ruleName = ruler.ruler_name;                ruleGain = ruler.gain_multiple.ToString();            }            NamingRuleHelper namingRuleHelper = new NamingRuleHelper();            NameRuleListDetails nameRuleListDetails = new NameRuleListDetails();            nameRuleListDetails.Name = this.textBox1.Text;            nameRuleListDetails.Text = this.textBox4.Text;            nameRuleListDetails.NameFormat = this.textBox2.Text;            nameRuleListDetails.ZTextOne = this.textBox7.Text;            nameRuleListDetails.ZTextTwo = this.textBox8.Text;            nameRuleListDetails.isContinue = this.radioButton1.Checked;            try            {                nameRuleListDetails.DigitNum = Convert.ToInt32(this.comboBox1.SelectedItem);            }            catch (Exception ex)            {                nameRuleListDetails.DigitNum = 1;            }            try            {                nameRuleListDetails.ContinueNumber = Convert.ToInt32(this.textBox3.Text);            }            catch (Exception ex)            {                nameRuleListDetails.ContinueNumber = 1;            }            nameRuleListDetails.NowNumber = this.radioButton1.Checked ? nameRuleListDetails.ContinueNumber : 1;            this.textBox5.Text = namingRuleHelper.GetNameFromXml(nameRuleListDetails, ruleName, ruleGain, this.measureMaintenanceType, this.filePath);        }        /// <summary>        ///         /// </summary>        /// <param name="sender"></param>        /// <param name="e"></param>        private void text_change(object sender, EventArgs e)        {            if (!string.IsNullOrEmpty(textBox2.Text))            {                textBox2_TextChanged(null, null);            }        }        /// <summary>        /// 应用        /// </summary>        /// <param name="sender"></param>        /// <param name="e"></param>        private void buttonUse_Click(object sender, EventArgs e)        {            if (this.listNames.SelectedItem != null && this.listNames.SelectedIndex != -1)            {                NameRuleListDetails nameRuleListDetails = (NameRuleListDetails)this.listNames.SelectedItem;                MessageBox.Show(PdnResources.GetString("ResultViewNameRule2.Use"));                this.Close();                this.resultsView.NameRuleCallBack(nameRuleListDetails);            }            else if(this.listNames.SelectedItem == null && !addFlag)            {                MessageBox.Show(PdnResources.GetString("ResultViewNameRule2.SelectNameRule"));                return;            }            else            {                MessageBox.Show(PdnResources.GetString("ResultViewNameRule2.SaveThenUse"));                return;            }        }    }}
 |