Hi Everyone,

I'm working on a project in Visual C# that requires multiple tabs (similar to an internet browser). I was easy to implement the tabs, however Im unable to use the tab pages with a text box.

The textbox is textBox1 and Ive got a lot of code connected to it, so I need this tectBox1 to be initialize every time that I open a new tab.

The code for the button for the new tab that i have is this:

TabPage newPage = new TabPage("New Page");
            tabControl1.TabPages.Add(newPage);
            this.tabPage1.Controls.Add(textBox1);

This makes a new tab appear with the text box, but it "steals" the text box from the previous tab (leaving that tab page empty).

Any ideas would be great!
Drue

Set TextBox1.Parent property.

textBox1.Parent = tabControl1.TabPages[1];

Thanks for the reply.

I tried this method, and what happens is the 1st new tab page that I generate has the text box, but the original and the tab pages i add afterwards dont have the text box. I would like to have the text box on every new tab page that I open.

Thanks again!

The textbox is textBox1 and Ive got a lot of code connected to it

Events? or the name "textBox1" ?

Thanks

Hi,

You need to create a new instance of TextBox and add it to the controls of the Tab Page thus:

TextBox tb = new TextBox();
            tb.Name = "tb" + tabPage1.Name;
            tb.Location = new Point(100, 100);
            tabPage1.Controls.Add(tb);

This will create a control with the name 'tb<TabPage.Name>'. To use the control on a specified page use this:

TextBox tb2 = (TextBox)tabPage1.Controls.Find("tb" + tabPage1.Name, false)[0];

If your code connected to the text box is purely event based then just assign the same procedure to each text box and use the reference passed to the procedure.

Hope this helps

Its the name of the text box, sorry if that was unclear. Im totally new to C# and we have to use it to do our final project.

An example of the type of code connected to the text box is:

When the user clicks a certain button, the contents of a linked list are set to appear in textbox1. Therefore, if the user adds new tab pages, textbox1 should appear on all pages and as a result, maintain its capabilities on every page.

By the way, I just did a little more investigating and I found something called a user control. Would it be possible to create a user control thats simply a text box that i can initialize in every tab page?

Also, I tried to use our method slider212 but Im afraid all that happens now is the text box stays on page 1 and the new tabs are empty.

I feel like Im getting closer to the answer, please dont give up on me yet! lol Thanks for your help so far!

Okay...

Here's what I came up with:

Code for new tab and textBox:

Add textBox1 to the first visible table. And then use this code for the new tab:

TabPage newPage = new TabPage("New Page");
newPage.Name = "tabPage" + (tabControl1.TabPages.Count + 1);
tabControl1.TabPages.Add(newPage);

TextBox tb = new TextBox();
// TextBox will be named "tbTabPage1, tbTabPage2 ...."
tb.Name = "tb" + newPage.Name;

// Do all the "tb.[I]X[/I] = textBox1.[I]X[/I]" below:
tb.Location = textBox1.Location;

newPage.Controls.Add(tb);

The above code will name textBox for each tab as "tbTabPage1 etc". You can't name the textBox for all the tabs as "textBox1".

Now if you want to update the text of the textBox use this code:

void UpdateAllTBs(string text)
{
            textBox1.Text = text;

            foreach (TabPage page in tabControl1.TabPages)
            {
                foreach (TextBox tb in page.Controls)
                {
                    if (tb.Name == "tb" + page.Name)
                        tb.Text = text;
                 }
            }
}

My code won't do exactly what you want but it will give you an idea.

Thanks

Hi,

If I am interpreting your requirements correctly you want 1 text box visible on every tab of a tab control. Well this is either a great solution or a stupid one but it seems to work.

In the forms designer create your text box on the form, not on the tab control. In your form load method move the form to the desired location on the tab control (or sholud I say 'over' the tab control) thus:

textBox1.BringToFront();
            textBox1.Location = new Point(tabControl1.Location.X + 150, tabControl1.Location.Y + 150);

Now just in case the order were to change I have added the following code to the Tab Selected event:

private void tabControl1_Selected(object sender, TabControlEventArgs e)
        {
            textBox1.BringToFront();
            tabControl1.SendToBack();
        }

In my test app the textbox is visible from every page as it is actually floating over the tab control.

It is important that the textBox is not a child of the tab control for this to work. i.e. both must be child controls of the form.

Hope this helps

Regards

Of the two replies, Farooqaaa was the closest to what I'm looking for right now. Thanks a lot anyway slider212! The perfect test to see if the code works is to make a button that adds text to the text box. That button is not going on the tab page but on the form, and therefore is always there.

So what should happen is I click the button (which ill call text) on the tab page im currently on, in its text box should appear the text ("Hello" for example). And this text should only appear on that tab page.

The big question is: How do i connect this button event to different text boxes on different tab pages?
Im on tab page 1, I click the button and the word "Hello" should appear. I move to tab page 2 which opens up with a blank text box. I click the button and the word "Hello" appears on that page as well and so on and so forth...

I investigated it, but I simply couldn't find any clues :(

I hope this makes it clearer for you now slider212, and thank you all for your help! Everyday I'm getting closer to my finishing my project, the tabs are the only thing thats left!

Where is the button?

Is it outside the tabControl?

Thanks

If its outside the tabControl then you can use this method:

void UpdateText(string text)
{
            TabPage selectedTab = tabControl1.SelectedTab;
            selectedTab.Controls["tb" + selectedTab.Name].Text = text;
}

And then put this in button Click event:

UpdateText("text");

Hello, I hope I understood you correctly ..
If you've started to talk about custom controls .. why not create a custom tab? Especially if you'll have an identical - looking tabs, I guess it would be a better way to create a sort of template. Here's an example to show what I'm talking about:
definition of a custom tab:

class CustomTab : TabPage
    {
        //just for example storing a link
        //to newly created texbox
        private TextBox _textBox;

        // a property to set a text 
        //for textBox
        public string BoxText
        {
            set { _textBox.Text = value; }
        }

        //creation of textBox on a Tab
        public void addTextBox()
        {
            TextBox tb = new TextBox();
            tb.Name = "tb" + Name;
            tb.Location = new Point(10, 10);
            tb.Parent = this;
            this.Controls.Add(tb);          
            _textBox = tb;
        }
    }

Ok, now we've encapsulated all the "magic" of creating and setting [inline]TextBox[/inline] inside the custom Tab class. This allows us to do such thing inside of main prog:

//creating a tab
CustomTab ct = new CustomTab();

... making some setups on Tab

//adding new TabPage to our tabControl
tabControl1.TabPages.Add(ct);

//adding a TextBox to our TabPage
ct.addTextBox();

... and maybe somewhere in other part of code (or from your example - on ButtonClick event):

private void button1_Click(object sender, EventArgs e)
{
   ((CustomTab)tabControl1.SelectedTab).BoxText = "Hi there";
}

This is just an example to start from .. you can place creation of the TextBox inside the constructor of CustomTab, or do whatever best passes your case.

Hope this would help :)

Hi there,

Here is the code you are looking for (I think):

Open up a new forms project

Add 1 Tab Control called tabControl1 (remove all tabs)
A 2 buttons (one called Add Tab and one called Add Text)

In the code veiw add the following method:

private void CreateTab()
        {
            TabPage tp = new TabPage();
            tp.Name = "TP_" + tabControl1.TabPages.Count.ToString();

            TextBox tb = new TextBox();
            tb.Location = new Point(100, 100);
            tb.Name = "txt" + tp.Name;

            tp.Controls.Add(tb);
            tabControl1.TabPages.Add(tp);
        }

This creates a tab page and adds a new text box onto it.

In the form load method add a call to CreateTab().

private void Form1_Load(object sender, EventArgs e)
        {
            CreateTab();
        }

Repeat the above for the AddTab button click event.

private void btnAddTab_Click(object sender, EventArgs e)
        {
            CreateTab();
        }

And finally add the following code to the Add Text button click event.

private void btnText_Click(object sender, EventArgs e)
        {
            TextBox tb = (TextBox)tabControl1.SelectedTab.Controls.Find("txt" + tabControl1.SelectedTab.Name ,false)[0];
            tb.Text = "Hello";
        }

The above code uses the SelectedTab to find the current tab and thus find the correct text box.

Is not what you want I can message you my email address and you could send the code that you have.

Regards

Paul

Thank you all very much for your help!

You're welcome. I'm glad you got it working. Please mark this thread as solved if you have found an answer to your question and good luck!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.