views:

745

answers:

5

Hi,

I'm creating a multi-tabbed .NET application that allows the user to dynamically add and remove tabs at runtime. When a new tab is added, a control is added to it (as a child), in which the contents can be edited (eg. a text box). The user can perform tasks on the currently visible text box using a toolbar/menu bar.

To better explain this, look at the picture below to see an example of what I want to accomplish. It's just a mock-up, so it doesn't actually work that way, but it shows what I want to get done. Essentially, like a multi-tabbed Notepad.

View the image here: http://picasion.com/pic15/324b466729e42a74b9632c1473355d3b.gif

Is this possible in .NET? I'm pretty sure it is, I'm just looking for a way that it can be implemented.

A: 

Yes, this question has been answered here it looks like the same thing you're asking for.

Jeremy Morgan
Yes I have that done, but what I really want is to have the items on a toolbar interface only with the control on the currently selected tab when clicked. The tab and control were only created at runtime. Take a multitab notepad as an example: I have already figured out how to create the tabs and add controls, all at runtime, but I want the Edit->Paste function to work properly, ie. paste into the currently selected text area. Hope I explained well.
Zach
A: 

I suggest you keep some "current state" variables updated so you always have a pointer to the selected Tab Page, and its child control (in the case of a tabbed-notepad emulation discussed here : a TextBox). My preference would be to keep track of the TabPage<>TextBox connections using a Dictionary to avoid having to cast the TextBoxes if they are accessed using the TabPage.Controls route : the following code assumes you have a TabControl named 'tabControl1 on a Form :

Dictionary<TabPage, TextBox> dct_TabPageToTextBox;

int tabCnt = 1;

TabPage currentTabPage;

TextBox currentTextBox;

So, as you create each new TabPage at run-time you call something like this :

    private void AddNewTabPage()
    {
        if (dct_TabPageToTextBox == null) dct_TabPageToTextBox = new Dictionary<TabPage, TextBox>();

        currentTabPage = new TabPage("Page " + tabCnt.ToString());
        tabControl1.TabPages.Add(currentTabPage);

        currentTextBox = new TextBox();

        dct_TabPageToTextBox.Add(currentTabPage, currentTextBox);

        currentTabPage.Controls.Add(currentTextBox);
        currentTextBox.Dock = DockStyle.Fill;

        currentTextBox.Text = "sample text for page " + tabCnt.ToString();

        tabControl1.SelectedTab = currentTabPage;
        tabCnt++;
    }

As the end-user changes the selected TabPage you can simply update your current state variables like this :

private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
{
    currentTabPage = tabControl1.SelectedTab;
    currentTextBox = dct_TabPageToTextBox[currentTabPage];

    MessageBox.Show("text in current Tab Page is : " + currentTextBox.Text);
}

So now have the code that is invoked by your menu choices applied only to the currentTextBox.

best, Bill

BillW
A: 

I tried this for fun ... I made a form with a ToolStripContainer, and a ToolStrip inside it, with the standard buttons (which includes the paste button). I renamed the paste button to pasteButton, and hooking everything up you get:

public Form2()
    {
        InitializeComponent();

        TabControl tc = new TabControl();
        toolStripContainer1.ContentPanel.Controls.Add(tc);
        tc.Dock = DockStyle.Fill;

        TextBox selectedTextBox = null;

        pasteButton.Click += (_, __) => selectedTextBox.Paste(Clipboard.GetText(TextDataFormat.Text));

        int pages = 0;

        newTabButton.Click += (_,__) => {                
            TextBox tb = new TextBox { Multiline = true, Dock = DockStyle.Fill, ScrollBars = ScrollBars.Vertical };
            TabPage tp = new TabPage("Page " + (++pages).ToString());                
            tc.Selected += (o, e) => selectedTextBox = e.TabPage == tp ? tb: selectedTextBox;
            tp.Controls.Add(tb);
            tc.TabPages.Add(tp);
            tc.SelectedTab = tp;
            selectedTextBox = tb;
        };           

    }
Richard Hein
A: 

You could use a simple extension method:

 public static void PasteIntoCurrentTab(this TabControl tabControl)
 {
  if (tabControl.SelectedTab == null)
  {
   // Could throw here.
   return;
  }

  if (tabControl.SelectedTab.Controls.Count == 0)
  {
   // Could throw here.
   return;
  }

  RichTextBox textBox = tabControl.SelectedTab.Controls[0] as RichTextBox;
  if (textBox == null)
  {
   // Could throw here.
   return;
  }

  textBox.Paste();     
 }

Usage:

myTabControl.PasteIntoCurrentTab();
Philip Wallace
I only used line 15 of your code, the tabControl.SelectedTab.Controls[0] (getting the correct control) was what I needed. Works perfectly (for now). Thanks (to you and the others as well).
Zach
A: 

It's better to use the WinControl component. It's free at wincontrol.rapiscada.ru

m_shir