-1

I want to add an item to my combobox in form1 from form2. But if I press the button, form2 closes but the item is not added in form1. I hope you can help me! I found no solution for this problem in the internet.

Form 2:

public void button5_Click(object sender, EventArgs e)
{
    Form1 main = new Form1();

    main.AddItem("Item");
    this.Close();
}

Form 1:

public void AddItem(object item)
{
    comboBox1.Items.Add(item);
}
Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
AlGrande
  • 3
  • 4
  • 3
    It's because you create new form and then destroy it. You must have reference to form1 instance, instead of creating new one. – Spawn Sep 24 '15 at 14:03
  • http://stackoverflow.com/questions/1736444/using-the-controls-of-one-form-into-another?lq=1 – ElectricRouge Sep 24 '15 at 14:05

4 Answers4

0

Reference the name property of your 'Form1' don't create a new instance. Then reference that forms combobox control.

Your original code creates a NEW instance. Meaning its creating a new version of Form1 and not accessing the original!

The code below should help

  public void button5_Click(object sender, EventArgs e)
        {

          Form1 myForm = whateverYourFormsNamePropertyIs;
          myForm.Combobox.AddItem("Item");
          this.Close();

        }
Harvey
  • 1,320
  • 3
  • 13
  • 33
  • I tried it but I get this Error: (translated from german) Error 7 "Launcher.Form1" is a "type", but its used a a variable. I used: Form1 myForm = Form1; – AlGrande Sep 24 '15 at 14:55
  • http://stackoverflow.com/questions/7273862/c-sharp-how-to-make-two-forms-reference-each-other Here is a good example of how to reference other forms within your application. You'll need to pass the reference into Form2 on construction – Harvey Sep 24 '15 at 14:57
  • Fix your answer; don't post yet another link. – B. Shea Oct 06 '16 at 18:34
0

First of all: You have to change comboBox access modifier to public. Then:

Form 2:

public void button5_Click(object sender, EventArgs e)
{
     Form1 main = new Form1();
     main.AddItem("Item");

     this.Hide(); // This will hide Form2 ("this." is redundant)
     main.ShowDialog(); // This will show Form1
}
Umut D.
  • 1,746
  • 23
  • 24
0

In form2...

    Form1 f;
    public Form2(Form1 parent)
    {
        InitializeComponent();
        f = parent;
    }
    private void Add_Click(object sender, EventArgs e)
    {
        f.comboBox1.Items.Add("item");
    }

In form1

public void AddItem(object item)
{
  comboBox1.Items.Add(item);
  Form2 f = new Form2(this);
  f.Show();
}
0

orginal form

private void FormPeople_Load(object sender, EventArgs e)
    {
        populateComboBoxTitles();
}
public void populateComboBoxTitles()
        {
            comboBox2.Items.Clear();
            comboBox2.Items.Add("mr");
             comboBox2.Items.Add("miss");

}
private void button5_Click(object sender, EventArgs e)
    {
FormAddTitle formAddTitle = new FormAddTitle(this);
        formAddTitle.Show();
}

secondary form

 FormPeople formPeople;
    public FormAddTitle(FormPeople formPeople)
    {
        InitializeComponent();
        this.formPeople = formPeople;
    }
private void button1_Click(object sender, EventArgs e)
    {
if (formPeople != null)
            formPeople.populateComboBoxTitles();
}