How to Reload Form in C# When Button Submit in Another Form Is Click?

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.

10

10 Answers

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...

0
this.Close();
frmMain main = new frmMain();
main.Show();
1

There is no such built in method to set all your values as you desire. As i mentioned in the comment that you should create a method with your required settings of all controls, here is the sample code:

private void ReloadForm()
{
    comboBox.ResetText();
    dataGridView.Update();   
    //and how many controls or settings you want, just add them here
}

private void button1_Click(object sender, EventArgs e)
{
    ReloadForm();   //and call that method on your button click
}
6

Try out this code.

this.Refresh();
Application.Doevents();
this.Refresh();
Refresh();
this.Hide();
frmScholars ss = new frmScholars();
ss.Show();

you want to Invalidate the form

IF you are looking to refresh page from usercontrol .Here is expample where i amrefreshing form from usercontrol Find the form in which this reload button is. Then Call invalidiate tab control and refresh it.

Dim myForm As Form = btnAuthorise.FindForm()

For Each c As Control In myForm.Controls
                If c.Name = "tabControlName" Then
                    DirectCast(c, System.Windows.Forms.TabControl).Invalidate()
                    DirectCast(c, System.Windows.Forms.TabControl).Refresh() 'force the call to the drawitem event
                End If
 Next

Not required reload for entire form. Just create a function for form initialise. you can call this function any time. This will refresh the form.

private void acc_Load(object sender, EventArgs e)
{
     form_Load();           
}

public void form_Load()
{
    // write form initialise codes example listView1.Clear();...
}

private void button1_Click(object sender, EventArgs e) //edit account
{
    //Do something then refresh form
    form_Load(); 

}

If you want to automatically update the value of the other form when you click the button from another one you can use timer control. Just set the timer to 0.5s in order to update the form fast

I think that, by calling the frmMain_load(sender,e) when you are clicking the button should reload the form.

You may also try to Invalidate() the form just like @Nahum said.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Elena Rostova

Elena Rostova

Lead Health, Wellness & Medical Journalist

Elena Rostova holds a Master's degree in Public Health Journalism. She covers groundbreaking medical research, holistic wellness trends, mental health awareness, and nutritional science.

Share this article
Twitter Facebook Pinterest