How to reload the form in c # when the submit button in another form is you clicking?

advertisements

I have a combo box in my C# which is place in form named frmMain which is automatically fill when I add (using button button1_Click) a product in my settings form named frmSettings. When I click the button button1_Click I want to reload the frmMain for the new added product will be visible.

I tried using

frmMain main = new frmMain();
main.Close();
main.Show();

I know this code is so funny but it didn't work. :D

This is windows form!

EDIT

Please see this image of my program for better understanding. This is my frmMain

Here is what my settings frmSettings form look like. So, as you can see when I click the submit button I want to make the frmMain to reload so that the updated value which I added to the settings will be visible to frmMain comboBox.


Update: Since you changed your question here is the updated version to update your products

This is your products form:

private frmMain main;

public frmSettings(frmMain mainForm)
{
  main = mainForm;
  InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
  main.AddProduct(textBox1.Text);
}

It will need the mainform in the constructor to pass the data to it.

And the main form:

private frmSettings settings;
private List<string> products = new List<string>();

public frmMain()
{
  InitializeComponent();
  //load products from somewhere
}

private void button1_Click(object sender, EventArgs e)
{
  if (settings == null)
  {
    settings = new frmSettings(this);
  }
  settings.Show();
}

private void UpdateForm()
{
  comboBoxProducts.Items.Clear();
  comboBoxProducts.Items.AddRange(products.ToArray());

  //Other updates
}

public void AddProduct(string product)
{
  products.Add(product);
  UpdateForm();
}

You then can call UpdateForm() from everywhere on you form, another button for example. This example uses just a local variable to store your products. There are also missing certain checks for adding a product, but I guess you get the idea...