Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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 enter image description here

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.

enter image description here

share|improve this question
Windows or Web ?? – yogi Apr 9 at 7:46
4  
Is there a special reason why you want to reload the whole form? Why don't you update just the combobox? – derape Apr 9 at 7:46
you mean adding another button to update the combo box? a separate code? but I want a few buttons in my form, actually I already have one button in my form, so if I add one for update there will be two and I don't like my form loaded with buttons. – Jayseer Apr 9 at 7:50
1  
@NetStarter this.Refresh() doesn't reset values. – Shaharyar Apr 9 at 7:59
1  
yeah it doesn't work. – Jayseer Apr 9 at 8:05
show 7 more comments

3 Answers

up vote 1 down vote accepted

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

This is you 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...

share|improve this answer
Please see my edit that is what my program looks like. – Jayseer Apr 9 at 8:32
@Jayseer I updated my example – derape Apr 9 at 12:00

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
}
share|improve this answer
I will try that but I dont want to reset the comboBox just want to reload it. – Jayseer Apr 9 at 8:06
First give it a try, then ask. This will just reload and remove user selection from item, not clear the items. If you want to clear the items, then use comboBox1.Items.Clear(). – Shaharyar Apr 9 at 8:09
@Jayseer what dou you mean by "update"? What behaviour do you want to provide? – derape Apr 9 at 8:10
I have a separate form for my settings. – Jayseer Apr 9 at 8:33
1  
Where is your data coming from into the gridview? From any DB? – Shaharyar Apr 9 at 8:45
show 2 more comments

you want to Invalidate the form

http://msdn.microsoft.com/en-us/library/598t492a.aspx

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.