使用相同的表单动态填充tabControls [英] Populate tabControls dynamically with same form
问题描述
我正在根据按钮点击创建一个将有多个标签的表单。
如果我点击2然后2个标签,因为PC1和PC2应该来
I am creating a form which will have multiple tabs according to button click.
if I click "2" then 2 tab as PC1 and PC2 should come
Panel pnlAdd = new Panel();
string[] strPCNames = GetPcNames.Split(',');//strPCNames contains the PC1,PC2
for (int inti = 0; inti < strPCNames.Length; inti++)
{
pnlAdd = this.pnlPCDetails;
if (inti == 0)
{
this.tbcPCDetails.TabPages[0].Text = strPCNames[inti];
this.tbcPCDetails.TabPages[inti].Controls.Add(pnlAdd);
}
else
{
TabPage page = new TabPage(strPCNames[inti]);
this.tbcPCDetails.TabPages.Add(page);
this.tbcPCDetails.TabPages[inti].Controls.Add(pnlAdd);
}
pnlAdd.Name = pnlAdd.Name + inti;
}
我试图创建整个面板的实例并将其调用到标签页但是仍然它不会工作。
任何人都可以建议我如何做到这一点..
提前谢谢..
I tried to create a instance of the whole pannel and calling it to the tab pages but still it wont work.
Can anyone suggest me how i can do it..
Thanks in advance..
推荐答案
嗨sasen903,
听起来你对编码时想做的事感到困惑(经常发生在我身上 - 很抱歉,如果我是这是错误的。
通常有助于将所有步骤写为简单的单词说明:
对你而言(虽然我不完整)了解你的问题)它可以像这样工作:
Hi sasen903,
Sounds like you got confused of what you want to do while coding it (it happend often to me - so sorry if I'm wrong on that).
Often helps to write down all steps as simple word instructions:
For you (although I don't completly understand your problem) it could work like this:
/*Create A Function to return a new Panel filled with a TabControl and TabPages for each string in the given string-Array.
Create the Panel to return
Create a TabControl
Loop through all strings in the given string Array
Create a TabPage for each string and add it to the TabControl
return the new Panel to the caller*/
之后只需填写代码:
After that just fill up with code:
/// <summary>
/// A Function to return a new Panel filled with a TabControl and TabPages for each string in the given string-Array.
/// </summary>
Panel CreateNewTabPanel(string[] astrPCNames)
{
// Create the Panel to return
Panel panel = new Panel();
// Create a TabControl
TabControl tabcontrol = new TabControl();
tabcontrol.Dock = DockStyle.Fill;
tabcontrol.Parent = panel;
// Loop through all strings in the given string Array
foreach(string strPCName in astrPCNames)
{
// Create a TabPage for each string and add it to the TabControl
TabPage page = new TabPage(strPCName);
tabcontrol.TabPages.Add(page);
}
// return the new Panel to the caller
return panel;
}
也许以这种方式你可以放松你的代码和解决你的问题 - 似乎一切都在那里 - 但是你可能在定义什么是父母的时候(或者你的特殊第一个TabPage的分配)犯了错误
$ b $祝你的项目好运。
亲切的问候
Johannes
Maybe in that way you can unwind your code and solve your Problem - It seems everything is there - but you possibly made a mistake in defining what is parent of what (or with assignment of your "Special" first TabPage)
good luck with your project and
kind regards
Johannes
这篇关于使用相同的表单动态填充tabControls的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!