简体   繁体   中英

C# how to move items of listview1 from usercontrol to another listview of usercontrol

Is it possible or is there any way to transfer the ListViewItems from usercontrol(child) to the another usercontrol(Parent) how could i do that without instances to parent? because i want to transfer the data from the listview of the child to pass on its parent. i cant instance parent because the parent is already showed but disabled. when my child usercontrol is closed or disposed, the parent will be enabled again how can i move my items from the child to its parent ?

PS: Usercontrol1 is the parent of Usercontrol2

Okay, I think this is right i ended up in using this code:

 private void button1_Click(object sender, EventArgs e)
    {
        string[,] name = new string[listView1.Items.Count,2];//  
        for(int i = 0; i < listView1.Items.Count; i++)       //
        {                                                    // This will insert the items from the listview to array
            name[i,0] = listView1.Items[i].Text;             //
            name[i, 1] = listView1.Items[i].SubItems[1].Text;//
        }                                                    //
        ucAddBook.pass(ref name);//called the method from parent and set name as reference in parameter
        this.Dispose();//this will trigger the parent to loop the array that i pass then insert in the listview
    }

this is the code from parent

    public static string[,] mylist { get; set; }// i set the array to static so if the usercontrol is already disposed the array wont get reset to null
    public void pass(ref string[,] name)        // this is where the array designated
    {
        mylist = name;
    }
    private void usercon_AddBook_ControlRemoved(object sender, ControlEventArgs e)
    {
        panel2.Enabled = true;
        for (int i = 0; i <mylist.Length/2; i++)                //  
        {                                                       //
            ListViewItem item = new ListViewItem(mylist[i, 0]); // this will insert the items from array to listview
            item.SubItems.Add(mylist[i, 1]);                    //
            listView1.Items.Add(item);                          //
        }                                                       //
    }

Please let me know if my code needs an improvements; thanks;

NOTE: I updated the for loop i changed it from mylist.Length-1 to mylist.Length/2 because im using 2d array so the lenght would doubleup thats why i divide it into 2,so my array will not going to out of range.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM