...

View Full Version : C# help with getting stuff to populate



leedbrierley
01-13-2013, 07:23 PM
Right I have this peace of code. I want it duplicate the textBox, and at the same time, I want them to appear on other tabs in a multidimensional array, I know there is a way of doing it, but I just dont have a bloody clue where to go it has had me perplexed most for over a week now. Any help would be appreciated

private void button1_Click(object sender, EventArgs e)
{
string title = "Round " + (tabControl1.TabCount + 1).ToString();
TabPage myTabPage = new TabPage(title);
tabControl1.TabPages.Add(myTabPage);
tabControl1.SelectedIndex = tabControl1.TabCount;

textBox9.Parent = tabControl1.TabPages[tabControl1.SelectedIndex];
label9.Parent = tabControl1.TabPages[tabControl1.SelectedIndex];
}

negative zero
01-14-2013, 05:41 AM
IIRC, you need to add the TextBox and Label controls to the Controls member of your TabPage... myTabPage.Controls.Add(...);

This: textBox9.Parent = tabControl1.TabPages[tabControl1.SelectedIndex];
... looks like it can be simplified to this: textBox9.Parent = myTabPage;

AceInfinity
01-20-2013, 02:41 PM
private void button1_Click(object sender, EventArgs e)
{
string title = "Round " + (tabControl1.TabCount + 1).ToString();
TabPage myTabPage = new TabPage(title);
tabControl1.TabPages.Add(myTabPage);
tabControl1.SelectedIndex = tabControl1.TabCount;

textBox9.Parent = tabControl1.TabPages[tabControl1.SelectedIndex];
label9.Parent = tabControl1.TabPages[tabControl1.SelectedIndex];
}

I can't understand why you are setting the SelectedIndex just to use it's value later on though here. Why not just use tabControl1.TabCount if that's the value you know you'll be using later on? SelectedIndex has other purposes.

If you're just setting the parent to a specific TextBox though then you're not duplicating anything. Same thing with that Label. Although pionting that out, this won't work anyways unless you use tabControl1.TabCount - 1, becuase the indexing is 0 based...

Example:

string title = "Round " + (tabControl1.TabCount + 1).ToString();
TabPage myTabPage = new TabPage(title);
tabControl1.TabPages.Add(myTabPage);

TextBox T = new TextBox()
{
Text = textBox1.Text,
Size = textBox1.Size
};

tabControl1.TabPages[tabControl1.TabCount - 1].Controls.Add(T);

*Set other properties for the new TextBox accordingly if you want to change the position or anything else... You can do it this way because each TabPage is a container for controls, so it has a Controls.Add() method.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum