sniffi
sniffi

Reputation: 125

How can I add a tab control to a RichTextBox?

Project description:

Hi, on my project I have 64 client´s they communicate with my main station on a one wire asynchronous bus, now I want to monitor the communication between them. For that I have a RichTextBox , In that box I see the whole data traffic. Now I want to implement the possibility that when I select a client for example nr. 4 that opens a new RichTextBox on a tab control of my main RichTextBox.

My problem is that I work with C# since 4 weeks and I don't know how can I do that, I searched in the internet but I didn´t find any example. So I ask here for help.

Question: How can I make the described requirement?, Is the requirement possible?

Picture for example:

enter image description here

Sorry for the bad picture, I have only Paint. That is my MainWindow at the red arrow´s I want my tabs.

I use Microsoft Visual Studio 2015 and WindowsFormsApplication

Upvotes: 0

Views: 2099

Answers (1)

Aimnox
Aimnox

Reputation: 899

You need to do it the other way arround, add richtextbox to tabpages.

It can be more simple if you make a custom control that inherits from tabpage. The problem with it is that you will be unable to design it via the visual editor. But since you need just one control in it you can add it via code.

The class for the custom tab is as follows:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    class CustomTab:TabPage
    {
        public RichTextBox textbox;
        public CustomTab()
        {
            textbox = new RichTextBox();
            this.Controls.Add(textbox);
            textbox.Dock = DockStyle.Fill;
        }
    }
}

As you can see it just inherits from tabpage and on the constructor it adds a RichTextBox that is docked in fill so it will cover all the page.

The textbox is public, so you can acces it simply with tab.textbox.

To add the tabs to the tabcontrol you just need to add the tabcontrol to the form and when you want to add the page simply:

tabControl1.TabPages.Add(new CustomTab());

Having a custom tab allows you to have all the data and methods that you want (Wich client it is for example)

If you have any quaestion feel free to ask

Upvotes: 1

Related Questions