using OTS.WinFormsUI.Docking; using SourceGrid; using System; 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 OTSPartA_STDEditor { public partial class STDRuleslist : DockContent { public int PreRow = 1;//之前选中的位置 public Form_Main m_MainForm = null; ValueChangedEvent m_ValueChangedEvent = null;//单元格内容改变事件 public STDRuleslist(Form_Main mainForm) { InitializeComponent(); m_MainForm = mainForm; m_ValueChangedEvent = new ValueChangedEvent(this); } System.Collections.Hashtable table_STDRuleslist; private void STDRuleslist_Load(object sender, EventArgs e) { m_MainForm.lan = new Language(this); table_STDRuleslist = m_MainForm.lan.GetNameTable("Attributes"); Grid_Minerals.Redim(m_MainForm.STDDictionary.Count + 1, 2); SourceGrid.Cells.ColumnHeader head1 = null; if (m_MainForm.lan.GetNameTable("Form_ConstantsEditor2")["language"].ToString() == "EN") { head1 = new SourceGrid.Cells.ColumnHeader("Rule Name"); } else { head1 = new SourceGrid.Cells.ColumnHeader("规则名称"); } Grid_Minerals[0, 0] = head1; SourceGrid.Cells.ColumnHeader head2 = null; if (m_MainForm.lan.GetNameTable("Form_ConstantsEditor2")["language"].ToString() == "EN") { head2 = new SourceGrid.Cells.ColumnHeader("Color"); } else { head2 = new SourceGrid.Cells.ColumnHeader("颜色"); } Grid_Minerals[0, 1] = head2; SourceGrid.Cells.Views.ColumnHeader boldHeader = new SourceGrid.Cells.Views.ColumnHeader(); boldHeader.Font = new Font("Microsoft YaHei UI", 11, FontStyle.Bold); boldHeader.TextAlignment = DevAge.Drawing.ContentAlignment.MiddleCenter; Grid_Minerals[0, 0].View = boldHeader; Grid_Minerals[0, 1].View = boldHeader; Grid_Minerals.Rows.SetHeight(0, 25); head1.AutomaticSortEnabled = false; head2.AutomaticSortEnabled = false; Grid_Minerals.Selection.EnableMultiSelection = false; Grid_Minerals.AutoStretchColumnsToFitWidth = true; Grid_Minerals.Columns[0].Width = this.Width/2-10; Grid_Minerals.Columns[1].Width = this.Width/2-20; int i = 1; foreach (KeyValuePair kv in m_MainForm.STDDictionary) { Grid_Minerals[i, 0] = new SourceGrid.Cells.Cell(kv.Value.StrName, typeof(string)); Grid_Minerals.Rows[i].Height = 25; Grid_Minerals[i, 0].Tag = kv.Key; Grid_Minerals[i, 1] = new SourceGrid.Cells.Cell(); SourceGrid.Cells.Views.Cell view = new SourceGrid.Cells.Views.Cell(); view.BackColor = colorHx16toRGB(kv.Value.Color); Grid_Minerals[i, 1].View = view; i++; } Grid_Minerals.Controller.AddController(m_ValueChangedEvent); Grid_Minerals.FixedRows = 1;// 第一行是列标题不可以滚动 Grid_Minerals.Selection.FocusStyle = FocusStyle.None; button_UpOrder.Enabled = false; button_DownOrder.Enabled = false; } /// /// [颜色:16进制转成RGB] /// /// 设置16进制颜色 [返回RGB] /// public static System.Drawing.Color colorHx16toRGB(string strHxColor) { try { if (strHxColor.Length == 0) {//如果为空 return System.Drawing.Color.FromArgb(255, 255, 204);//设为白色 } else {//转换颜色 return System.Drawing.Color.FromArgb(System.Int32.Parse(strHxColor.Substring(1, 2), System.Globalization.NumberStyles.AllowHexSpecifier), System.Int32.Parse(strHxColor.Substring(3, 2), System.Globalization.NumberStyles.AllowHexSpecifier), System.Int32.Parse(strHxColor.Substring(5, 2), System.Globalization.NumberStyles.AllowHexSpecifier)); } } catch {//设为白色 return System.Drawing.Color.FromArgb(255, 255, 204); } } private void MineralsGrid_Click(object sender, EventArgs e) { SourceGrid.Grid ls_gd = (SourceGrid.Grid)sender; ls_gd.Focus(); int i = ls_gd.Selection.ActivePosition.Row; int j = ls_gd.Selection.ActivePosition.Column; //string jj =m_MainForm.m_Attributes.Grid_Attributes[10, 1].Value.ToString(); /// 保证鼠标点击的GRID行和列是有效的 if (i >= 0 && j >= 0) { //规则名称不为空 if (Grid_Minerals[i, 0].Value.ToString().Replace(" ", "").Trim() != "") { if (m_MainForm.CheckAttributes() && m_MainForm.Checktextbox_STDEditor()) { m_MainForm.SaveDataOfSelRule(PreRow, 0); m_MainForm.ChangeSTDEditorAndGrid_Attributes(int.Parse(Grid_Minerals[i, 0].Tag.ToString())); m_MainForm.m_SubMidWindow.m_ComparativeLibrary.MakeCheckboxUnchecked(); PreRow = i; } else { Position pos = new Position(PreRow, 0); Grid_Minerals.Selection.Focus(pos, true); m_MainForm.m_SubMidWindow.m_ComparativeLibrary.MakeCheckboxUnchecked(); } } else { Position pos = new Position(PreRow, 0); Grid_Minerals.Selection.Focus(pos, true); m_MainForm.m_SubMidWindow.m_ComparativeLibrary.MakeCheckboxUnchecked(); } } SetOrderButtonsStatus(); } /// /// 新建 /// /// /// private void ToolStripMenuItem_New_Click(object sender, EventArgs e) { int i = Grid_Minerals.Selection.ActivePosition.Row; int j = Grid_Minerals.Selection.ActivePosition.Column; /// 保证鼠标点击的GRID行和列是有效的 if (i >= 0 && j >= 0) { //规则名称不为空 if (Grid_Minerals[i, j].Value.ToString().Replace(" ", "").Trim() != "") { if (m_MainForm.CheckAttributes()) { if (m_MainForm.Checktextbox_STDEditor()) { m_MainForm.SaveDataOfSelRule(i, j); int STDId = m_MainForm.AddSTDDictionaryItem(); AddNewRow(STDId, "NewRuleName", Color.Gray); m_MainForm.m_SubMidWindow.m_ComparativeLibrary.MakeCheckboxUnchecked(); } } } } else { int STDId = m_MainForm.AddSTDDictionaryItem(); AddNewRow(STDId, "NewRuleName", Color.Gray); m_MainForm.m_SubMidWindow.m_ComparativeLibrary.MakeCheckboxUnchecked(); } //SetOrderButtonsStatus(); } public void AddNewRow(int STDId, string RuleName, Color color) { Grid_Minerals.Rows.Insert(Grid_Minerals.Rows.Count); Grid_Minerals[Grid_Minerals.Rows.Count - 1, 0] = new SourceGrid.Cells.Cell(RuleName, typeof(string)); Grid_Minerals[Grid_Minerals.Rows.Count - 1, 0].Tag = STDId; Grid_Minerals.Rows[Grid_Minerals.Rows.Count - 1].Height = 25; Grid_Minerals[Grid_Minerals.Rows.Count - 1, 1] = new SourceGrid.Cells.Cell(); //Grid_Minerals[Grid_Minerals.Rows.Count - 1, 1].AddController(SourceGrid.Cells.Controllers.Unselectable.Default); m_MainForm.STDDictionary[STDId].StrName = RuleName; m_MainForm.ChangeSTDEditorAndGrid_Attributes(STDId); SourceGrid.Cells.Views.Cell view = new SourceGrid.Cells.Views.Cell(); view.BackColor = color; Grid_Minerals[Grid_Minerals.Rows.Count - 1, 1].View = view; Position pos = new Position(Grid_Minerals.Rows.Count - 1, 0); Grid_Minerals.Selection.Focus(pos, true); PreRow = Grid_Minerals.Rows.Count - 1; SetOrderButtonsStatus(); } void Grid_MineralsDelRow() { int x = Grid_Minerals.Selection.ActivePosition.Row; if (x > 0) { m_MainForm.RemoveSTDDictionaryItem((int)Grid_Minerals[x, 0].Tag); m_MainForm.m_SubMidWindow.m_STDEditor.DelSTDXray((int)Grid_Minerals[x, 0].Tag); Grid_Minerals.Rows.Remove(x); if (Grid_Minerals.RowsCount > 1) { if (x != Grid_Minerals.RowsCount) { Position pos = new Position(x, 0); Grid_Minerals.Selection.Focus(pos, true); Grid_Minerals[x, 0].Grid.Select(); m_MainForm.ChangeSTDEditorAndGrid_Attributes(int.Parse(Grid_Minerals[x, 0].Tag.ToString())); PreRow = x; } else { Position pos = new Position(x - 1, 0); Grid_Minerals.Selection.Focus(pos, true); Grid_Minerals[x - 1, 0].Grid.Select(); m_MainForm.ChangeSTDEditorAndGrid_Attributes(int.Parse(Grid_Minerals[x - 1, 0].Tag.ToString())); PreRow = x - 1; } } else { m_MainForm.SetNull(); PreRow = -1; } } else { MessageBox.Show("There is no rule to delete!", "Tip"); } SetOrderButtonsStatus(); } private void ToolStripMenuItem_Del_Click(object sender, EventArgs e) { Grid_MineralsDelRow(); } public class ValueChangedEvent : SourceGrid.Cells.Controllers.ControllerBase { protected STDRuleslist m_parentWnd = null; public ValueChangedEvent(STDRuleslist parentWnd) { m_parentWnd = parentWnd; } public override void OnValueChanged(SourceGrid.CellContext sender, EventArgs e) { base.OnValueChanged(sender, e); if (sender.Value != null&& sender.Value.ToString().Replace(" ", "").Trim() != "") { m_parentWnd.ChangeStrName(sender.Position.Row, sender.Value.ToString()); } else { m_parentWnd.ChangeStrName(sender.Position.Row, "NewRuleName"); m_parentWnd.Grid_Minerals[sender.Position.Row,0].Value = "NewRuleName"; } } } public void ChangeStrName(int RowNum, string NewStrName) { m_MainForm.ChangeStrName(RowNum, NewStrName); } private void Grid_Minerals_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e) { SourceGrid.Grid ls_gd = (SourceGrid.Grid)sender; ls_gd.Focus(); int i = ls_gd.Selection.ActivePosition.Row; int j = ls_gd.Selection.ActivePosition.Column; if(i==-1) { return; } if (e.KeyCode == Keys.Up) { /// 保证鼠标点击的GRID行和列是有效的 if (i >= 2 && j >= 0) { //规则名称不为空 if (Grid_Minerals[i, 0].Value.ToString().Replace(" ", "").Trim() != "") { if (m_MainForm.CheckAttributes() && m_MainForm.Checktextbox_STDEditor()) { m_MainForm.SaveDataOfSelRule(i, 0); m_MainForm.ChangeSTDEditorAndGrid_Attributes(int.Parse(Grid_Minerals[i - 1, 0].Tag.ToString())); m_MainForm.m_SubMidWindow.m_ComparativeLibrary.MakeCheckboxUnchecked(); PreRow = i; if (i == 2) { button_UpOrder.Enabled = false; } if (i == ls_gd.RowsCount - 1) { button_DownOrder.Enabled = true; } } else { Position pos = new Position(i + 1, 0); Grid_Minerals.Selection.Focus(pos, true); m_MainForm.m_SubMidWindow.m_ComparativeLibrary.MakeCheckboxUnchecked(); } } else { Position pos = new Position(i + 1, 0); Grid_Minerals.Selection.Focus(pos, true); m_MainForm.m_SubMidWindow.m_ComparativeLibrary.MakeCheckboxUnchecked(); } } } if (e.KeyCode == Keys.Down) { if (i < ls_gd.RowsCount - 1 && j >= 0) { //规则名称不为空 if (Grid_Minerals[i, 0].Value.ToString().Replace(" ", "").Trim() != "") { if (m_MainForm.CheckAttributes()&& m_MainForm.Checktextbox_STDEditor()) { m_MainForm.SaveDataOfSelRule(i, 0); m_MainForm.ChangeSTDEditorAndGrid_Attributes(int.Parse(Grid_Minerals[i + 1, 0].Tag.ToString())); PreRow = i; m_MainForm.m_SubMidWindow.m_ComparativeLibrary.MakeCheckboxUnchecked(); if (i == 1) { button_UpOrder.Enabled = true; } if (i == ls_gd.RowsCount - 2) { button_DownOrder.Enabled = false; } } else { Position pos = new Position(i - 1, 0); Grid_Minerals.Selection.Focus(pos, true); m_MainForm.m_SubMidWindow.m_ComparativeLibrary.MakeCheckboxUnchecked(); } } else { Position pos = new Position(i - 1, 0); Grid_Minerals.Selection.Focus(pos, true); m_MainForm.m_SubMidWindow.m_ComparativeLibrary.MakeCheckboxUnchecked(); } } } if (e.KeyCode == Keys.Delete) { Grid_MineralsDelRow(); } } public int ChangeSTDRulesLISTBackColor() { if (Grid_Minerals.RowsCount > 1) { Position pos = new Position(1, 0); Grid_Minerals[1, 0].Grid.Select(); Grid_Minerals[Grid_Minerals.Selection.ActivePosition.Row, 1].View.BackColor = m_MainForm.m_Attributes.Grid_Attributes[3, 1].View.BackColor; m_MainForm.STDDictionary[(int)Grid_Minerals[Grid_Minerals.Selection.ActivePosition.Row, 0].Tag].Color= Attributes.colorRGBtoHx16(m_MainForm.m_Attributes.Grid_Attributes[3, 1].View.BackColor.R, m_MainForm.m_Attributes.Grid_Attributes[3, 1].View.BackColor.G, m_MainForm.m_Attributes.Grid_Attributes[3, 1].View.BackColor.B); Grid_Minerals.Refresh(); return (int)Grid_Minerals[Grid_Minerals.Selection.ActivePosition.Row, 0].Tag; } else { return -1; } } private void MenuStrip_STDRulelist_Resize(object sender, EventArgs e) { } private void button_UpOrder_Click(object sender, EventArgs e) { Grid_Minerals.Focus(true); int selrow = Grid_Minerals.Selection.ActivePosition.Row; int id = (int)Grid_Minerals[selrow, 0].Tag; int id2 = (int)Grid_Minerals[selrow - 1, 0].Tag; string sname = Grid_Minerals[selrow, 0].Value.ToString(); Grid_Minerals[selrow, 0].Value = Grid_Minerals[selrow - 1, 0].Value; Grid_Minerals[selrow, 0].Tag = id2; SourceGrid.Cells.Views.Cell view = new SourceGrid.Cells.Views.Cell(); view.BackColor = colorHx16toRGB(m_MainForm.STDDictionary[id2].Color); Grid_Minerals[selrow, 1].View = view; Grid_Minerals[selrow - 1, 0].Value = sname; Grid_Minerals[selrow - 1, 0].Tag = id; SourceGrid.Cells.Views.Cell view2 = new SourceGrid.Cells.Views.Cell(); view2.BackColor = colorHx16toRGB(m_MainForm.STDDictionary[id].Color); Grid_Minerals[selrow - 1, 1].View = view2; m_MainForm.STDDictionary[id].StrName = Grid_Minerals[selrow - 1, 0].Value.ToString(); m_MainForm.STDDictionary[id2].StrName = Grid_Minerals[selrow, 0].Value.ToString(); Position pos = new Position(selrow - 1, 0); Grid_Minerals[selrow - 1, 0].Grid.Select(); Grid_Minerals.Selection.Focus(pos, true); PreRow = selrow - 1; button_DownOrder.Enabled = true; if (selrow - 1 == 1) { button_UpOrder.Enabled = false; } m_MainForm.IsModified = true; //m_MainForm.ChangeSTDEditorAndGrid_Attributes(int.Parse(Grid_Minerals[selrow-1, 0].Tag.ToString())); } private void button_DownOrder_Click(object sender, EventArgs e) { Grid_Minerals.Focus(true); int selrow = Grid_Minerals.Selection.ActivePosition.Row; int id = (int)Grid_Minerals[selrow, 0].Tag; int id2 = (int)Grid_Minerals[selrow + 1, 0].Tag; string sname = Grid_Minerals[selrow, 0].Value.ToString(); Grid_Minerals[selrow, 0].Value = Grid_Minerals[selrow + 1, 0].Value; Grid_Minerals[selrow, 0].Tag = id2; SourceGrid.Cells.Views.Cell view = new SourceGrid.Cells.Views.Cell(); view.BackColor = colorHx16toRGB(m_MainForm.STDDictionary[id2].Color); Grid_Minerals[selrow, 1].View = view; Grid_Minerals[selrow + 1, 0].Value = sname; Grid_Minerals[selrow + 1, 0].Tag = id; SourceGrid.Cells.Views.Cell view2 = new SourceGrid.Cells.Views.Cell(); view2.BackColor = colorHx16toRGB(m_MainForm.STDDictionary[id].Color); Grid_Minerals[selrow + 1, 1].View = view2; m_MainForm.STDDictionary[id].StrName = Grid_Minerals[selrow + 1, 0].Value.ToString(); m_MainForm.STDDictionary[id2].StrName = Grid_Minerals[selrow, 0].Value.ToString(); Position pos = new Position(selrow + 1, 0); Grid_Minerals[selrow + 1, 0].Grid.Select(); Grid_Minerals.Selection.Focus(pos, true); PreRow = selrow + 1; button_UpOrder.Enabled = true; if (selrow + 1 == Grid_Minerals.RowsCount - 1) { button_DownOrder.Enabled = false; } #region 添加修改stdid顺序 ,由于需求改变只完成一部分(差相应修改能谱部分) //DialogResult result=MessageBox.Show("检测到顺序已修改,是否调整STDId?", "提示",MessageBoxButtons.YesNo, MessageBoxIcon.Warning); //if (result == DialogResult.Yes) //{ // ChangeSTDidFromNum changeSTDidFromNum = new ChangeSTDidFromNum(); // DialogResult res=changeSTDidFromNum.ShowDialog(); // { // if (res == DialogResult.OK) // { // int StartId = int.Parse(changeSTDidFromNum.textBox_NewFirstSTDId.Text); // Dictionary STDDictionaryCopy = new Dictionary(); // for (int i = 0; i < Grid_Minerals.RowsCount-1; i++) // { // STDdata dataCopy = m_MainForm.STDDictionary[(int)Grid_Minerals[i+1, 0].Tag]; // STDDictionaryCopy.Add(StartId + i, dataCopy); // } // m_MainForm.STDDictionary.Clear(); // m_MainForm.STDDictionary = STDDictionaryCopy; // } // } //} #endregion m_MainForm.IsModified = true; } public void InsertNewRow(int STDId, string RuleName, Color color) { Grid_Minerals.Focus(true); Grid_Minerals.Rows.InsertRange(Grid_Minerals.Selection.ActivePosition.Row, 1); //Grid_Minerals.Rows.Insert(Grid_Minerals.Rows.Count); int i = Grid_Minerals.Selection.ActivePosition.Row; Grid_Minerals[Grid_Minerals.Selection.ActivePosition.Row, 0] = new SourceGrid.Cells.Cell(RuleName, typeof(string)); Grid_Minerals[Grid_Minerals.Selection.ActivePosition.Row, 0].Tag = STDId; Grid_Minerals.Rows[Grid_Minerals.Selection.ActivePosition.Row].Height = 25; Grid_Minerals[Grid_Minerals.Selection.ActivePosition.Row, 1] = new SourceGrid.Cells.Cell(); m_MainForm.ChangeSTDEditorAndGrid_Attributes(STDId); Grid_Minerals.Focus(true); SourceGrid.Cells.Views.Cell view = new SourceGrid.Cells.Views.Cell(); view.BackColor = color; Grid_Minerals[Grid_Minerals.Selection.ActivePosition.Row, 1].View = view; Position pos = new Position(Grid_Minerals.Selection.ActivePosition.Row, 0); Grid_Minerals.Selection.Focus(pos, true); PreRow = Grid_Minerals.Selection.ActivePosition.Row - 1; SetOrderButtonsStatus(); } private void Grid_Minerals_VScrollPositionChanged(object sender, ScrollPositionChangedEventArgs e) { //Grid_Minerals.Refresh(); } void SetOrderButtonsStatus() { Grid_Minerals.Focus(); int i = Grid_Minerals.Selection.ActivePosition.Row; int j = Grid_Minerals.Selection.ActivePosition.Column; if (i != -1) { if (i == 1) { button_UpOrder.Enabled = false; } else { button_UpOrder.Enabled = true; } if (i == Grid_Minerals.RowsCount - 1) { button_DownOrder.Enabled = false; } else { button_DownOrder.Enabled = true; } } else { button_UpOrder.Enabled = false; button_DownOrder.Enabled = false; } } } }