How to reload form in c# when button submit in another form is click?
Solution 1
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...
Solution 2
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
}
Solution 3
Try out this code.
this.Refresh();
Application.Doevents();
Solution 4
this.Refresh();
Refresh();
this.Hide();
frmScholars ss = new frmScholars();
ss.Show();
Solution 5
this.Close();
frmMain main = new frmMain();
main.Show();
Jayseer
Updated on August 12, 2021Comments
-
Jayseer almost 3 years
I have a combo box in my C# which is place in form named
frmMain
which is automatically fill when I add (using buttonbutton1_Click
) a product in my settings form namedfrmSettings
. When I click the buttonbutton1_Click
I want to reload thefrmMain
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 thefrmMain
to reload so that the updated value which I added to the settings will be visible tofrmMain
comboBox.