-1

I'm wondering if I'm doing this correctly, calling a listview1 from another form. So I can use a query involving listView1 of the Transactions form.

private void button2_Click(object sender, EventArgs e)
{
    ///transactions goback = new transactions();
    //goback.button6.Enabled = true;
    //this.Hide();
    transactions reba = new transactions();
    reba.listView1 = new ListView();
}
Salah Akbari
  • 39,330
  • 10
  • 79
  • 109
Reber Lot
  • 9
  • 1
  • 2
    Where are you *"calling"* a listview? In this code snippet you are creating a new instance of a `ListView` – UnholySheep Mar 10 '17 at 08:17
  • oohhhhhh i see how should i approach tho? – Reber Lot Mar 10 '17 at 08:21
  • To improve the Quality of the Question as per http://stackoverflow.com/help/how-to-ask, please give greater evidence of what research you have done and how things are not working versus how you wanted to work (inluding any error message, unexpected results, etc.). – toonice Apr 08 '17 at 02:15

1 Answers1

2

You can not access the listview1 from another form because it is a private field. But you can access it if you create a property for it:

Fist go to the Designer file of the transactions form (transactions.Designer.cs) and add the following property to it:

public ListView ListView1
{
    get { return listView1; }
    set { listView1 = value; }
}

Then you can access to it from another form:

transactions reba = new transactions();
reba.ListView1... 
Salah Akbari
  • 39,330
  • 10
  • 79
  • 109
  • good example indeed. May be you should add that OP should not create a new instance of the transaction form but use the one which has a non-empty `ListView` – Mong Zhu Mar 10 '17 at 08:37
  • ohh so creating a new instance erases the listview items? Sorry im really new to this but im getting the hang of it XD – Reber Lot Mar 10 '17 at 08:39
  • @ReberLot think of an object/instance as a box. Everything inside the box is gone if you simply throw that box out and place a new box in its place (thats what your doing with = new transactions() and new ListView(). – Mafii Mar 10 '17 at 08:40
  • OHHH i see! , how do i call the ones with contents ? inside it ? what approach should i use – Reber Lot Mar 10 '17 at 08:41
  • 1
    @ReberLot you need to pass the instance of the transaction form to the second one. Have a look at [Send values from one form to another form](http://stackoverflow.com/questions/1559770/send-values-from-one-form-to-another-form) – Mong Zhu Mar 10 '17 at 08:45
  • @ReberLot This answer is about *Calling ListView from another form* as you want but to access the old `ListView` with contents you need to pass it to the new form as Mong Zhu said. – Salah Akbari Mar 10 '17 at 08:49
  • I see! now its starting to clear up! THANKS very much! you three! :D – Reber Lot Mar 10 '17 at 08:51