Avatar of Jason
Jason
Flag for Australia

asked on 

C# creating a tabpage with listview dynamically

I need help with the code to create a dynamic tabpage with listview.

I can create the tabpage dynamically but I cant get the listview to be created.
The listview name will need to change when it is created.  I was trying to use the tab name to attach to the listview name for example tab name "Sample" the listview name "lvSample"

Any help would be great

private void tc1TabsAdd()
        {
            foreach (ListViewItem i in listView2.Items)
            {
                string x = i.Text;
                tabControl1.TabPages.Add(x);
                CreateMyListView(x);
                
            }


        }

        private void CreateMyListView(string x)
        {
            Listview lv = "Listview" + x;
            // Create a new ListView control.
            ListView lv = new ListView();
            listView1.Bounds = new Rectangle(new Point(10, 10), new Size(300, 200));
            // Set the view to show details.
            listView1.View = View.Details;
            // Allow the user to edit item text.
            listView1.LabelEdit = true;
            // Allow the user to rearrange columns.
            listView1.AllowColumnReorder = true;
            // Display check boxes.
            listView1.CheckBoxes = true;
            // Select the item and subitems when selection is made.
            listView1.FullRowSelect = true;
            // Display grid lines.
            listView1.GridLines = true;
            // Sort the items in the list in ascending order.
            listView1.Sorting = SortOrder.Ascending;

            //Creat columns:
            ColumnHeader column1 = new ColumnHeader();
            column1.Text = "Customer ID";
            column1.Width = 159;
            column1.TextAlign = HorizontalAlignment.Left;

            ColumnHeader column2 = new ColumnHeader();
            column2.Text = "Customer name";
            column2.Width = 202;
            column2.TextAlign = HorizontalAlignment.Left;

            //Add columns to the ListView:
            listView1.Columns.Add(column1);
            listView1.Columns.Add(column2);


            // Add the ListView to the control collection.
            this.Controls.Add(listView1);
        }

Open in new window

C#

Avatar of undefined
Last Comment
Jason

8/22/2022 - Mon