From 340c3f2dc659b054e75826d1aa923ffa8121db51 Mon Sep 17 00:00:00 2001 From: Bitl Date: Mon, 30 Aug 2021 15:56:06 -0700 Subject: [PATCH] add context menu, reorganize code --- .../Forms/SDK/XMLContentEditor.Designer.cs | 32 ++++++++++++ .../Forms/SDK/XMLContentEditor.cs | 52 +++++++++++++++---- .../Forms/SDK/XMLContentEditor.resx | 3 ++ 3 files changed, 76 insertions(+), 11 deletions(-) diff --git a/Novetus/NovetusLauncher/Forms/SDK/XMLContentEditor.Designer.cs b/Novetus/NovetusLauncher/Forms/SDK/XMLContentEditor.Designer.cs index 9f2eb58..1ab5c3e 100644 --- a/Novetus/NovetusLauncher/Forms/SDK/XMLContentEditor.Designer.cs +++ b/Novetus/NovetusLauncher/Forms/SDK/XMLContentEditor.Designer.cs @@ -26,6 +26,7 @@ /// private void InitializeComponent() { + this.components = new System.ComponentModel.Container(); System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(XMLContentEditor)); this.XMLStrip = new System.Windows.Forms.MenuStrip(); this.fileToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); @@ -34,8 +35,12 @@ this.partColorsToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.saveToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.XMLView = new System.Windows.Forms.DataGridView(); + this.ContextMenu = new System.Windows.Forms.ContextMenuStrip(this.components); + this.insertRowToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.deleteRowToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.XMLStrip.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.XMLView)).BeginInit(); + this.ContextMenu.SuspendLayout(); this.SuspendLayout(); // // XMLStrip @@ -105,6 +110,29 @@ this.XMLView.Name = "XMLView"; this.XMLView.Size = new System.Drawing.Size(800, 426); this.XMLView.TabIndex = 30; + this.XMLView.CellMouseUp += new System.Windows.Forms.DataGridViewCellMouseEventHandler(this.XMLView_CellMouseUp); + // + // ContextMenu + // + this.ContextMenu.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { + this.insertRowToolStripMenuItem, + this.deleteRowToolStripMenuItem}); + this.ContextMenu.Name = "contextMenuStrip1"; + this.ContextMenu.Size = new System.Drawing.Size(134, 48); + // + // insertRowToolStripMenuItem + // + this.insertRowToolStripMenuItem.Name = "insertRowToolStripMenuItem"; + this.insertRowToolStripMenuItem.Size = new System.Drawing.Size(133, 22); + this.insertRowToolStripMenuItem.Text = "Insert Row"; + this.insertRowToolStripMenuItem.Click += new System.EventHandler(this.insertRowToolStripMenuItem_Click); + // + // deleteRowToolStripMenuItem + // + this.deleteRowToolStripMenuItem.Name = "deleteRowToolStripMenuItem"; + this.deleteRowToolStripMenuItem.Size = new System.Drawing.Size(133, 22); + this.deleteRowToolStripMenuItem.Text = "Delete Row"; + this.deleteRowToolStripMenuItem.Click += new System.EventHandler(this.deleteRowToolStripMenuItem_Click); // // XMLContentEditor // @@ -121,6 +149,7 @@ this.XMLStrip.ResumeLayout(false); this.XMLStrip.PerformLayout(); ((System.ComponentModel.ISupportInitialize)(this.XMLView)).EndInit(); + this.ContextMenu.ResumeLayout(false); this.ResumeLayout(false); this.PerformLayout(); @@ -134,4 +163,7 @@ private System.Windows.Forms.ToolStripMenuItem partColorsToolStripMenuItem; private System.Windows.Forms.ToolStripMenuItem saveToolStripMenuItem; private System.Windows.Forms.DataGridView XMLView; + private System.Windows.Forms.ContextMenuStrip ContextMenu; + private System.Windows.Forms.ToolStripMenuItem insertRowToolStripMenuItem; + private System.Windows.Forms.ToolStripMenuItem deleteRowToolStripMenuItem; } \ No newline at end of file diff --git a/Novetus/NovetusLauncher/Forms/SDK/XMLContentEditor.cs b/Novetus/NovetusLauncher/Forms/SDK/XMLContentEditor.cs index 8add1d9..c7ade7e 100644 --- a/Novetus/NovetusLauncher/Forms/SDK/XMLContentEditor.cs +++ b/Novetus/NovetusLauncher/Forms/SDK/XMLContentEditor.cs @@ -24,7 +24,7 @@ public partial class XMLContentEditor : Form public Provider[] contentProviders; List loaderList = new List(); XMLContentType ListType; - BindingSource XMLDataBinding; + private int rowIndex = 0; #endregion #region Constructor @@ -47,6 +47,16 @@ public partial class XMLContentEditor : Form private void saveToolStripMenuItem_Click(object sender, EventArgs e) { + //https://stackoverflow.com/questions/37145086/datagridview-remove-empty-rows-button + for (int i = XMLView.Rows.Count - 1; i > -1; i--) + { + DataGridViewRow row = XMLView.Rows[i]; + if (!row.IsNewRow && row.Cells[0].Value == null) + { + XMLView.Rows.RemoveAt(i); + } + } + List providerList = new List(); List partColorList = new List(); @@ -54,15 +64,6 @@ public partial class XMLContentEditor : Form { if (data.IsNewRow) continue; - //https://stackoverflow.com/questions/8255186/how-to-check-empty-and-null-cells-in-datagridview-using-c-sharp - for (int i = 0; i < data.Cells.Count; i++) - { - if (data.Cells[i].Value == null || data.Cells[i].Value == DBNull.Value || string.IsNullOrWhiteSpace(data.Cells[i].Value.ToString())) - { - continue; - } - } - switch (ListType) { case XMLContentType.ContentProviders: @@ -142,6 +143,34 @@ public partial class XMLContentEditor : Form MessageBox.Show(fileName + " has been saved! The list will now reload.", "XML Content Editor - File Saved", MessageBoxButtons.OK, MessageBoxIcon.Information); LoadXML(ListType); } + + //http://csharp.net-informations.com/datagridview/deletegridview.htm + private void XMLView_CellMouseUp(object sender, DataGridViewCellMouseEventArgs e) + { + if (e.Button == MouseButtons.Right) + { + XMLView.Rows[e.RowIndex].Selected = true; + rowIndex = e.RowIndex; + XMLView.CurrentCell = XMLView.Rows[e.RowIndex].Cells[1]; + ContextMenu.Show(Cursor.Position); + } + } + + private void deleteRowToolStripMenuItem_Click(object sender, EventArgs e) + { + if (!XMLView.Rows[rowIndex].IsNewRow) + { + XMLView.Rows.RemoveAt(rowIndex); + } + } + + private void insertRowToolStripMenuItem_Click(object sender, EventArgs e) + { + if (!XMLView.Rows[rowIndex].IsNewRow) + { + XMLView.Rows.Insert(rowIndex, 1); + } + } #endregion #region Functions @@ -230,4 +259,5 @@ public partial class XMLContentEditor : Form } #endregion } -#endregion \ No newline at end of file +#endregion + diff --git a/Novetus/NovetusLauncher/Forms/SDK/XMLContentEditor.resx b/Novetus/NovetusLauncher/Forms/SDK/XMLContentEditor.resx index 3aa8969..776c5c0 100644 --- a/Novetus/NovetusLauncher/Forms/SDK/XMLContentEditor.resx +++ b/Novetus/NovetusLauncher/Forms/SDK/XMLContentEditor.resx @@ -120,6 +120,9 @@ 17, 17 + + 118, 17 +