1

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.

AleMaffe
  • 49
  • 1
  • 7
  • This may help. Check this out - https://stackoverflow.com/questions/45327125/automatically-set-combo-box-value-when-textbox-contains-text – AT-2017 Jun 29 '21 at 07:06
  • `new Form1()` is a wrong way to access *existing form*. You need to pass/make available instance somehow. Ideally you should not deal in views directly, the list should be located in model with all functionality and both form access model instance. – Sinatr Jun 29 '21 at 07:14
  • Does this answer your question? [How to access a form control for another form?](https://stackoverflow.com/questions/4822980/how-to-access-a-form-control-for-another-form) – Sinatr Jun 29 '21 at 07:15
  • Some of the answers in [this related (but not duplicate) thread](https://stackoverflow.com/questions/15605161/how-to-make-form1-label-text-change-when-checkbox-on-form2-is-checked) may help. – Matthew Watson Jun 29 '21 at 08:14
  • You must create a Form1 as class field (private Form1 frm1) in the form where btnConfirmNewProduct_Click lives. Show that frm1, only then all other Form1-controls are instantiated and filled correctly. Then call getProducts() and hide or destroy form1 if you want. – Martin.Martinsson Jun 29 '21 at 08:28
  • why creating new Form1 object is different from instantiating Form1 attribute? If I instantiate form1 attribute, the compiler will automatically point at my control and automatically update my real original Form1 object? – AleMaffe Jul 02 '21 at 15:36

1 Answers1

0

In form1 In button1_Click Event:

 private void button1_Click(object sender, EventArgs e)
    {
        Form2 form2 = new Form2(comboBox1.SelectedItem.ToString());
        if (!form2.IsClose)
        {
            form2.ShowDialog();
        }
    }

in form2 Add that Code

public partial class Form2 : Form
{
    public bool IsClose;
    public Form2(string Item)
    {
        InitializeComponent();
        AddNewItem(Item);
    }
    private void AddNewItem(string Item)
    {
        if (!comboBox1.Items.Contains(Item))
        {
            comboBox1.Items.Add(Item);
        }
        else
        {
            MessageBox.Show("error message is displayed and no value is added.");
            IsClose = true;
            this.Close();
        }
    }
}
  • 1
    thanks! Just a thing, why should I put "isClose=true"? According to this, the next time I try to add a new item it shouldn't display me "form2.ShowDialog();" or i'm wrong? – AleMaffe Jun 30 '21 at 11:18
  • 1
    You are right if you don't need to show form2 then there is no need for a boolean variable – أحمد إبراهيم Jun 30 '21 at 12:33