C
C#15mo ago
u41c

Inventory Maintence Project Issue

I have two forms, one that lets you enter data for a new item. Once you hit Save, it should create a new InvItem object and add it to a list of inventory items and display it in the list. I can't seem to figure out how to get it to add the item. Any suggestions?
13 Replies
Angius
Angius15mo ago
someListOfItems.Add(newItem)
u41c
u41cOP15mo ago
There's a helper class called InvItemDB.cs and I'm somehow supposed to use a method from it to add the item and save it.
u41c
u41cOP15mo ago
Pastebin
InvItemDB.cs - Pastebin.com
Pastebin.com is the number one paste tool since 2002. Pastebin is a website where you can store text online for a set period of time.
u41c
u41cOP15mo ago
Here's the new item form
using System;
using System.Windows.Forms;

namespace InventoryMaintenance
{
public partial class frmNewItem : Form
{
public frmNewItem()
{
InitializeComponent();
}

InvItem invItem = null;

public InvItem GetNewItem()
{
if (IsValidData())
{
int newItemNumber = int.Parse(txtItemNo.Text);
string newDescription = txtDescription.Text;
decimal newPrice = decimal.Parse(txtPrice.Text);

return new InvItem(newItemNumber, newDescription, newPrice);
}

return null;
}

private bool IsValidData()
{
return Validator.IsPresent(txtItemNo) &&
Validator.IsInt32(txtItemNo) &&
Validator.IsPresent(txtDescription) &&
Validator.IsPresent(txtPrice) &&
Validator.IsDecimal(txtPrice);
}

private void btnSave_Click(object sender, EventArgs e)
{
if (IsValidData())
{
invItem = GetNewItem();
InvItemDB.GetItems();
this.Close();
}
}

private void btnCancel_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
using System;
using System.Windows.Forms;

namespace InventoryMaintenance
{
public partial class frmNewItem : Form
{
public frmNewItem()
{
InitializeComponent();
}

InvItem invItem = null;

public InvItem GetNewItem()
{
if (IsValidData())
{
int newItemNumber = int.Parse(txtItemNo.Text);
string newDescription = txtDescription.Text;
decimal newPrice = decimal.Parse(txtPrice.Text);

return new InvItem(newItemNumber, newDescription, newPrice);
}

return null;
}

private bool IsValidData()
{
return Validator.IsPresent(txtItemNo) &&
Validator.IsInt32(txtItemNo) &&
Validator.IsPresent(txtDescription) &&
Validator.IsPresent(txtPrice) &&
Validator.IsDecimal(txtPrice);
}

private void btnSave_Click(object sender, EventArgs e)
{
if (IsValidData())
{
invItem = GetNewItem();
InvItemDB.GetItems();
this.Close();
}
}

private void btnCancel_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
u41c
u41cOP15mo ago
No description
Angius
Angius15mo ago
That helper class lets you save a list of items to a file, yes
u41c
u41cOP15mo ago
No description
Angius
Angius15mo ago
So you need to read items from that file, add the new item, then save items to that file again
u41c
u41cOP15mo ago
Yeah but the list isn't updating once I save and load the items back.
Angius
Angius15mo ago
On btnSave click you get the items from file I don't see you saving them
u41c
u41cOP15mo ago
I've taken a long break from coding and this is very frustrating that I can't figure this out
private void btnAdd_Click(object sender, EventArgs e)
{
// Add code here that creates an instance of the New Item form
// and then gets a new item from that form.
// Create an instance of the frmNewItem form
frmNewItem newItemForm = new frmNewItem();

DialogResult result = newItemForm.ShowDialog();

if (result == DialogResult.OK)
{
InvItem newItem = newItemForm.GetNewItem();

if (newItem != null)
{
newItemForm.Close();
FillItemListBox();
lstItems.Items.Add(newItem);
}
}
else
{
}

newItemForm.Dispose();
}
private void btnAdd_Click(object sender, EventArgs e)
{
// Add code here that creates an instance of the New Item form
// and then gets a new item from that form.
// Create an instance of the frmNewItem form
frmNewItem newItemForm = new frmNewItem();

DialogResult result = newItemForm.ShowDialog();

if (result == DialogResult.OK)
{
InvItem newItem = newItemForm.GetNewItem();

if (newItem != null)
{
newItemForm.Close();
FillItemListBox();
lstItems.Items.Add(newItem);
}
}
else
{
}

newItemForm.Dispose();
}
FillItemListBox(); should do it, right?
Angius
Angius15mo ago
Where do you call InvItemDB.SaveItems(items)? Because that is what saves items to a file
u41c
u41cOP15mo ago
🤦‍♂️ oh lemme try that
Want results from more Discord servers?
Add your server