-1

Im trying to add data to a listview from another form. I have tired doing it from the same form and that works like it should, but when I try to pass the info between the forms no data gets entered.

Form 1 Contains listview

private void button3_Click(object sender, EventArgs e)
    {
        newTask form = new newTask();
        form.Show();
    }

Form 2 Contains form to submit info to listview

         public void button1_Click(object sender, EventArgs e)
    {

        string url = textBox1.Text;
        string size = textBox2.Text;

        Form1 table = new Form1();

        table.listView1.Items.Add(url);
        table.listView1.Items.Add(size);


        this.Hide();
    }
Enis.b
  • 23
  • 4
  • Are you trying to add the items on an _existing instance_ of the form? Note that you're currently adding the items to the ListView on a _new instance_ of `Form1` which (at least in the code block above) not yet displayed. – 41686d6564 stands w. Palestine Apr 28 '19 at 15:44
  • Yeah its an existing instace. – Enis.b Apr 28 '19 at 15:47
  • Well, that's the problem. You're adding the items to a completely different (not shown) instance. You need to show us how the _existing instance_ of the form is created so we can help you access it instead. If `Form1` is the main form, then you should show how the _current_ form is created. Please [edit] your question and add the relevant information. – 41686d6564 stands w. Palestine Apr 28 '19 at 15:48
  • `table` is not the same form that has already been opened because you just did `new Form1()`. This continues to be one of the most asked .net questions on SO. Forms are just regular classes so how would you set values on an instance of a class? You would not make a new instance, you would set the values on the existing one. – Crowcoder Apr 28 '19 at 15:50
  • Im not sure what you mean, pretty new to programing. The listview is loaded on the main interface of the application. There is a button which opens up a new form that has couple of text inputs. The code above is the code thats gets ran when I hit the submit button. – Enis.b Apr 28 '19 at 15:52
  • Add the following to the code you have shown and it may become clear: `table.ShowDialog()` – Crowcoder Apr 28 '19 at 15:54
  • 1
    Possible duplicate of [How to access a form control for another form?](https://stackoverflow.com/questions/4822980/how-to-access-a-form-control-for-another-form) – 41686d6564 stands w. Palestine Apr 28 '19 at 15:55
  • If you're using `ShowDialog()` to open the second form, use `ShowDialog(this)` instead so that in the second form you can access the main one by using something like `var table = (Form1)this.Owner;`. If, one the other hand, you are using `.Show()` to open the second form, [this answer](https://stackoverflow.com/a/4823535/4934172) should help you. – 41686d6564 stands w. Palestine Apr 28 '19 at 15:57
  • You should use the Delegates and Callback technique to Send data from one Form to Another – Sayed Muhammad Idrees Apr 28 '19 at 18:04

1 Answers1

0

With Form1 table = new Form1(); you are creating new object type Form1 and it doesn't reference to your old form.

What you need to do is change constructor of second form to

private Form1 myFirstForm;

public newTask(Form1 form1)
{
    InitializeComponent();
    myFirstForm = form1;
}

and your second part of code should then look like this:

public void button1_Click(object sender, EventArgs e)
{

    string url = textBox1.Text;
    string size = textBox2.Text;

    myFirstForm.listView1.Items.Add(url);
    myFirstForm.listView1.Items.Add(size);

    this.Hide();
}
Aleksa Ristic
  • 2,394
  • 3
  • 23
  • 54