我可以在控件中托管 Windows 窗体吗 [英] Can I host a Windows Form inside a control

查看:26
本文介绍了我可以在控件中托管 Windows 窗体吗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个客户,它作为具有有线表示逻辑的单实例模式的 Visual Basic 项目.

I have a customer which as a Visual Basic Project in single instance mode with a wired presentation logic.

主窗体包含一个带有多个 TabPage 的 TabControl.如果我点击 TabPageA,另一个表单会显示在表单前面并调整大小以使其与 TabPage 的大小相同.

The main form contains a TabControl with mutliple TabPages. If I click on TabPageA another form is shown in front of the Form and resized to have the same size as the TabPage.

如果我点击 TabPageB,第一个表单将被隐藏并显示另一个表单.所以基本上对于用户来说,看起来你有一个带有不同 TabPage 的 TabControl,但事实并非如此.

If I click on TabPageB the first form is hidden and another form is displayed. So basically for the user it looks like you have a TabControl with different TabPages which is not the case.

我尝试将 Forms 转换为 UserControls 并将它们放在 TabPage 中,但是,多亏了 SingleInstance 应用程序,这需要大量重构.我尝试过,但最终因为许多运行时错误而放弃,我不想再为此付出更多努力.

I tried converting the Forms to UserControls and put them inside the TabPage, but, thanks to the SingleInstance app, this would take a whole lot of refactoring. I tried it but eventually gave up because of many many runtime errors and I don't want to put any more effort in this.

我的想法是,在运行时,我可以将表单添加到 TabPages 并让它们像 UserControls 一样工作,这甚至可能吗?

My Ideam was that, at runtime, I could add the forms to the TabPages and let them act like UserControls, is this even possible?

推荐答案

您可以通过将 Form 类的 TopLevel 属性设置为 False 将其转回子控件.它本质上变成了一个带有一些未使用开销的 UserControl.让它看起来像这样:

You can turn a Form class back to a child control by setting its TopLevel property to False. It becomes essentially a UserControl with some unused overhead. Make it look similar to this:

Public Class Form1
    Public Sub New()
        InitializeComponent()
        Dim frm As New Form2
        frm.TopLevel = False
        frm.FormBorderStyle = Windows.Forms.FormBorderStyle.None
        frm.Visible = True
        frm.Dock = DockStyle.Fill
        TabPage1.Controls.Add(frm)
    End Sub
End Class

这篇关于我可以在控件中托管 Windows 窗体吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆