blah blah blah is here! blah blah » Close

up1down
link

Dear All,

Another GUI Question. My form is like the picture below. There is a tabcontrol component with buttons and textbox. Each buttons has code inside.

Below, there are 2 components, textbox and button. When the textbox is filled with number, and expand is pressed, it will duplicate the tabpage into (number filled in the textbox) pages.



My question is : How to do this? Is this possible? I ve learned that we can make another form and call them if we want. This is similar but different.

Help me guys..thanks :)

So by this example, there will be 6 duplicates of tabPage1 with the same buttons, textbox, and codes.

last answered one year ago

1 answers

link

The only way I can think of doing this without getting into an almighty mess is to clone the tab page and everything on it but to share the eventhandlers you've already coded for the controls on the first tab page, suitably adjusted so that you can tell which tabpage the control is on.

To do this, you'll need to first look at your InitializeComponent() method in the designer generated code in Form1.Designer.cs and then create your 'clone' method from that.

To give you a better idea of what to do, I've created a form containing a tab control (tabControl1), with one tab page (tabPage1) which contains only a textbox (tabTextBox) and a button (btnOne). I've also added a click handler for btnOne:

private void btnOne_Click(object sender, EventArgs e)
{
MessageBox.Show("Button 1 on tab page 1 was pressed");
}

In addition, I added a textbox (textBox1) and an expand button (btnExpand) to the form itself as you've done.

From InitializeComponent(), I first copied this text out into Notepad:
// 
// tabPage1
//
this.tabPage1.Controls.Add(this.btnOne);
this.tabPage1.Controls.Add(this.tabTextBox);
this.tabPage1.Location = new System.Drawing.Point(4, 22);
this.tabPage1.Name = "tabPage1";
this.tabPage1.Padding = new System.Windows.Forms.Padding(3);
this.tabPage1.Size = new System.Drawing.Size(192, 74);
this.tabPage1.TabIndex = 0;
this.tabPage1.Text = "tabPage1";
this.tabPage1.UseVisualStyleBackColor = true;
//
// tabTextBox
//
this.tabTextBox.Location = new System.Drawing.Point(42, 6);
this.tabTextBox.Name = "tabTextBox";
this.tabTextBox.Size = new System.Drawing.Size(100, 20);
this.tabTextBox.TabIndex = 0;
//
// btnOne
//
this.btnOne.Location = new System.Drawing.Point(55, 45);
this.btnOne.Name = "btnOne";
this.btnOne.Size = new System.Drawing.Size(75, 23);
this.btnOne.TabIndex = 1;
this.btnOne.Text = "1";
this.btnOne.UseVisualStyleBackColor = true;
this.btnOne.Click += new System.EventHandler(this.btnOne_Click);

Having done that, I then used the info therein to create a clone method within the form:
private void CloneTabPage(TabControl tc)
{
TabPage tp = new TabPage();
string index = (tc.TabPages.Count + 1).ToString();
tp.Name = "tabPage" + index;
tp.Text = tp.Name;
tp.UseVisualStyleBackColor = true;
tc.TabPages.Add(tp);

TextBox tb = new TextBox();
tb.Location = new System.Drawing.Point(42, 6);
tb.Name = "tabTextBox" + index;
tb.Size = new System.Drawing.Size(100, 20);
tb.TabIndex = 0;
tp.Controls.Add(tb);

Button b = new Button();
b.Location = new System.Drawing.Point(55, 45);
b.Name = "btnOne" + index;
b.Size = new System.Drawing.Size(75, 23);
b.TabIndex = 1;
b.Text = "1";
b.UseVisualStyleBackColor = true;
b.Click += new System.EventHandler(this.btnOne_Click);
tp.Controls.Add(b);
}

Incidentally, it shouldn't be necessary to set location and size properties for the tab page as the tab control seems to sort these out automatically when the tab page is added to its tabpages collection.

I then added this code in the expand button's Click handler:
private void btnExpand_Click(object sender, EventArgs e)
{
int num;
int.TryParse(textBox1.Text, out num);
if (num <= 0) return;
if (num > 6) num = 6; // keep to a reasonable number
for (int i = 1; i <= num; i++)
{
CloneTabPage(tabControl1);
}
}

Finally, I adjusted the code in btnOne's click handler, so that I could tell on which tab page the pressed button was on. This can be done using the sender parameter and then parsing the name of the control:
private void btnOne_Click(object sender, EventArgs e)
{
Button b = (Button)sender;
int tabNum;
if (b.Name == "btnOne")
{
tabNum = 1;
}
else
{
tabNum = int.Parse(b.Name.Replace("btnOne", ""));
}
MessageBox.Show("Button 1 on tab page " + tabNum +" was pressed");
}

It worked surprisingly well :)

gsvirdi
412

I'd suggest that Erya should use a dropdown list instead of a textbox. another thing I was wondering about was that why did u used a "Finite" integer (6) there????..... " if (num > 6) num = 6; "

vulpes
17279

Better still would be a NumericUpDown. Not sure what you mean by a 'finite' integer? The maximum is around 2 billion but the tabcontrol would have blown up well before then! If you mean that you should take into account the existing number of tabs, then - yes - I agree that it would be better to set an overall maximum.

Feedback