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

JasonAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
You need to add the ListView to the TABPAGE you created; in this case, the last TabPage in the collection.  Additionally, you need to work with the dynamic ListView you created called "lv".  Something like:
private void CreateMyListView(string x)
{
	// Create a new ListView control.
	ListView lv = new ListView();
	lv.Name = "lv" + x;
	lv.Bounds = new Rectangle(new Point(10, 10), new Size(300, 200));
	// Set the view to show details.
	lv.View = View.Details;
	// Allow the user to edit item text.
	lv.LabelEdit = true;
	// Allow the user to rearrange columns.
	lv.AllowColumnReorder = true;
	// Display check boxes.
	lv.CheckBoxes = true;
	// Select the item and subitems when selection is made.
	lv.FullRowSelect = true;
	// Display grid lines.
	lv.GridLines = true;
	// Sort the items in the list in ascending order.
	lv.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 naame";
	column2.Width = 202;
	column2.TextAlign = HorizontalAlignment.Left;

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

	// Add the ListView to the last TabPage:
	tabControl1.TabPages[tabControl1.TabPages.Count - 1].Controls.Add(lv);
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
it_saigeDeveloperCommented:
One way you could accomplish this:
using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;

namespace EE_Q29070309
{
    public partial class Form1 : Form
    {
        readonly List<string> items = (from i in Enumerable.Range(0, 10) select $"tpItem{i}").ToList();
        public Form1()
        {
            InitializeComponent();
        }

        private void OnLoad(object sender, EventArgs e)
        {
            foreach (var i in items)
            {
                TabPage page = new TabPage(i);
                CreateMyListView(page, i);
                tabControl1.TabPages.Add(page);
            }
        }

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

            lv.Columns.AddRange(new[] {
                new ColumnHeader { Text = "Customer ID", TextAlign = HorizontalAlignment.Left, Width = 159 },
                new ColumnHeader { Text = "Customer Name", TextAlign = HorizontalAlignment.Left, Width = 202 }
            });

            page.Controls.Add(lv);
        }
    }
}

Open in new window


Produces the following output -Capture.PNG
-saige-
1
JasonAuthor Commented:
Woeks a treat thx heaps
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.