我想在TabControl上创建一个新的TabPage。新的TabPage生成一个新的ListView和3个TextBoxes。我想向此插入内容。我的问题:我不知道如何为每个新的TabPage访问ListViews和TextBoxes。
这是我的代码:
// Create new Tab on Tabcontroll
private void createNewTabwithNotebookName()
{
TabPage myTabpage = new TabPage(NewNotebook.notebookname);
tcMainWindow.TabPages.Add(myTabpage);
}
public void CreateListviewAndTextboxes()
{
ListView listView1 = new ListView();
listView1.Bounds = new Rectangle(new Point(10, 10), new Size(159, 400));
listView1.View = View.List;
listView1.LabelEdit = false;
listView1.AllowColumnReorder = true;
listView1.CheckBoxes = false;
listView1.FullRowSelect = true;
listView1.GridLines = true;
ColumnHeader column1 = new ColumnHeader();
column1.Width = 159;
column1.TextAlign = HorizontalAlignment.Left;
listView1.Columns.Add(column1);
int i = tcMainWindow.TabCount - 1;
// Add the ListView to the control collection.
this.tcMainWindow.TabPages[i].Controls.Add(listView1);
TextBox textbox1 = new TextBox();
textbox1.Bounds = new Rectangle(new Point(240, 20), new Size(350, 20));
textbox1.Multiline = true;
this.tcMainWindow.TabPages[i].Controls.Add(textbox1);
TextBox textbox2 = new TextBox();
textbox2.Bounds = new Rectangle(new Point(240, 60), new Size(350, 250));
textbox2.Multiline = true;
this.tcMainWindow.TabPages[i].Controls.Add(textbox2);
TextBox textbox3 = new TextBox();
textbox3.Bounds = new Rectangle(new Point(240, 350), new Size(350, 20));
textbox3.Multiline = true;
this.tcMainWindow.TabPages[i].Controls.Add(textbox3);
}
每个ListView / TextBox都应该获得其他内容。请帮忙。
答案 0 :(得分:1)
试试这段代码:
循环浏览TabPages controls
并找到所需的控件:
//method for all tab pages
private void AllTabPages()
{
foreach (TabPage pg in tcMainWindow.TabPages)
FillControls(pg);
}
//method for individual tab page
private void FillControls(TabPage pg)
{
foreach (Control c in pg.Controls)
{
if (c is ListView)
{
//do something
ListView lv = c as ListView;
lv.Items.Add("abc");
lv.Items.Add("def");
}
else if (c is TextBox)
{
//do something
c.Text = "Add Some Text";
}
}
}
如果您想在特定标签页调用FillControls(TabPage pg)
方法中搜索控件:
FillControls(this.tcMainWindow.TabPages[i]);