Forum guidelines
  • Home
  • »
  • C# Language
  • »
  • How to refresh the combobox from one form in another
AuthorQuestion
How to refresh the combobox from one form in another
Posted By Nel on 20 Feb 2012
Hi,
I have a form with combobox. I want to enter a new value in the combobox form another form

In the second form I have:
 private void button1_Click(object sender, EventArgs e)
  {
  OleDbCommand com = new OleDbCommand();
  com.Connection = conn;
  com.CommandText = "Insert into Komin ( Komin) values ( ?)";
  com.Parameters.AddWithValue("@Komin", textBox1.Text);
  try
  {
  conn.Open();
 
  OleDbDataAdapter oleDbAdapter1 = new OleDbDataAdapter(new OleDbCommand("Select * from Komin order by ID asc", conn));
  com.ExecuteNonQuery();
  dataset1.Clear();
  oleDbAdapter1.Fill(dataset1, "Komin");
 }
  finally
  {
  textBox1.Text = "";
  conn.Close();
  }
  button2.Focus(); 
  }
so, I enter the new value and I can see it in the access database. But when I close the second form I can't see the inserted value in the combobox, until I close and reopen the form. In the first form I have:
private void button11_Click(object sender, EventArgs e)
  {
  Komint komin = new Komin(this);
  komin.Show();
  }


to open the second form for entering the new value.

What can I do to refresh the combobox in the first box on clicking the close button in the second form?
Now I have:
 private void button2_Click(object sender, EventArgs e)
  {
 
 dog.Show(); 
  this.Close();
  }
but if I put this code, two windows are open for the first form.

Please help me if anybody has an idea.

Thanks in advance

AuthorReply
Re: How to refresh the combobox from one form in another
Posted By Pramod Kumar Nandagiri on 20 Feb 2012  
Hi , you put u r second form button1_click code in the second form form_load event and try...

thanks & regards,Pramod.
Nel
  • 0
  • 0
Re: How to refresh the combobox from one form in another
Posted By Nel on 20 Feb 2012  
Thanks, but doesn't solve the problem
Vulpes
  • 0
  • 0
accepted
Re: How to refresh the combobox from one form in another
Posted By Vulpes on 20 Feb 2012   Accepted Answer
There are various ways in which you could do this but perhaps the easiest way is this - I'm assuming the first form is called Form1, its combobox is called comboBox1 and it's been placed directly on Form1, not in some container such as a panel.

The following code should be added to button1_Click on Form2:

Form1 f1 = (Form1)Application.OpenForms["Form1"];
ComboBox cb = (ComboBox)f1.Controls["comboBox1"];
cb.Items.Add(textBox1.Text);


Nel
  • 0
  • 0
Re: How to refresh the combobox from one form in another
Posted By Nel on 20 Feb 2012  
Thank you very much Vulpes:)
Nel
  • 0
  • 0
Re: How to refresh the combobox from one form in another
Posted By Nel on 20 Feb 2012  
Thank you very much Vulpes:)

SPONSORED BY

Custom Software Development
MCN is your source for developing solutions involving websites, mobile apps, cloud-computing, databases, BI, back-end services and processes and client-server applications.