Skip to content

Commit

Permalink
Add ability to edit items in the Invoice Creator
Browse files Browse the repository at this point in the history
  • Loading branch information
soda3x committed Apr 2, 2021
1 parent 28614ff commit 4a96087
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 76 deletions.
77 changes: 39 additions & 38 deletions Moneybags/InvoiceCreator.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 16 additions & 2 deletions Moneybags/InvoiceCreator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@ private Persona GetLoadedPersona()

private void AddItemBtn_Click(object sender, EventArgs e)
{
ItemCreator itemCreator = new ItemCreator(this.itemsListView);
ItemCreator itemCreator = new ItemCreator(this.itemsListView, false);
itemCreator.MinimumSize = itemCreator.Size;
itemCreator.MaximumSize = itemCreator.Size;
itemCreator.Show();
itemCreator.ShowDialog();
}

private void RemoveItemBtn_Click(object sender, EventArgs e)
Expand All @@ -76,6 +76,18 @@ private void RemoveItemBtn_Click(object sender, EventArgs e)
}
}

private void ItemsListView_DoubleClick(object sender, EventArgs e)
{
// Check if Selected Items count > 0 to avoid ArgumentOutOfRange Exception being thrown later
if (this.itemsListView.SelectedItems.Count > 0)
{
ItemCreator itemCreator = new ItemCreator(this.itemsListView, true);
itemCreator.MinimumSize = itemCreator.Size;
itemCreator.MaximumSize = itemCreator.Size;
itemCreator.ShowDialog();
}
}

private void LoadIncompleteInvoiceBtn_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog
Expand All @@ -91,6 +103,8 @@ private void LoadIncompleteInvoiceBtn_Click(object sender, EventArgs e)

if (openFileDialog.ShowDialog() == DialogResult.OK)
{
// Clear any items from the invoice before loading from file
this.itemsListView.Items.Clear();
string invoiceString = File.ReadAllText(openFileDialog.FileName).Trim();
string[] invoiceItemsStr = invoiceString.Split(new[] { Environment.NewLine }, StringSplitOptions.None);

Expand Down
28 changes: 26 additions & 2 deletions Moneybags/ItemCreator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,27 @@ namespace Moneybags
public partial class ItemCreator : MetroFramework.Forms.MetroForm
{
private ListView listView;
public ItemCreator(ListView listView)
private bool editMode;
public ItemCreator(ListView listView, bool editMode)
{
this.listView = listView;
this.editMode = editMode;
InitializeComponent();

addItemBtn.Text = "Add Item to Invoice";

if (editMode)
{
const int ITEM_CODE = 0;
const int DESC = 1;
const int UNIT_PRICE = 2;
const int QTY = 3;
itemCodeTB.Text = this.listView.SelectedItems[0].SubItems[ITEM_CODE].Text;
unitPriceTB.Text = this.listView.SelectedItems[0].SubItems[UNIT_PRICE].Text;
qtyTB.Text = this.listView.SelectedItems[0].SubItems[QTY].Text;
descriptionTB.Text = this.listView.SelectedItems[0].SubItems[DESC].Text;
addItemBtn.Text = "Update Item";
}
}

private void AddItemBtn_Click(object sender, EventArgs e)
Expand All @@ -19,7 +36,14 @@ private void AddItemBtn_Click(object sender, EventArgs e)
string[] row = {this.itemCodeTB.Text, this.descriptionTB.Text,
this.unitPriceTB.Text, this.qtyTB.Text, gst.ToString(), total.ToString()};
ListViewItem listViewItem = new ListViewItem(row);
this.listView.Items.Add(listViewItem);
if (editMode)
{
this.listView.Items[this.listView.SelectedIndices[0]] = listViewItem;
}
else
{
this.listView.Items.Add(listViewItem);
}
this.Dispose();
}
}
Expand Down
24 changes: 12 additions & 12 deletions Moneybags/PersonaCreator.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 5 additions & 22 deletions Moneybags/PersonaCreator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ private Persona CreatePersona(string filePath)
{
path = filePath
};

return newPersona;
}

Expand Down Expand Up @@ -69,27 +68,6 @@ private void SaveAndUsePersonaBtn_Click(object sender, EventArgs e)
Application.Restart();
}

private void SavePersonaToFileBtn_Click(object sender, EventArgs e)
{
SaveFileDialog saveFileDialog = new SaveFileDialog
{
Filter = "Moneybags Persona (*.mbpersona)|*.mbpersona",
RestoreDirectory = true,
Title = "Choose a location to save your new Persona",
DefaultExt = "mbpersona",
CheckPathExists = true,
InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
};

if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
IFormatter formatter = new BinaryFormatter();
Stream stream = new FileStream(saveFileDialog.FileName, FileMode.Create, FileAccess.Write);
formatter.Serialize(stream, this.CreatePersona(saveFileDialog.FileName));
stream.Close();
}
}

private void PrefillDataFromLoadedPersona()
{
FileInfo fileInfo = new FileInfo(@".\currentuser");
Expand Down Expand Up @@ -139,5 +117,10 @@ private void LoadPersonaFromFileBtn_Click(object sender, EventArgs e)
PrefillDataFromLoadedPersona();
}
}

private void SavePersonaToFileBtn_Click(object sender, EventArgs e)
{

}
}
}

0 comments on commit 4a96087

Please sign in to comment.