add context menu, reorganize code

This commit is contained in:
Bitl 2021-08-30 15:56:06 -07:00
parent 567dc6978e
commit 340c3f2dc6
3 changed files with 76 additions and 11 deletions

View File

@ -26,6 +26,7 @@
/// </summary> /// </summary>
private void InitializeComponent() private void InitializeComponent()
{ {
this.components = new System.ComponentModel.Container();
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(XMLContentEditor)); System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(XMLContentEditor));
this.XMLStrip = new System.Windows.Forms.MenuStrip(); this.XMLStrip = new System.Windows.Forms.MenuStrip();
this.fileToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.fileToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
@ -34,8 +35,12 @@
this.partColorsToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.partColorsToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.saveToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.saveToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.XMLView = new System.Windows.Forms.DataGridView(); 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(); this.XMLStrip.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.XMLView)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.XMLView)).BeginInit();
this.ContextMenu.SuspendLayout();
this.SuspendLayout(); this.SuspendLayout();
// //
// XMLStrip // XMLStrip
@ -105,6 +110,29 @@
this.XMLView.Name = "XMLView"; this.XMLView.Name = "XMLView";
this.XMLView.Size = new System.Drawing.Size(800, 426); this.XMLView.Size = new System.Drawing.Size(800, 426);
this.XMLView.TabIndex = 30; 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 // XMLContentEditor
// //
@ -121,6 +149,7 @@
this.XMLStrip.ResumeLayout(false); this.XMLStrip.ResumeLayout(false);
this.XMLStrip.PerformLayout(); this.XMLStrip.PerformLayout();
((System.ComponentModel.ISupportInitialize)(this.XMLView)).EndInit(); ((System.ComponentModel.ISupportInitialize)(this.XMLView)).EndInit();
this.ContextMenu.ResumeLayout(false);
this.ResumeLayout(false); this.ResumeLayout(false);
this.PerformLayout(); this.PerformLayout();
@ -134,4 +163,7 @@
private System.Windows.Forms.ToolStripMenuItem partColorsToolStripMenuItem; private System.Windows.Forms.ToolStripMenuItem partColorsToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem saveToolStripMenuItem; private System.Windows.Forms.ToolStripMenuItem saveToolStripMenuItem;
private System.Windows.Forms.DataGridView XMLView; private System.Windows.Forms.DataGridView XMLView;
private System.Windows.Forms.ContextMenuStrip ContextMenu;
private System.Windows.Forms.ToolStripMenuItem insertRowToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem deleteRowToolStripMenuItem;
} }

View File

@ -24,7 +24,7 @@ public partial class XMLContentEditor : Form
public Provider[] contentProviders; public Provider[] contentProviders;
List<object> loaderList = new List<object>(); List<object> loaderList = new List<object>();
XMLContentType ListType; XMLContentType ListType;
BindingSource XMLDataBinding; private int rowIndex = 0;
#endregion #endregion
#region Constructor #region Constructor
@ -47,6 +47,16 @@ public partial class XMLContentEditor : Form
private void saveToolStripMenuItem_Click(object sender, EventArgs e) 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<Provider> providerList = new List<Provider>(); List<Provider> providerList = new List<Provider>();
List<PartColor> partColorList = new List<PartColor>(); List<PartColor> partColorList = new List<PartColor>();
@ -54,15 +64,6 @@ public partial class XMLContentEditor : Form
{ {
if (data.IsNewRow) continue; 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) switch (ListType)
{ {
case XMLContentType.ContentProviders: 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); MessageBox.Show(fileName + " has been saved! The list will now reload.", "XML Content Editor - File Saved", MessageBoxButtons.OK, MessageBoxIcon.Information);
LoadXML(ListType); 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 #endregion
#region Functions #region Functions
@ -230,4 +259,5 @@ public partial class XMLContentEditor : Form
} }
#endregion #endregion
} }
#endregion #endregion

View File

@ -120,6 +120,9 @@
<metadata name="XMLStrip.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"> <metadata name="XMLStrip.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>17, 17</value> <value>17, 17</value>
</metadata> </metadata>
<metadata name="ContextMenu.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>118, 17</value>
</metadata>
<assembly alias="System.Drawing" name="System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" /> <assembly alias="System.Drawing" name="System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
<data name="$this.Icon" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64"> <data name="$this.Icon" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value> <value>