如何实现形式 - 内 - 一个形式与运行嵌入式形式切换?形式、嵌入式、如何实现

2023-09-06 14:26:08 作者:往事隔山水

我需要实现与手动TabControl的类似的行为(事件,在点击一个按钮为例)的页面切换,并具有设计和实施为单独形式的所有页面。要成立的形式(如面板控制)内的主要形式,取而代之的是根据需要另一种形式。

I need to implement TabControl-like behaviour with manual (on event, on a button click for example) pages switching and having all pages designed and implemented as separate forms. A form to be incorporated (as a panel control) inside main form and replaced by another form as needed.

如何实现这一目标?

PS:为什么我不希望使用TabControl的,而不是是因为有将是太多标签的原因 - 我preFER至present他们的名单,作为一个TreeView和实例上需求。另一个原因来自我的另一个项目 - 还有我要实现的插件,其中在面板内部的主窗口将被类提供动态加载和将运行时切换

PS: The reason why I don't want to use TabControl instead is because there are going to be too many tabs - I'd prefer to present the list of them as a TreeView and instantiate on demand. The another reason comes from another project of mine - there I am going to implement plug-ins, where a panel inside main window will be provided by a class loaded dynamically and will be runtime-switchable.

推荐答案

我做了类似的事情,一次,因为这个原因,我有ReplaceControl方法,我贴如下:

I did similar thing once, and for that reason, I have ReplaceControl method, which I paste below:

    static public void ReplaceControl(Control ToReplace, Form ReplaceWith) {
        ReplaceWith.TopLevel=false;
        ReplaceWith.FormBorderStyle=FormBorderStyle.None;
        ReplaceWith.Show();
        ReplaceWith.Anchor=ToReplace.Anchor;
        ReplaceWith.Dock=ToReplace.Dock;
        ReplaceWith.Font=ToReplace.Font;
        ReplaceWith.Size=ToReplace.Size;
        ReplaceWith.Location=ToReplace.Location;
        ToReplace.Parent.Controls.Add(ReplaceWith);
        ToReplace.Visible=false;
    }

唯一剩下要做的就是在窗体上手动创建一些控制,作为占位符形式。使用标签,例如。

Only thing left to do is to create some control manually on the form, as the placeholder for your Form. Use label, for example.