I'm trying to add a new value chosen by the user in a combobox via another form, where the user has to write the desired value into a textbox. I also want to check that if the value already exists, an error message is displayed and no value is added.
In the form with the combobox to update I wrote this method:
public ArrayList getProducts() //to get all the products into an ArrayList and check if product to add already exists, but i get a cast error
{
return (ArrayList)cbbProducts.Items.Cast<ArrayList>();
}
//this is made in order to add the product to the combobox
public void addProductInCbb(string newProduct)
{
cbbProducts.Items.Add(newProduct);
}
Here i get the first error: I can't cast all the values into an ArrayList properly. In the addProduct form, related to the "confirm" button, i have:
private void btnConfirmNewProduct_Click(object sender, EventArgs e)
{
Order o = new Order(new Form1()); //don't know if access is made correctly...
String newProduct = txtNewProduct.Text;
bool found = false;
ArrayList products = o.getProducts(); //cast error
foreach(String product in products)
{
if (product.Equals(newProduct)) found = true;
}
if (!found)
{
o.addProductInCbb(newProduct);
MessageBox.Show("Success!","", MessageBoxButtons.OK, MessageBoxIcon.Information);
//}
//else MessageBox.Show("Error! Product already exists", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
Here comes the second issue: If I try to comment all the block to check product existance, it doesn't add it anyway, so probably there's a second error in this sense.