English 中文(简体)
Multi-tab application (C#)
原标题:

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.

最佳回答

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();
问题回答

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

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;
        };           

    }




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签