I have a button that programmatically adds tabs with a listview attached to a tabcontrol. I'm trying to access the listview i created so i can add a listview item(s) to it.
Here is my method that creates the tab with the list view
private void AddTabPage(string tabName)
{
ListView lv = new ListView();
lv.Name = String.Format("listView{0}", tabName);
lv.Dock = DockStyle.Fill;
lv.GridLines = true;
lv.View = View.Details;
lv.Columns.Add("Property", -2);
lv.Columns.Add("Value", -2);
TabPage tPage = new TabPage(tabName);
tPage.Name = String.Format("tab{0}", tabName);
tPage.Controls.Add(lv);
tabControl1.TabPages.Add(tPage);
tabControl1.SelectedTab = tPage;
}
As you can see i have made the control names dynamic. Example (listviewComputer1, tabComputer1) Now how would i go about accessing the listview after it has been created?
Note: I can't add the listview item(s) at the time of creating the listview
Get TabPage by name, then get ListView control from its controls:
var lv = tabControl1.TabPages[tabName].Controls
.OfType<ListView>()
.First();
Now you can add items to ListView:
lv.Items.Add(new ListViewItem(new []{ "Foo", "42" }));
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.