0

I'm trying to add an item to combobox that is in one form and the button I'm trying to do that with is in a different form.

Form1 is the form with the combobox and Form2 is the form with the button.

This is the code I tried:

private void dodajGumb_Click(object sender, EventArgs e)
    {
        var frm2 = new Form1();
        frm2.comboFilmovi.Items.Add(imeText.Text);
    }

I also tried to create a public method in form1.cs like this:

public void AddItem(string item)
    {
        comboFilmovi.Items.Add(item);
    }

and this code in form2.cs:

var fr2 = new Form1();
        fr2.AddItem(imeText.Text + " - " + datum.Value.ToString("dd-MM-yyyy") + " - " + vrijemeText.Text);

I don't get any errors, it's just that nothing happens, no new item in combobox. Any advice?

petz666
  • 11
  • 1
  • @C-PoundGuru well I think I did just what it says in that post, but still no results... – petz666 Jun 01 '18 at 13:33
  • I hate voting to close these, but I think the answer is in the other question. You should set up breakpoints and step through the code. – johnny Jun 01 '18 at 18:59

1 Answers1

1

Issue in your code is that you're trying to create new instance of Form1. You have to have constructor for Form2 that accepts Form1 instance as param. Something like this:

private Form1 _form1;
public Form2(Form1 form)
{
    InitializeComponent();
    _form1 = form;
}
 private void dodajGumb_Click(object sender, EventArgs e)
 {
    _form1.comboFilmovi.Items.Add(imeText.Text);
 }

Hope this helped.

Caldazar
  • 2,801
  • 13
  • 21