这是我目前的水族馆库存更新销售代码。我没有为删除按钮编码任何内容,因为我不知道该怎么做。
namespace Project_Aquarium
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
string[] product = new string[10];
float[] retailprice = new float[10];
float[] costprice = new float[10];
int[] quantity = new int[10];
int[] totalSold = new int[10];
private void btnCheck_Click(object sender, EventArgs e)
{
for (int i = 0; i < product.Length; i++)
{
if (cbProduct.SelectedIndex.Equals(i))
{
lblRetail.Text = retailprice[i].ToString();
lblCost.Text = costprice[i].ToString();
lblInStock.Text = quantity[i].ToString();
lblSold.Text = totalSold[i].ToString();
}
}
}
private void btnCheckClear_Click(object sender, EventArgs e)
{
lblRetail.Text = "";
lblCost.Text = "";
lblInStock.Text = "";
lblSold.Text = "";
}
private void btnAdd_Click(object sender, EventArgs e)
{
if (cbProduct.Items.Count < 10)
{
string name = txtProductAdd.Text;
float cost_price, retail_price;
int totalQty;
string searchProduct = txtProductAdd.Text;
if (float.TryParse(txtRetailAdd.Text, out retail_price) == false)
{
MessageBox.Show("Please Enter Retail Price.");
txtRetailAdd.Clear();
txtRetailAdd.Focus();
}
else if (float.TryParse(txtCostAdd.Text, out cost_price) == false)
{
MessageBox.Show("Please Enter Cost Price");
txtCostAdd.Clear();
txtCostAdd.Focus();
}
else if (int.TryParse(txtQuantityAdd.Text, out totalQty) == false)
{
MessageBox.Show("Please Enter Quantity of Product.");
txtQuantityAdd.Clear();
txtQuantityAdd.Focus();
}
else
for (int i = 0; i < product.Length; i++)
{
if (searchProduct == product[i])
{
MessageBox.Show("Store Already Has This Product.");
break;
}
else if (product[i] == null && searchProduct != product[i])
{
product[i] = name;
retailprice[i] = retail_price;
costprice[i] = cost_price;
quantity[i] = totalQty;
MessageBox.Show(txtProductAdd.Text
+ " was added. "
+ "Quantity is: "
+ txtQuantityAdd.Text);
break;
}
}
cbProduct.Items.Add(name);
cbProductDelete.Items.Add(name);
cbProductEdit.Items.Add(name);
cbProductSold.Items.Add(name);
}
else
{
MessageBox.Show("Storage space has exceeded.");
}
}
private void btnClear_Click(object sender, EventArgs e)
{
txtProductAdd.Text = "";
txtCostAdd.Clear();
txtRetailAdd.Clear();
txtQuantityAdd.Clear();
txtProductAdd.Focus();
}
private void btnSold_Click(object sender, EventArgs e)
{
string searchFish = cbProductSold.Text;
int foundIndex = -1;
int sold = 0;
if (cbProductSold.SelectedItem == null)
{
MessageBox.Show("Please Select A Product.");
}
else if (txtQuantitySEdit.Text == "")
{
MessageBox.Show("Please enter a value for quantity.");
txtQuantitySEdit.Focus();
}
else if (int.TryParse(txtQuantitySEdit.Text, out sold) == false)
{
MessageBox.Show("Please enter a numeric value for quantity.");
txtQuantitySEdit.Focus();
}
else
{
for (int i = 0; i < product.Length; i++)
{
if (searchFish == product[i])
{
foundIndex = i;
break;
}
}
}
if (quantity[foundIndex] < sold)
{
MessageBox.Show("There is not enough fish to be sold. Current product chosen quantity is: "
+ quantity[foundIndex]);
}
else if (foundIndex != -1)
{
totalSold[foundIndex] = totalSold[foundIndex] + sold;
quantity[foundIndex] = quantity[foundIndex] - sold;
lblNettPrice.Text = costprice[foundIndex].ToString("");
lblQuantityChosen.Text = sold.ToString("");
lblTotalPrice.Text = CalculateTotalPrice(sold, costprice[foundIndex]).ToString("C");
}
}
private float CalculateTotalPrice(int sold, float costprice)
{
float nettPrice;
nettPrice = sold * costprice;
return nettPrice;
}
private void btnSave_Click(object sender, EventArgs e)
{
string foundProduct = txtProductEdit.Text;
string editName = txtProductEdit.Text;
float editCost, editRetail;
int editQty;
int editSold;
editName = txtProductEdit.Text;
editRetail = float.Parse(txtRetailEdit.Text);
editCost = float.Parse(txtCostEdit.Text);
editQty = int.Parse(txtQuantityEdit.Text);
editSold = int.Parse(txtSoldEdit.Text);
for (int i = 0; i < product.Length; i++)
{
product[i] = editName;
retailprice[i] = editRetail;
costprice[i] = editCost;
quantity[i] = editQty;
totalSold[i] = editSold;
MessageBox.Show(product[i]
+ " Has Been Updated.");
break;
}
}
private void btnDelete_Click(object sender, EventArgs e)
{
}
private void btnSelectEditProduct_Click(object sender, EventArgs e)
{
for (int i = 0; i < product.Length; i++)
{
if (cbProductEdit.SelectedIndex.Equals(i))
{
txtProductEdit.Text = product[i].ToString();
txtRetailEdit.Text = retailprice[i].ToString();
txtCostEdit.Text = costprice[i].ToString();
txtQuantityEdit.Text = quantity[i].ToString();
txtSoldEdit.Text = totalSold[i].ToString();
}
}
}
}
}
因此,在我的“删除产品”的GUI中,有一个组合框(cbProductDelete)和一个单击删除的按钮。是啊。这个删除产品应该删除产品数组中的所有内容,以便用户可以添加不同的数组,因此最大值为10个数组。嗯。它甚至可能吗?请告诉我,如果我不清楚我的问题..
答案 0 :(得分:1)
这可能/可能不会直接回答您的问题。但我希望我能引导你朝着正确的方向前进。
以“面向对象”的方式进行。引入一个包含相关属性组的类。我们说Product
public class Product
{
public string ProductName{ get; set; }
public float RetailPrice{ get; set; }
public float CostPrice{ get; set; }
public int Quantity{ get; set; }
public int TotalSold{ get; set; }
}
然后,使用List<Product>
而不仅仅是Array
数组的大小是固定的。如果要添加/删除元素,则必须使用Collections
。
private List<Products> myProducts = new List<Products>();
要向列表添加元素,您需要创建Product
的实例,然后添加。
Product product = new Product{ ProductName = "Something", RetailPrice = 10.26f,...};
myProducts.Add(product);
要删除元素,请使用Remove
或RemoveAt
方法。
foreach(var product in myProducts)
{
if(product.ProductName == "ProductToRemove")
{
myProducts.Remove(product);
break;//Must else you'll get an exception
}
}
我希望您可以使用此信息自行解决问题。